Sub topic 17
Choosing a strategy for your workload
This chapter assumes you know the basic patterns (cache-aside, read-through, write-through, write-back, write-around), TTL, eviction, and invalidation from earlier chapters. It shows how to pick among them for a real workload.
Start with the workload, not the pattern
Four questions narrow the choice quickly:
- How read-heavy is it? A cache pays off when the same keys are read many times per write. Below roughly a 5:1 read-to-write ratio, invalidation traffic can cost as much as the cache saves.
- How much staleness is acceptable? A product description can be minutes old. An account balance usually cannot.
- How skewed is access? If a small set of keys gets most of the traffic, even a small cache reaches a high hit ratio. If access is uniform, a cache needs to hold most of the data to help.
- What happens if a write is lost? This decides whether write-back is allowed at all.
Decision flow
Comparing the patterns
| Pattern | Best for | Main risk |
|---|---|---|
| Cache-aside | General read-heavy data, simple code | Stale reads until TTL or invalidation |
| Read-through | Same as cache-aside, with loading centralized in the cache layer | Depends on the cache library or service supporting it |
| Write-through | Data read soon after it is written | Slower writes, since every write hits two stores |
| Write-around | Write-once, rarely re-read data such as logs | First read after a write is always a miss |
| Write-back | Very high write rates that can be batched | Data loss if the cache fails before flushing |
Layer the decisions
You rarely choose one strategy for the whole system. Pick per data type:
- Static assets: HTTP caching with long
max-ageand versioned URLs. - Reference data such as catalogs: cache-aside with a long TTL.
- User profiles: cache-aside with delete-on-write, plus a short TTL as a safety net.
- Counters and metrics: write-back, if losing a few seconds of data is acceptable.
Common mistakes
- Adding a cache before measuring. Confirm the database or backend is the bottleneck first.
- Choosing a TTL by guessing. Set it from the staleness your users can tolerate, then check it against your measured hit ratio.
- Ignoring stampedes on hot keys. If one key expires under heavy load, add per-key locking or early refresh.
- Using write-back for data you cannot recreate.
Validate with metrics
After deploying, watch hit ratio, p95 latency, and eviction rate. A low hit ratio with many evictions means the cache is too small or the keys are too varied. A high hit ratio with user complaints about stale data means the TTL or invalidation rules are too loose.
Exercise
An analytics service ingests 50,000 events per second. Each event is written once and read only by a nightly batch job. A teammate proposes write-through caching. What pattern fits better, and why?
Show answer
Write-around. Events are rarely re-read, so write-through would fill the cache with entries nobody reads and slow every write by adding a second store. Write-around sends writes straight to the database and leaves the cache for data that is read repeatedly.