System Design · Practice problem · Easy
Stop a Cache Stampede
A cache stampede happens when a popular entry disappears and many readers ask the database for it at once. Let one reader fetch it while the others wait for that answer. Build that here and run it under real traffic.
Easy means one bottleneck to find. You solve it on a canvas that runs your design. The canvas needs JavaScript; everything on this page works without it.
What is the situation?
You run the live match centre for a sports site. On the last night of the season 30 matches kick off together, and readers pile on to the few biggest games, 1,000 reads a second in all. Building a match page is a heavy query, about 100 ms of work, so one Database builds only about 25 pages a second. Design the system behind GET /matches/:id so that pages stay quick and fresh and the bill stays under budget, on a normal night and on a bad one. A few facts about this canvas matter. A Cache that leaves the fetching to an App Server only keeps pages, so each reader who misses a page goes to fetch it. A Cache with its own connection to the Database fetches for itself, and its settings decide what happens when many readers miss the same page at once. A page costs the same heavy query wherever the results are stored, so a Read Replica or an Object Store builds about 25 pages a second too. And a database finishes every query in its line, even after the reader who asked has given up.
What must your design do?
- Reads answer within 500 ms at the 99th percentile, counting the reads that were answered.
- At least 99.9% of reads are served. A reader gives up after 5 seconds.
- No reader sees a page older than 60 seconds.
- The system costs $1,000 a month or less. Most parts have a flat monthly price. An Origin Shield also charges $0.20 for every million reads it takes, an Object Store $0.40 and a CDN $0.60.
- Every page comes from durable storage, because a cache can lose what it holds.
What does Run test?
Run works without an account. It checks your design against 2 tests you can read before you start:
- A normal night. 1,000 reads a second for 65 seconds, with the first 5 seconds not judged, and all 30 pages already in any Cache you built. Nothing goes wrong.
- A restart in the quiet hour. The same start at only 50 reads a second. At second 20 a restart empties every cache in your design, whatever kind it is.
What does Submit add?
Submit needs an account. It runs 2 more tests you cannot read in advance, and stops at the first one your design fails, showing what happened and why. Passing every test, or giving up, unlocks the written solution.
Which parts can you use?
- Load Balancer: about 50,003 a second, $18 a month each
- App Server: about 2,000 a second, $45 a month each
- Cache: about 100,003 a second, $30 a month each
- Origin Shield: about 200,001 a second, $60 a month each
- Database: about 25 a second, $120 a month each
- Read Replica: about 25 a second, $90 a month each
- Object Store: about 25 a second, $15 a month each
Every connection takes 1 ms, and the budget is $1,000 a month.
What does this problem leave out?
- Cache memory limits and eviction.
- Merging misses inside an App Server's own code. A merge on this canvas happens at one shared place, such as the Cache, so the version with one fetch per App Server cannot be drawn.
- Random lifetimes, refreshing a page before it expires, and serving an old copy during a refill.
- A waiting reader tying up an App Server. A read waiting on the database holds no App Server slot here, so App Servers do not run out of threads during a stampede.
- A cache server dying outright. A restart here empties a cache and leaves it running.
- Regions.
Where does this problem fit?
Its category is Read-heavy: Serve billions of reads fast. It practises caching and reliability & ops.
By GOAT Engineer. Explore System Design · Privacy Policy · Contact.