CACHING
THE SHELF
fast copies · 2 ms
name:
—
Alan ✓
Alana ✓
expires:
no timer
5:00
THE APP
serving pages
USERS
thousands, asking for Alan's page
DATABASE
the truth
name:
Alan
Alana ✓
80 ms · slow
The trick that makes
apps 40× faster.
IT LIES — AND IT CAN KILL YOUR DATABASE
Alan
a read
·
Alan
the row it carries
The app answers thousands of users — by asking the
slow database
, every time.
1/5
THE SHELF
Alan
Alan
Alan
1
the first read
2
copy lands
Alan ✓
3
next read
2 ms ✓
First read: on the way back, the app drops a copy of Alan's row onto the
shelf
.
The next read never touches the database —
two milliseconds
, not eighty.
2/5
THE LIE
Alana
1
rename: Alana
Alana ✓
≠ old copy
shows: Alan ✗
You change your name to
Alana
. The database gets the update.
But the
shelf
still says
Alan
— and the app reads the shelf.
That's a
stale cache
.
3/5
THE TIMER
Alana
Alana
5:00
a timer
0:00
—
thrown away
1
fetch the truth
Alana ✓
The fix: every copy gets a
timer
when it lands — five minutes, say.
Time's up? Thrown away.
The next read fetches the truth:
Alana
.
4/5
THE STAMPEDE
0:00
expired
—
1
all miss
1,000 at once
But timers set a trap: a popular copy
expires
…
…and a thousand readers miss at the same moment. They all charge the
database
.
The shelf just
crushed
the thing it was protecting.
5/5
ONLY THE FIRST
Alana
Alana
Alana
1
only the first
Alana ✓
5:00
999 wait ✓
2 ms ✓
The fix: only the
first
miss goes to the database.
The other 999
wait a beat
— then read the fresh copy.
One read rebuilds the
shelf
for everyone.
40× faster — and now you know the cost:
THE SHELF
answers in 2 ms, not 80.
THE TIMER
throws stale copies away.
ONE FETCH
refills it for a thousand.
The shelf's real name: a
cache
.
The famous one:
Redis
.
The trick that makes
apps 40× faster.
It lies a little, saves a lot —
that's caching.