Overview
Curated: · Written: · Reviewed:
Caching Strategies
In one line
A cache keeps a reusable result near the caller so hits skip a slower source. Reach for it when keys repeat, the product can state a staleness budget in seconds or minutes, and the miss path can carry full traffic if the cache goes cold or down.
Key definitions
- Hit ratio: share of lookups served from cache. Quote request hit rate and byte hit rate separately, because a few large objects can dominate bytes.
- Cache-aside: the application reads the cache, loads the source on a miss, then writes the cache.
- Write-behind: the cache acknowledges a write and flushes it later, so until the flush the cache holds the only copy.
- Stampede: many callers miss the same expired key together and all reach the source.
- Stale-while-revalidate and stale-if-error: Cache-Control extensions from RFC 5861 that permit serving a stale response during background refresh or origin failure.
- Fencing token: a monotonic number the store checks so an expired lock holder cannot overwrite a newer write.
Formulas and complexity
These are hypothetical figures. The 50 ms miss time is the full miss path, including the 2 ms lookup.
How it works
A lookup either returns the value or triggers one fill. Single-flight lets one caller load the source while the others wait on that fill. The fill writes the entry with a jittered TTL and a version, so a late writer can be detected and rejected.
When to use and when not to
| Situation | Cache? | Why |
|---|---|---|
| Repeated reads, staleness budget of seconds to minutes | Yes | Hits remove source work |
| Mostly unique keys | No | Entries expire before reuse |
| Must reflect the latest committed write | Only with explicit invalidation or versioning | TTL alone cannot guarantee it |
| Authorization or balance decisions during an outage | No, fail closed | Stale answers are unsafe |
| Write-heavy with read-after-write needs | Invalidate after commit, or write-around | Write-through still races |
| Lost acknowledged writes unacceptable | Write-through or commit to source first | Write-behind holds writes in the cache |
Traps interviewers probe
- Believing write-through means never stale. Fix: write-through still faces partial failure, concurrent writers, retries and replication lag. Commit to the source first, then update or invalidate the cache idempotently.
- Deleting the key after the database write and assuming no race. Fix: a slow reader can store version 7 after version 8 invalidates the key. Use version checks or compare-and-set on store, and keep a bounded TTL as a backstop.
- Deleting only the L2 key. Fix: L1 copies survive. Cap the L1 TTL (for example 30 s) and define what happens when an invalidation message is missed.
- Sizing for hit rate. Fix: at 10,000 rps and 90% hits, the source still sees 1,000 rps, and a cold cache sends about 10,000. Load-test the miss path with the cache disabled.
- Unbounded fallback during a cache outage. Fix: bound fallback concurrency, use a circuit breaker, shed noncritical traffic, and fail closed on authorization.
Interview tips
Opening: "The cache is justified by reuse and a freshness budget of X. The miss path must survive Y requests per second, so I would size and load-test that first."
Expect these follow-ups: the write race in cache-aside, stampede control on a hot key, key design for tenant and locale, CDN privacy for authenticated responses, and what happens when invalidation messages are lost.
30-second outline:
- Goal: which latency, throughput or cost problem the cache solves.
- Reuse: which keys repeat and how skewed the working set is.
- Freshness: a bounded staleness contract per data class, including during failures.
- Pattern and placement: cache-aside or write-through, and which layers.
- Correctness: key dimensions, version checks, and invalidation with a TTL backstop.
- Protection: single-flight, TTL jitter, hot-key handling and backpressure.
- Evidence: hit and byte rate, miss penalty, source load, evictions, and a cache-disabled drill.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.