Read Replicas, High Availability & Failover Basics
Scale reads and survive primary failure with replicas and failover plans.
In plain words
Copies of the database answer read questions so the main one isn't overloaded. If the main one breaks, a copy takes over.
Think of it like…
A head chef (the primary) takes every order, while assistant chefs copy the head chef's notes so they can answer customers' questions. They're a few seconds behind the latest notes. If the head chef falls ill, one assistant takes over, possibly missing the last few notes.
Where you’ve seen it
An app sends writes to the primary and product-page reads to two replicas. A user updates their profile and immediately reloads it from a replica that's 200 ms behind, and sees the old name. The fix: read from the primary right after a user's own write (read-your-writes).
Words you’ll hear
- Primary / replica
- The writable server and its read-only copies.
- Replication lag
- How far behind a replica is; nonzero with asynchronous replication.
- Synchronous replication
- The primary waits for a replica to confirm before COMMIT returns: no data loss, more latency.
- RPO / RTO
- How much data you can afford to lose / how long recovery may take.
Visual diagram
Theory & example queries
Scaling reads, and the lag problem
Replicas let you add read capacity: reporting, search pages and APIs that tolerate slightly old data read from replicas, while writes and freshness-critical reads go to the primary. Many frameworks and proxies support routing reads and writes to different connection pools.
With asynchronous replication, a replica can be milliseconds or, under heavy load, minutes behind. That breaks read-your-own-writes: a user saves something and immediately reads stale data. Common fixes: read from the primary for a short time after a user's write, route session-critical reads to the primary, or check replica lag and fall back when it's too high.
SELECT now() - pg_last_xact_replay_timestamp() AS replication_lag;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative replay lag1 sample row
| replication_lag |
|---|
| 00:00:01.8 |
Note: Lag is illustrative and can change for every measurement. The result is NULL before replay has occurred or when no commit timestamp is available, and the wall-clock calculation is affected by primary/replica clock conditions.
Key takeaway: Replicas add read capacity at the cost of slightly stale data.
High availability and failover
If the primary fails, a replica is promoted and applications are pointed at it, via DNS, a virtual IP or a proxy. Tools like Patroni (PostgreSQL), or managed services such as Amazon RDS Multi-AZ and Cloud SQL HA, automate detection and promotion, typically within seconds to a minute or two.
With asynchronous replication, commits the primary acknowledged but hadn't yet sent are lost on failover; synchronous replication to at least one standby avoids that at the cost of write latency. Automation must also prevent split-brain, where two nodes both believe they're primary. Finally, replicas aren't backups: an accidental DELETE replicates within milliseconds, so point-in-time backups are still essential.
synchronous_standby_names = 'FIRST 1 (replica_b)' makes COMMIT wait until replica_b has the WAL, so failing over to it loses nothing.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Classify your reads
Split reads into those that tolerate a little staleness (listings, reports) and those that must be fresh (just after a user's write, payments).
stale-OK → replica · fresh → primary
When to use it & trade-offs
What it is
A read replica is a copy of the primary database that continuously applies its changes (PostgreSQL streams the WAL; MySQL replays the binlog) and serves read-only queries. Replication is usually asynchronous, so replicas lag slightly behind. Failover promotes a replica to become the new primary when the old one fails; done automatically, it's the basis of high availability (HA).
Why it matters: Replicas spread read traffic and keep a standby ready for outages, but they introduce stale reads, possible data loss on failover and routing complexity. "How do you scale reads?" and "what happens when the primary dies?" are standard system design and backend interview questions.
When to reach for it
- Offloading reporting and read-heavy endpoints
- Keeping a hot standby for automatic failover
- Placing read copies closer to users in other regions
When to avoid or be careful
- Reading a user's own just-written data from a lagging replica
- Treating replicas as backups
- Failover automation without protection against split-brain
Asynchronous vs synchronous replication vs backups
Strengths: No impact on write latency.
Watch out: Stale reads; recent commits can be lost on failover.
Strengths: Committed data is guaranteed to be on the standby.
Watch out: Higher write latency; writes stall if the standby is unavailable (unless configured otherwise).
Strengths: Can restore to a point before an accidental DELETE.
Watch out: Slower recovery than failover; must be tested.
Interview signals & numbers to know
When this topic comes up
- “How do you scale database reads?”
- “What happens if the primary fails?”
- “Users don't see their changes right away”
- Replication lag
- RPO / RTO
Benchmarks & rules of thumb
- ~0 s
- data loss (RPO) with a synchronous standby
- Seconds–minutes
- typical automated failover time
- 0
- protection from accidental DELETE offered by replicas
Common mistakes & follow-ups
Common interview mistakes
- Sending all reads to replicas and showing users stale data right after they save.
- Assuming asynchronous failover never loses data.
- Relying on replicas instead of backups, then losing data to a replicated DELETE.
Likely interviewer follow-ups
- How would you guarantee users see their own writes when reads go to replicas?
- What's the difference between RPO and RTO?
- How does synchronous replication affect write latency?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Read Replicas, High Availability & Failover Basics with hands-on SQL practice and interview Q&A.