Lab · Building blocks
Caching
A cache keeps copies of popular data in memory so most reads skip the slower database. See when that works, what happens on writes, and the three classic failure modes: stale data, stampedes and cold starts.
Try it
- Clients — 100% reads — healthy — 12 ops/s
- App server — Cache-aside — healthy
- Cache (Redis) — LRU · TTL off — healthy — 6/6 keys
- Database — source of truth — healthy — 4 reads/s
- Request
- Cache hit
- Cache miss → DB
- Stale data
- Write
- Invalidate
- Response
Numbers add up:33 reads = 12 hits + 21 misses; misses = 21 DB reads + 0 coalescedThe lab checks this sum as it runs, so a counting mistake would show here.
Controls
Follow a request
Pause to pick a request that is on screen.
What's in the cache right now
Evicted item:20- item:1
- item:3
- item:6
- item:7
- item:5
- item:2
Keys are ordered by when they entered the cache. item:1 is the most popular key, item:20 the least.
Show data table
| Time | Hit rate |
|---|---|
| 1s | 0% |
| 1s | 33% |
| 2s | 57% |
| 2s | 60% |
| 3s | 40% |
| 3s | 50% |
Show data table
| Time | DB reads/s |
|---|---|
| 1s | 18 |
| 1s | 4 |
| 2s | 6 |
| 2s | 4 |
| 3s | 6 |
| 3s | 4 |
What just happened
- 0.3sWarning: Duplicate DB read: item:5 missed again while its first DB read was still running. With heavy traffic this becomes a stampede.
- 0.0sInfo: Cache ready: 6 slots, LRU eviction, Cache-aside.
Compare two write strategies on the same requests
Both sides get the same reads and writes, for the same keys, at the same moments. Only the write strategy differs, so any gap in the numbers comes from the strategy. Both sides start with 20 seconds already run.
Reads fill the cache on a miss. Writes go to the database, then delete the cached copy.
Every write updates the cache and the database before it is acknowledged.
- Clients — 30% writes — healthy
- App server — Cache-aside — healthy
- Cache — 5/6 keys — healthy
- Database — 100 reads — healthy
- Clients — 30% writes — healthy
- App server — Write-through — healthy
- Cache — 6/6 keys — healthy
- Database — 91 reads — healthy
- A read on its way
- A write on its way
- Found in the cache
- Not in the cache: read the database
- The answer, on its way back
Each box is one read or write, named after its key: item:1 is the most popular key. Each side draws only a few at a time, so the boxes stay easy to follow. The table counts every request.
| Measure | Side 1 · Cache-aside | Side 2 · Write-through | Gap |
|---|---|---|---|
| Reads and writes sent | 241 | 241 | Same requests |
| Hit rate | 35% | 41% | Write-through higher by 6 points |
| Database reads | 100 | 91 | Write-through lower by 9 |
| Database writes | 85 | 85 | Same |
| Write latency, p50 (median) | 540 ms | 570 ms | Cache-aside lower by 30 ms |
| Acknowledged writes lost | 0 | 0 | Same |
Changing a setting starts both sides again, so the numbers always describe one setup from its start. The crash button empties both caches at the same moment and keeps the run going. A write-behind cache loses any write it has acknowledged but not yet saved to the database.
What this model simplifies
- Time is slowed: a database read takes 0.26 s here so you can watch it. Real ratios are similar in spirit — memory is roughly 10–100× faster than a database round trip.
- The trip between client and server is slowed too, and it is the same for a hit and a miss. "At the server" leaves it out, so it shows what the cache itself saves.
- The cache is a single node. Real deployments shard keys across many nodes and replicate them.
- Write-behind flushes every 2.5 s in one batch. Real systems flush by time or batch size and may persist a log to survive crashes.
- Every key has the same size and cost. In practice big or expensive-to-compute values change which keys are worth caching.