Back to results for “Caching”
Node.js › Caching

What is multi-tier caching (combining an in-memory L1 cache with a distributed L2 cache), and what problem does it solve?

Advanced

Answer

Multi-tier caching layers a fast, small in-memory (L1) cache on each application instance in front of a larger, shared distributed (L2) cache like Redis -- a read first checks L1 (fastest), falling back to L2 on a miss, and finally to the actual data source, giving both the raw speed of in-memory access for hot data and the consistency/capacity of a shared cache.
const l1Cache = new Map();

async function getData(key) {
  if (l1Cache.has(key)) return l1Cache.get(key);
  const l2Value = await redis.get(key);
  if (l2Value) { l1Cache.set(key, JSON.parse(l2Value)); return JSON.parse(l2Value); }
  const fresh = await db.fetch(key);
  l1Cache.set(key, fresh);
  await redis.set(key, JSON.stringify(fresh), { EX: 300 });
  return fresh;
}
Real-world example A high-traffic API serving the same handful of extremely popular product pages adds a small in-memory L1 cache in front of its existing Redis cache, cutting typical response latency further for those hot items.

Common follow-ups: How do you keep L1 caches on different instances from serving inconsistent stale data relative to each other after an invalidation?;What's an appropriate L1 cache size given it exists on every application instance?