Skip to content
BytePatterns

Caching

System Design: lesson 4 of 15

Keep the answer near whoever keeps asking.

Lesson 4 of 15 · 5 min

Caching

Step 1 of 11

A cache keeps the result of expensive work near whoever keeps asking.

The Idea

A cache stores the result of expensive work close to whoever needs it. A hit answers immediately; a miss does the real work and usually stores the result on the way back. Caching pays off only when a small set of items is requested far more often than everything else.

Real-World Example

A scrub nurse lays out the ten instruments this operation actually uses. Anything else means a runner walking to the store room. The tray is tiny next to the store room, yet it answers nearly every request — because surgical demand is nowhere close to uniform.

The Tradeoff

A cache is a second copy of the truth, so it will be wrong, briefly. Caching requests that rarely repeat buys nothing but memory cost and a stale-data bug. Measure the hit ratio before believing the cache earns its place: ninety hits in a hundred is a cache, nine is a liability.

Your turn

Put the steps in the right order.

  1. Store the fetched value in the cache under the request's key
  2. Look the key up in the cache
  3. Return the value to the caller
  4. On a miss, fetch the value from the database

Mini quiz

1 / 3

A cache hit means: