2.6.2.1 Architecture & Data Flow

1. Polling or WebSocket from a crypto provider.

2. Caching in Redis to reduce API calls (e.g., store updated prices for 30–60 seconds).

3. Broadcast to frontend clients via Socket.io or respond through REST endpoints.

Example Service:

// backend/src/services/marketDataService.js
const axios = require('axios');
const redisClient = require('../config/redisClient');

async function fetchPrice(coinSymbol) {
  const cacheKey = `price:${coinSymbol}`;
  const cached = await redisClient.get(cacheKey);
  if (cached) return JSON.parse(cached);

  // Example: CoinGecko
  const url = `https://api.coingecko.com/api/v3/simple/price?ids=${coinSymbol}&vs_currencies=usd`;
  const res = await axios.get(url);
  const priceData = res.data;

  // Cache result
  await redisClient.set(cacheKey, JSON.stringify(priceData), { EX: 30 });
  return priceData;
}

module.exports = { fetchPrice };

Last updated