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

Lab time 3.0 s
get / setreads on miss, writesClients, 100% readsClients100% reads12 ops/sApp server, Cache-asideApp serverCache-asideCache (Redis), LRU · TTL offCache (Redis)LRU · TTL off6/6 keysDatabase, source of truthDatabasesource of truth4 reads/s
About each part:
  • 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
Hit rate
36%
Watch: most reads miss
Database reads
4/s
Read latency,
570 ms
At the server: hits 30 ms, misses 290 ms
Stale reads
0
Write latency,
—
Lost writes
0

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

Traffic
12/s
0%
Key popularity

A few keys get most of the reads (typical web traffic).

Cache
6 keys
Eviction

Evict the key that was used least recently.

(TTL)
Writes & failures
Write strategy

Reads fill the cache on a miss. Writes go to the database, then delete the cached copy.

One DB read per key at a time; other misses wait for it.

What's in the cache right now

Evicted item:20
  • item:1v1 · used 5×
  • item:3v1 · used 5×
  • item:6v1 · used 2×
  • item:7v1 · used 2×
  • item:5v1 · used 1×
  • item:2v1 · used 1×

Keys are ordered by when they entered the cache. item:1 is the most popular key, item:20 the least.

Hit rate
0%25%50%75%100%1s2s3s
Show data table
TimeHit rate
1s0%
1s33%
2s57%
2s60%
3s40%
3s50%
Database reads per second
051015201s2s3s
Show data table
TimeDB reads/s
1s18
1s4
2s6
2s4
3s6
3s4

What just happened

  1. 0.3sWarning: Duplicate DB read: item:5 missed again while its first DB read was still running. With heavy traffic this becomes a stampede.
  2. 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.

Side 1 write strategy

Reads fill the cache on a miss. Writes go to the database, then delete the cached copy.

Side 2 write strategy

Every write updates the cache and the database before it is acknowledged.

30%
Lab time 20.0 s
Side 1 Cache-aside
Clients, 30% writesClients30% writesApp server, Cache-asideApp serverCache-asideCache, 5/6 keysCache5/6 keysDatabase, 100 readsDatabase100 reads
  • Clients — 30% writes — healthy
  • App server — Cache-aside — healthy
  • Cache — 5/6 keys — healthy
  • Database — 100 reads — healthy
Side 2 Write-through
Clients, 30% writesClients30% writesApp server, Write-throughApp serverWrite-throughCache, 6/6 keysCache6/6 keysDatabase, 91 readsDatabase91 reads
  • 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.

Compare two write strategies on the same requests: the numbers for each side and the gap between them
MeasureSide 1 · Cache-asideSide 2 · Write-throughGap
Reads and writes sent241241Same requests
Hit rate35%41%Write-through higher by 6 points
Database reads10091Write-through lower by 9
Database writes8585Same
Write latency, p50 (median)540 ms570 msCache-aside lower by 30 ms
Acknowledged writes lost00Same

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.