Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Caching properly: write strategies, stampedes, and invalidation

Chapters

Narration

Back at the stall we chalked a slate and called it caching. That version was a toy. Hit rate, a lifetime on each line, and erase whatever nobody asks for. It skipped three questions, and those are the three that page you at two in the morning. What happens on a write. What happens when one hot key expires under load. And which of the five caches between the reader and the truth is the one lying to you. There is never one cache. There are five, stacked between the reader and the truth. The browser holds one. The edge network holds the next. Your own process holds a third in memory. A shared Redis holds the fourth. And the database keeps a buffer pool of the pages it touched last. Closer to the reader is faster, smaller, staler. Further out is slower, bigger, fresher. Same idea, five distances. Reads have one default worth knowing, and it is cache aside: check the cache, miss, read the database, put the answer back yourself. Writes are the half that got skipped, and writes are where correctness gets decided. Write through updates cache and database together: always fresh, slower going in. Write back answers at the cache and drains later: fast, and it loses whatever was in flight when that node dies. Write around skips the cache entirely and lets the next read fill it: nothing gets polluted, the first read always misses. Now the failure that actually kills databases. One key, the hottest one you have, reaches the end of its lifetime and goes blank. Every request that was being served from it misses in the same instant. Not one query to the database: ten thousand, inside the same millisecond, all asking for the identical row. A thundering herd of misses, and it arrives at peak traffic, because peak traffic is what made that key hot. The fix is not a bigger cache. It is single flight: the first miss rebuilds the value, everybody else waits on that one rebuild, and the database sees one query instead of ten thousand. Then jitter the expiry times, so a whole generation of keys never falls over together. Serve the stale value while a refresh runs behind it. And if a key does not exist at all, cache that nothing briefly, or every lookup for it walks straight through you. Which leaves the hard one. Not eviction: invalidation. The moment a value changes, every copy of it is wrong, and you just built five copies at five distances. A lifetime on each entry is the net: pick a number, accept being stale for that long, and the system heals itself. Deleting on write is fresher and far harder, because you have to find every copy, and something is always reading while you delete. Run the net by default, and delete by hand only for what must never be stale. So a cache is not a setting you switch on. It is four decisions: where it lives, how reads fill it, how writes reach the truth, and how a wrong answer gets taken back.

Saved in this browser, and to your account when you sign in.