Question presented to candidate:
"Our primary Postgres database is overloaded with reads. One engineer wants to add read replicas, another wants to put Redis in front. How do you decide, and could you use both?"
What a strong answer should cover:
The core distinction: a read replica is a full copy of the database that can run any query (joins, filters, aggregates) but is a little behind (replication lag); a cache holds a subset of answers under keys you choose, is far faster per lookup, and stays correct only if you invalidate it
Which workload fits which: repeated key lookups of hot objects favour a cache; many different queries, reports and search pages favour replicas
Consistency on replicas: asynchronous replication, read-your-writes (route a user to the primary after their own write), monotonic reads (stick a session to one replica)
Consistency on caches: invalidation on write, TTLs, and the race of refilling a cache from a lagging replica
Cost and operations: replicas cost a whole database instance each and need no application changes beyond routing; a cache needs code for keys, invalidation and failure handling
Using both: cache for the hot keys, replicas for the long tail of queries, primary for writes and must-be-current reads
Clarifying questions expected:
Are the expensive reads repeated key lookups, or many distinct queries?
How stale may a read be, and does a user need to see their own write immediately?
Code / implementation expected: No β a clear comparison with the consistency traps is the answer; the example simulates lag, routing and the cache refill race.
read-replica
caching
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Beginners to system design β you know what a database and a query are; replication and caching are explained here.
Difficulty: Medium
Verified with: Node 22.22 and Python 3.12 (both print identical output).
How to read this doc: Every idea is explained in plain language first, then tagged with π Interview term: so you also learn the exact words an interviewer expects. Behaviour claims (stale reads, counts) come from running the example; the Verified section quotes the full output.
1. Why This Even Matters β A Story First
A busy library has one reference librarian who knows every book. Queues form. You could hire assistant librarians who each have a full copy of the catalogue, updated every evening from the head librarian's notes: they can answer any question, but if a book was moved this afternoon, they do not know yet. Or you could put
The solution is hidden so you can try first
10 min read Β· 11 sections
4
Read the code
Read replica vs cache: query power, replication lag, read-your-writes, refilling a cache from a replica