Scaling Reads
How to serve many more reads than one database can answer: make each query cheap, add read replicas, cache in layers, precompute results, and decide how stale each answer may be.
Overview
Most products are read far more than they are written. A post is written once and viewed thousands of times; a product page changes weekly and is loaded all day. When one database can no longer answer every read, the system has to find ways to answer most reads somewhere else.
Scaling reads is the set of techniques for that: making each query cheaper, adding copies of the database that serve reads (read replicas), keeping recent answers in fast memory (caches, from the browser and the CDN down to the application), and computing expensive answers ahead of time (denormalized tables and materialized views).
Every one of these serves a copy of the data, and a copy can be out of date. So the real design question is not 'how do I add a cache' but 'how stale may this answer be, and what happens when it is'. A like count can be a few seconds behind. A user who just changed their password must not see the old one.
What you will be able to do
- Lay out the read path and estimate how much traffic each layer removes.
- Use read replicas, and handle replication lag with read-your-writes routing.
- Keep caches correct with delete-on-write, TTLs as a backstop, and stampede protection.
- Cache API responses at the CDN with the right HTTP headers.
- Decide when a materialized view or a separate read model is worth its cost.
Key concepts
- Read path layers
- Cache hit ratio
- Read replicas
- Replication lag
- Read-your-writes
- Cache-aside and invalidation
- Stampedes and leases
- HTTP and CDN caching
- Materialized views
- CQRS read models
Interview questions
- A read-heavy service is overloading its database. Walk through how you would scale it.
- How do you give users read-your-writes consistency with async replicas?
- Why delete a cache key on write instead of updating it?
- What is a cache stampede and how do you prevent it?
- How would you cache API responses at the CDN?
- Design the read path for a social feed.
- When would you use a materialized view instead of a cache?
- What are the costs of read replicas?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio