Caching Basics
Before this, it helps to know: Numbers Every Beginner Should Know.
Read a little, play a little. No scary maths, and no rush.
The shop that memorizes regulars
A coffee shop regular walks in; the barista starts their usual before they order. That memory — fast, approximate, refreshable — is a cache. Computers do the same: keep a copy of expensive answers somewhere cheap to reach, and skip the hard work most of the time. A cache hit turns a 200-millisecond database query into a 2-millisecond memory lookup.
The everyday pattern: cache-aside
Your app checks the cache first. Hit? Return it. Miss? Ask the database, store the answer with an expiry (TTL), then return it. The cache fills lazily with whatever people actually ask for — no preloading the universe.
GET product:42
→ cache? HIT → return in 2ms
→ MISS → DB query (200ms) → SET product:42 EX 300 → return
That EX 300 (5-minute TTL) is doing quiet hero work: even if your invalidation logic has a bug, stale data self-destructs in five minutes.
Layers, because one is never enough
A hot product page is typically served by three layers: a CDN at the edge (serves images and pages from a city near the user), Redis in your datacenter (5-minute TTLs for rendered fragments), and the database as the source of truth. Each layer absorbs ~90% of what reaches it, so the database sees a trickle.
The stampede and other ways caches bite
When a hot key expires, a thousand requests can miss simultaneously and stampede the database at once. Fixes: let exactly one request rebuild while others wait (single-flight), add random jitter to TTLs so keys don't expire in lockstep, and version your keys (product:42:v3) so deploys never serve yesterday's format.
What to cache, and what not to
Good candidates are expensive to compute, read often, and change rarely: product pages, user profiles, rendered fragments, results of slow reports.
Poor candidates:
- Values that must be exact right now, such as an account balance at the moment of payment. Read those from the source of truth.
- Requests that are nearly always unique, such as one-off searches. The hit rate stays near zero and the cache only adds a hop.
- Huge objects that are rarely read again. They push useful entries out of memory.
Keeping the cache in step with writes
- Invalidate on write: update the database, then delete the cache key, so the next read fetches fresh data. Simple and common, paired with cache-aside.
- Write-through: write to the cache and the database together, so the cache is always current, at the cost of slower writes.
- Write-behind: write to the cache now and the database later. Fast, but anything not yet flushed is lost if the cache crashes, so it suits data you can afford to lose.
When memory fills up
A cache has a fixed amount of memory, so something must be removed to make room. The usual policy is LRU (least recently used): evict whatever has gone longest without being read. Watch two numbers in production: the hit rate (the share of reads served from the cache) and evictions. A falling hit rate with frequent evictions usually means the cache is too small for the working set.
Remember this
- Cache-aside + short TTL is the safe default for everything.
- Stale data is a business question ("is a 5-minute-old price okay?"), not just a technical one.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
The product cache outgrows its memory
Beginner · 2 questions
A price cache with no eviction
Beginner · 2 questions
Customers see yesterday's price
Beginner · 2 questions
The database spikes every minute
Intermediate · 2 questions
An old name comes back for a day
Senior · 2 questions
Discord's hot partitions and trillions of messages
Senior · 2 questions
Related chapters
Spotted a mistake or want a topic covered? Report an issue