Skip to content

Lesson 6 of 9 · 24m

Caching Patterns That Survive Traffic Spikes

Cache-aside, stampede control, TTL jitter, negative caching, invalidation, and when caching creates correctness bugs.

Caching is easy in a demo because only one request arrives at a time. Production adds concurrency, partial failure, deploy skew, and hot keys. The purpose of a cache is not merely speed — it is protecting the systems behind it. A cache that collapses under a miss storm can take down the database it was meant to shield.

Cache-aside is the default contract

read:
  value = GET key
  if value exists: return it
  value = load from database
  SET key value EX ttl
  return value

The application owns population and invalidation. Redis does not know whether the database changed. This is simple, flexible, and full of race conditions unless you design for them.

Stampede control matters more than hit ratio

A hot key expires and ten thousand requests miss at once. Without coordination, ten thousand database queries execute. This is the classic cache stampede.

Mitigations:

  • request coalescing in the application or library;
  • short-lived locks using SET lock NX PX;
  • probabilistic early refresh;
  • stale-while-revalidate semantics where the product can tolerate staleness.

A lock holder should carry a token and release it with a compare-and-delete Lua script. Blind DEL can delete another worker's lock after your lease expired.

TTLs need jitter

If a batch job writes ten million keys with EX 3600, they all expire in the same second an hour later. Add randomness:

ttl = 3600 + random.randint(0, 600)

Jitter smooths regeneration load. It also prevents correlated expiry across keys that were created together.

Negative caching is a feature and a hazard

Caching 'this object does not exist' protects the database from repeated lookups for missing IDs. Use a short TTL and distinguish a negative sentinel from a real value. Without it, cache penetration attacks or scanning behavior hit the backing store continuously.

Keep negative TTLs shorter than positive TTLs when objects can be created later. Otherwise creation appears delayed for no good reason.

Invalidation is product design

You have three practical choices:

  1. TTL-only — bounded staleness, minimal coordination.
  2. Explicit invalidation — delete or version keys when writes occur.
  3. Versioned keys — include a generation or entity version in the key so old entries become unreachable garbage.

Versioned keys often beat broad deletion. user:42:v17:profile does not require finding every derived key when version 18 appears. It does require memory discipline because old garbage remains until expiry.

Do not cache what correctness cannot afford

Authorization decisions, inventory counts, idempotency results, and money movement can all use Redis — but not as casually stale copies. Decide whether the value is advisory, reconstructable, or authoritative. A wrong answer served fast is still wrong.

Cache hit ratio is a means. Tail latency and database protection are the outcomes.

Field notes

  • Stampede protection must handle lock-holder crashes; every lock needs an expiry.
  • Very short TTLs can be worse than none if regeneration cost is high and misses synchronize.
  • Avoid GET plus SET as two network round trips in hot paths where SET with NX/XX, pipelining, or server-side logic can express the intent.
  • Monitor hit ratio by key family, not just globally. 99% overall can hide 0% on the expensive query.
  • Empty collections are valid cached values; distinguish them from misses or every empty result becomes a database hit.

Was this lesson helpful?

Sign in to rate