Topic 2 of 6 · 5 min read
Caching layers and invalidation risks
Goal: Find the staleness or data-leak bug in a proposed cache design and choose an invalidation strategy that fits the data.
A cache trades correctness for speed, so every cache in a design needs three answers: what is the key, how stale may the data be, and what happens when it is wrong. Most cache bugs are not slow code. They are the wrong person seeing the wrong data.
Check yourself · 1 of 3
A CDN caches the page /account for 10 minutes, and the key is only the URL. What is the main risk?
Why it matters
Caches are the cheapest way to cut load, so designs add them everywhere. Each layer is a second copy of the truth. A CDN caches pages near the user. An application cache such as Redis holds computed results. The database has its own buffer cache. If two copies disagree, someone gets an old price, an old permission, or another user's page.
How to review it
- List the layers and what each caches.
- Read the cache key. Does it include everything the response depends on: user id, language, currency, permissions, query parameters? A key such as
/dashboardcached at a CDN can serve one user's dashboard to everyone. - Decide the staleness budget per data type. A product description can be an hour old. A bank balance or a permission change cannot.
- Check invalidation. Time-to-live (TTL) expiry is simple. Explicit deletion on write is exact but easy to forget on one code path. Versioned keys (
product:42:v7) avoid deletion by moving to a new key. - Check the pattern. Cache-aside means the app reads the cache, falls back to the database and fills the cache. It is the common choice and tolerates a cache outage.
- Check the stampede. When a popular key expires, hundreds of requests hit the database together. Look for request coalescing, jittered TTLs or early refresh.
Before and after
Weak:
Cache GET /api/account responses in the CDN for 10 minutes.
Better:
Do not cache /api/account at the CDN: the response is per user.
Cache GET /api/products/:id for 10 minutes, keyed on id and
language. On product update, delete the key; TTL is the backstop.
What goes wrong
- Shared key for private data. The most serious cache bug: user A sees user B's data.
- Invalidation on only one write path. The admin tool updates the row but the import job does not clear the cache.
- Stampede on expiry. A homepage query that takes a second now runs a thousand times at once.
- Caching errors. A failed lookup cached as "not found" keeps failing after the cause is fixed.
Examples
The leaked dashboard
A design caches GET /dashboard at the CDN for five minutes and identifies users by cookie. The cache key ignores the cookie, so the first user's dashboard is served to the next visitors. The fix is to mark the response private or include the user in the key, and not to cache personal pages at a shared layer.
Stale permissions
An application caches a user's role for one hour. An admin removes a contractor's access, and the contractor keeps it for up to 60 minutes. For permissions the design should use a short TTL or delete the key on the role change.
Key points
- Every cache needs a key, a staleness budget and an invalidation rule written in the design.
- A cache key must include everything the response varies on, especially the user for personal data.
- TTL is the safety net; explicit invalidation or versioned keys give faster correctness.
- Check every write path, not just the main one, for cache invalidation.
- Popular keys need protection from stampedes when they expire.
Common mistakes
- Caching a personal response under a key that has no user in it.
- Relying on TTL alone for data that must be correct right after a write.
- Adding a cache without saying how stale the data may be.
- Forgetting that a cache outage can overload the database it protects.
Check yourself · 2 of 3
Which data tolerates a long cache TTL best?
Check yourself · 3 of 3
A popular cache key expires and a thousand requests hit the database at once. What is this called?
Practice items, not counted towards your mastery.
Start here
- The System Design Primer (opens in a new tab)ToolFree
A free, long-lived study guide covering scaling, caching, queues and back-of-envelope estimates.
- Site Reliability Engineering (Google SRE book) (opens in a new tab)BookFree
Google's free book on running reliable services: monitoring, overload, failure and release practice.
- Cloud design patterns (Azure Architecture Center) (opens in a new tab)DocsFree
A catalogue of named patterns such as cache-aside, retry, circuit breaker and queue-based load leveling.