Skip to content
Caching

Lesson 1 of 14 · 16 min

x
1/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

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
1// High RPS example → one DB query per request without a cache (illustrative)2app.get('/products/:id', async (req, res) => {3  const product = await db.query(4    'SELECT * FROM products WHERE id = $1',5    [req.params.id]6  );7  res.json(product);8});
After
With cache — most requests served from memory
1// Same RPS example → far fewer DB queries at a high hit ratio (illustrative; depends on key skew)2app.get('/products/:id', async (req, res) => {3  const cached = cache.get(`product:${req.params.id}`);4  if (cached) return res.json(cached);       // hit5 6  const product = await db.query(...);        // miss7  cache.set(`product:${req.params.id}`, product, 300);8  res.json(product);9});

Check your understanding

  • What is hit ratio?Show answer

    Answer

    hits / total lookups — the primary health signal for whether the cache is earning its keep.
  • What happens on a miss?Show answer

    Answer

    Load from the source of truth, optionally populate the cache, then return the value.
  • Name three reasons caches exist.Show answer

    Answer

    Lower latency, lower cost/load on backends, and absorbing read spikes — for the cached subset of traffic.

Progress is saved in this browser.

Next Lesson