System Design Notebook / 10
Caching
How wrong you are willing to be, and for how long
Where a cache goes and what it costs you when it is stale. The hit ratio arithmetic, the stampede that turns smooth load into spikes, and the invalidation nobody gets right the first time.
- pages
- 77
- sections
- 47
- editions
- Reading, Print, Tablet
Sign in to read it
Sign in and the whole notebook opens, all three editions with it. An account is what makes a download belong to someone.
Sign in with GitHub or GoogleStart reading
How to use this book
What is inside
Every section, and every one of them opens in the browser. Nothing here is hidden: if a section you need is not in this list, the notebook is not the right one and you should not spend a week on it.
- How to use this book
- The whole picture on one page
- 1The requirement: the query that runs a million times
- 2What a cache actually is
- 3The arithmetic of a hit ratio
- 4The cost you just took on
- 5What must never be cached
- 6The hierarchy, end to end
- 7Hardware caching, and the numbers worth memorising
- 8Why RAM is fast, and why sequential is faster still
- 9The caches you did not configure
- 10DNS, and caching at five levels
- 11CDN: the edge, the PoP, and the origin
- 12The application cache: Redis and Memcached
- 13Cache-aside, and the race inside it
- 14Read-through
- 15Write-through, and the claim that it is always fresh
- 16Write-behind
- 17Refresh-ahead and stale-while-revalidate
- 18Choosing
- 19Why invalidation is the hard part
- 20TTL is the honest answer
- 21Delete, do not update
- 22The ordering problem
- 23Versioned keys
- 24Cache key design, and the day you serve the wrong user's data
- 25The thundering herd
- 26Avalanche, and the one line that prevents it
- 27Penetration, and caching the miss
- 28Hot keys
- 29When the cache goes down
- 30Eviction: what to drop when you run out
- 31Cache-Control, and the directive everybody misreads
- 32max-age, s-maxage, and who obeys which
- 33ETag and conditional requests
- 34stale-while-revalidate
- 35Vary, and the cache key you did not know you had
- 36Single-threaded, and the command that kills production
- 37Persistence, and what durable means here
- 38Sessions and rate limits, and the bug in the counter
- 39Redis or Memcached
- 40The delivery script
- 41Whiteboard: adding a cache live
- 42Follow-up question bank
- Appendix A. Numbers worth memorising
- Appendix B. The caching checklist
- Appendix C. Glossary
- Appendix D. Self-test
How to read it
Read with a pen. Every notebook opens with a question to answer before you start and asks you to redo the answer at the end, and the margin in the Print edition exists so you have somewhere to be wrong first. The Tablet edition is 16:9 with vector text, so note apps draw on it rather than treating it as a photograph.