Lessons › From one user to millions
Add a cache
20,000 req/s at peak, 95% of them reads, and the machine budget is $1,800/month. Adding more and more replicas is getting expensive.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
Most traffic hits a small set of popular data. A Cache (Redis) keeps it in memory: about 1 ms per read instead of ~10 ms, and ~100,000 operations/sec per instance (§7).
Cache-aside: the app checks the cache first. On a miss it reads the database and stores the result. The hit rate decides how much load the database still sees.
What you have to achieve
20,000 requests a second at the busiest hour, 5% of them writes.
- No broken or illegal connections
- Use a Cache (Redis)
- No component is overloaded
- Fewer than 0.1% of requests fail
- p99 response time under 250 ms
- Machines cost at most $1,800 / month
What you learn
A 90% hit rate cuts database reads by 10×. Look at the bill too: internet traffic costs far more than the machines, and every byte you send counts.
Check yourself
A cache has a 90% hit rate. How much does it cut database reads?
- By 10%
- By 2×
- By 10×
Show the answer
By 10×. Only 1 read in 10 misses and reaches the database, so it sees a tenth of the reads.
In cache-aside, what happens on a miss?
- The request fails
- The app reads the database and stores the answer in the cache
- The cache asks the database by itself
Show the answer
The app reads the database and stores the answer in the cache. The app checks the cache first. On a miss it reads the database, then saves the result so the next read hits.