Caching

Lesson 1 of 14 · 16 min

x
7%

What Is Caching and Why It Exists

A cache is a fast storage layer that holds copies of data so future requests can be served without repeating expensive work. Every time your app queries a database, calls an external API, or recomputes a result, it spends time and money. Caching trades memory for speed — store the answer once, reuse it many times. Caching solves three problems at once. Latency drops because reading from RAM is orders of magnitude faster than hitting a database over the network. Cost falls because fewer database queries mean smaller instance sizes and lower cloud bills. Load on downstream systems shrinks — your database handles 10x more traffic when 90% of reads never reach it. The two outcomes every cache produces are a hit and a miss. A cache hit means the requested data was found in the cache — fast path. A cache miss means it was not — the system must fetch from the source, store the result, and return it. Hit ratio (hits / total requests) is the single most important metric: a 95% hit ratio means only 1 in 20 requests touches the slow path.

Before
No cache — every request hits the database
// 1000 requests/sec → 1000 DB queries/sec
app.get('/products/:id', async (req, res) => {
  const product = await db.query(
    'SELECT * FROM products WHERE id = $1',
    [req.params.id]
  );
  res.json(product);
});
After
With cache — most requests served from memory
// 1000 requests/sec → ~50 DB queries/sec at 95% hit ratio
app.get('/products/:id', async (req, res) => {
  const cached = cache.get(`product:${req.params.id}`);
  if (cached) return res.json(cached);       // hit

  const product = await db.query(...);        // miss
  cache.set(`product:${req.params.id}`, product, 300);
  res.json(product);
});

Key Takeaway

Caches trade memory for speed — measure hit ratio to know if your cache is actually helping.

Next Lesson