Replication and high availability
A database is usually the hardest part of a system to make highly available, because it holds state. An application server can simply be replaced. A database must be replaced with one that has the same data, and every design choice trades off how much data might be lost, how long recovery takes and how fast every write is.
Replication keeps copies of the data on other servers. High availability (HA) adds the ability to switch to a copy automatically when the primary fails. Backups protect against what replication can’t: mistakes, corruption and attacks that replicate instantly to every copy.
Diagram
Section titled “Diagram”- Applications connect through a router or driver that knows which server is the primary.
- All writes go to the primary; read-only queries can go to replicas, accepting a little lag.
- Each write is confirmed only after the standby in another zone has it, so no committed data is lost if the primary fails.
- A failover manager watches the primary and promotes the standby if it fails, using a quorum to avoid two primaries.
- Backups and transaction logs allow restoring to any point in time, including just before a bad change.
Synchronous vs asynchronous replication
Section titled “Synchronous vs asynchronous replication”| Aspect | Synchronous | Asynchronous |
|---|---|---|
| A write is confirmed when | The standby has it too | The primary has it |
| Data lost if the primary fails | None (RPO zero) | Whatever hadn’t replicated yet |
| Write latency | Higher: adds a round trip to the standby | Unaffected |
| If the standby is slow or down | Writes slow or stop, unless configured to fall back | Nothing changes for writes |
| Typical use | A standby in another zone, close by | Read replicas and replicas in other regions |
How it works
Section titled “How it works”- One writer at a time is the norm for relational databases: a single primary accepts writes and replicates its log of changes (for example, PostgreSQL’s write-ahead log or MySQL’s binary log) to the others.
- Multi-zone first. A synchronous standby in a second availability zone covers most failures with no data loss. Cloud managed databases offer this as a setting.
- Read replicas scale reads, not writes. They lag behind the primary, so a user who just saved something may not see it on a replica. Route those reads to the primary.
- Automatic failover needs a referee. A failover manager (or the managed service) uses health checks and a quorum so a network split doesn’t produce two primaries, which is called split brain.
- Applications must reconnect. After failover the primary’s address changes. Use the provider’s endpoint, a proxy or a cluster-aware driver, with retries and sensible timeouts.
- Backups are not replication. A dropped table replicates to every copy in milliseconds. Point-in-time recovery from backups and logs is the protection. Test restores regularly.
- Distributed SQL and multi-leader designs (for example, databases built on consensus protocols such as Raft or Paxos) accept writes in several places, but at the cost of latency and complexity. Use them when the requirements truly need it.
- Survives server and zone failures with little or no data loss.
- Read scaling through replicas.
- Maintenance without downtime: patch the standby, fail over, patch the old primary.
- Synchronous replication adds write latency and can block writes if the standby is unhealthy.
- Replica lag creates confusing “where did my data go?” bugs.
- Failover is never instant; applications see errors for seconds to minutes and must handle them.
When to use it
Section titled “When to use it”Every production database that matters should have at least a standby in another zone and tested point-in-time recovery. Add read replicas when reads dominate, and a cross-region replica when a region outage would be unacceptable.
When to avoid it
Section titled “When to avoid it”Development and test databases rarely need HA. Backups are usually enough, as long as you can recreate the environment.
Common pitfalls
Section titled “Common pitfalls”- Never testing failover, then discovering the application doesn’t reconnect.
- Reading your own writes from a lagging replica.
- Treating replicas as backups, until a bad migration or ransomware replicates everywhere.
- Backups never restored, so nobody knows how long recovery really takes.
- Long-running transactions that block replication or vacuuming and grow storage without limit.
Related pages
Section titled “Related pages”- Partitioning and sharding
- Zero-downtime schema changes
- High availability and multi-region
- Scenario: disaster recovery
Glossary: CAP theorem, eventual consistency
Further reading
Section titled “Further reading”- PostgreSQL, High availability, load balancing and replication (opens in a new tab)
- MySQL, Replication (opens in a new tab)
- Martin Kleppmann, Designing Data-Intensive Applications, chapter 5: Replication (O’Reilly)
- AWS, Amazon RDS Multi-AZ deployments (opens in a new tab)
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.