SproutStackSproutStack home

Caching Basics

6 min readBeginner-friendlyFree to read

Before this, it helps to know: Numbers Every Beginner Should Know.

Read a little, play a little. No scary maths, and no rush.

Share:

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.

miss1. check2. on a missUserCDNedge, near the userApp serverRedisTTL 5 minutesDatabasesource of truth
Each layer answers most of what reaches it. On a Redis miss, the app reads the database and stores the answer with a TTL.

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

1. A popular product key expires and several hundred requests miss at the same instant, all hitting the database. What is this called and what fixes it?
2. With cache-aside, your app checks the cache, then the database on a miss, then writes to the cache. Why is this the most common default?
3. Your team wants to delete a cache key the instant a product price changes. What is the most reliable approach?
4. Which layer should sit in front of your database for a read-heavy page?
5. A cache hit rate of 95% still leaves your database handling 5% of a large request volume. Is that a problem?

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

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.