Read replicas and stale reads
Spread reads across copies of the database and know when they return old data.
One writer, many readers
A replica is a copy of the database that follows the primary. All writes go to the primary; reads can go to any replica. Three replicas roughly triple read capacity.
Replication is usually asynchronous: the primary confirms a write, then ships it to replicas a few milliseconds (or, under load, seconds) later.
The catch: replication lag
A user who posts a comment and then reloads may hit a replica that hasn't received it yet, and the comment seems to vanish.
Fix it where it matters: read your own writes from the primary for a short time after writing, and let everyone else read from replicas.
read(userId, key):
if userId wrote key in the last 10 s:
return primary.read(key) // fresh, for the writer
return anyReplica.read(key) // may lag a little
Replicas or a cache?
A cache is cheaper per read and faster, but holds only hot items. Replicas serve every query, including ones a cache can't (searches, filters). Big read-heavy systems use both.
Key takeaways
- Writes go to the primary; reads spread over replicas.
- Replicas lag, so a writer may not see their own change.
- Send a writer's own reads to the primary for a short while.
Try it in the simulator: Add replicas until reads fit
Put this concept into practice on E-Commerce Search & Catalog:
- Build Client → Load balancer → API servers → PostgreSQL with no cache, and run it.
- Select PostgreSQL and raise Read replicas to 1, then 2, running after each change.
What to watch: With no replica the database is overloaded and the run fails. Each replica takes a share of the reads, so the database's load falls and p99 recovers, while the cost rises. The run passes, yet the simulator still notes that a cache is what this problem expects, because nearly every read is for the same popular products.
Check your understanding
Answer these questions interactively to test what you learned and track progress on your skill map:
- A user edits their profile, reloads, and sees the old version. What is the likely cause?
- What do read replicas not help with?