Core system concepts · Concept lesson
Replication
Understand how copies of data are maintained for availability, read scale, and recovery, and reason about lag and failover.
Written for readers who already know the basics. If the words here are new to you, read The request lifecycle and Capacity estimation first.
Original educational architecture · Version 2026-11-12.1 · Examples use stated hypothetical assumptions, not a company's actual design.
What you will be able to explain
- Distinguish synchronous and asynchronous replication tradeoffs.
- Explain replication lag and read-after-write behavior.
- Describe safe failover, promotion, and replica recovery concerns.
How replication works
Replication maintains multiple copies of data by transmitting changes from one node to one or more others. In a primary-replica model, the primary accepts writes and publishes a log or change stream; replicas apply those changes in order or according to the database's rules. Synchronous replication waits for selected replicas to acknowledge before confirming a write, which can improve durability against certain failures but adds latency and may reduce write availability when acknowledgements cannot arrive. Asynchronous replication acknowledges earlier and accepts some risk that the latest changes have not reached a surviving replica. Replicas can serve reads, provide recovery copies, or support analytics, but they may be behind. Replication is not the same as backup: deletions and corruption can propagate. Nor does having copies alone prove that failover can promote the right copy without losing committed data. Define the failure model, acknowledgement point, and recovery objectives.
Submits a state change to the current primary.
Accepts writes and publishes ordered changes.
Receives and applies changes, possibly later.
May read from primary or a lagging replica.
Provides historical recovery beyond live replication.
- Writer → Primary: write
- Primary → Replica: replication stream
- Primary → Reader: current read
- Replica → Reader: possibly stale read
- Primary → Independent backup: separate recovery capture
Worked example: replica lag
Consider a primary outage after it acknowledged a write at log position 50,000, while the candidate replica has applied only through position 49,700. If acknowledgements were asynchronous, the missing 300 positions may include committed writes; promoting this replica can lose them. This is a recovery-point decision, not a way to make the replica current. Before promotion, stop or fence the former primary so it cannot accept writes if it later becomes reachable. Select the most advanced eligible replica according to the documented recovery policy, record the promotion point, and route clients to the new authority only after its write path is ready. The old primary must rejoin by following the new history or be rebuilt; allowing both timelines to accept writes creates divergent state that cannot be repaired by simply reconnecting replication. After service returns, reconcile operations that may have been acknowledged but not copied using durable request records or business evidence. A replica's reported delay can be time-based, while its log position shows missing work; monitor both. Test this sequence in an isolated drill and state the acceptable data-loss window before an incident forces an improvised choice.
Assume a write stream produces 2,000 change records per second and a replica applies 1,800 records per second for 30 seconds. During that interval, backlog grows by (2,000 − 1,800) × 30 = 6,000 records. If input then falls to 1,500 records/second and application remains at 1,800, the replica catches up at 300 records/second and needs about 6,000 ÷ 300 = 20 seconds to clear the backlog. A user who writes a profile and immediately reads from this replica may see the older value during that window. Route read-after-write requests to the primary, wait until a replication position is reached, or return the written representation directly according to the contract. These numbers assume steady rates and equal cost per record; variable record sizes and apply contention can invalidate the estimate. Monitor both time lag and byte or log-position backlog because each answers a different operational question.
Replication alternatives
Synchronous replication can require one or more acknowledgements before commit; quorum variants wait for a defined subset, while asynchronous systems accept writes without waiting for all copies. A single-writer topology simplifies conflict handling but concentrates write ownership. Multi-writer replication permits writes at several sites but needs conflict detection, resolution, or a domain model that avoids conflicting updates. Physical replication copies storage-level changes and can be efficient for compatible versions; logical replication streams selected tables or changes and can support transformation, but introduces schema and ordering concerns. Read replicas improve some read workloads only when their lag and workload isolation are acceptable. A backup or point-in-time recovery archive provides a different protection layer. Select the topology based on write locality, recovery objective, consistency needs, and operator ability to manage failover. More replicas create more states to monitor and do not automatically improve correctness.
Failure and recovery
During a primary failure, a replica may be promoted only after the system determines which node has the authoritative history. Promoting a lagging replica can lose acknowledged asynchronous writes; keeping both old and new primaries writable can create split-brain divergence. Fencing the former primary and controlling leadership are essential. After recovery, the old node should rejoin by following the new history or be rebuilt; it must not simply resume writing from a divergent timeline. Monitor replica receive and apply lag separately, disk capacity, replication slot growth, and promotion health. A replica that is hours behind may still be healthy for backup but not for user reads or fast failover. Test planned failover, forced failure, and rejoin procedures. Recovery objectives must state allowable data loss and restoration time. Replication cannot replace backups, and backups cannot guarantee low-latency failover; use both when requirements demand both.
When not to read from a replica
Do not send every read to a replica simply because it exists. A workflow that must immediately reflect a successful write needs session consistency or a primary read. A replica under heavy analytical queries can become slower than the primary and increase lag. Synchronous replication may be a poor choice when a distant acknowledgement would violate latency goals or when the business accepts a small recovery-point window. Multi-writer replication is not a shortcut for global coordination if updates conflict on the same entities. For small datasets and modest demand, a well-backed-up single database can be simpler and safer. Choose replication to meet a specified availability, read-scale, or recovery requirement, then test its effect on those requirements. Never describe a replica as a backup that protects from accidental deletion, since the destructive change may replicate promptly.
Senior followups
Ask precisely what a successful write acknowledgement guarantees: local log flush, one replica, or a quorum? What is the maximum tolerated lag for failover, and how is it measured? Can the old primary be fenced during a network partition, and who is allowed to promote a replica? How will clients behave when a read-after-write is routed to a lagging copy? Is replication bandwidth sufficient during peak write volume and catch-up, including large transactions? Could schema changes be applied in a compatible order? Do backups have independent retention and restore tests? Consider geographic distance, correlated failure domains, and whether replicas share credentials or control planes. Senior review also tests the operator's runbook: promotion, client routing, rejoin, and reconciliation must be practiced. An architecture is only as safe as its observed acknowledgement semantics and recovery process.
Operational practice
Expose replication lag in time and underlying log or byte positions, and alert on sustained trends rather than one transient sample. Observe primary write latency, replica apply rate, storage growth, and failed replication connections. For read routing, preserve a write session's consistency when needed and offer an explicit fallback to the primary if replica lag exceeds a safe threshold. Keep failover automation conservative: a false promotion can create split-brain, while a delayed promotion extends outage. Rehearse recovery with an isolated environment and verify the actual data-loss window. Record promotion decisions and the point at which writes resume. After failover, check for missing acknowledged operations and reconcile business state using durable logs or external records where available. Maintain independent backups and verify restores. Capacity planning should include the extra network and storage used by replica logs, not just the replica's table size.
Terms in plain language
- Primary
- The current replica authorized to accept a particular class of writes.
- Replica
- A copy maintained from another node's changes, with guarantees depending on the replication mode.
- Replication lag
- The delay or amount of unapplied change between a source and a replica.
- Failover
- A controlled change of service responsibility to another node after failure or planned maintenance.
References and boundaries
Primary documentation supports the mechanisms described. It does not certify these hypothetical architectures, prices or sizing assumptions. Review platform-specific behavior before implementation.
Primary database documentation on streaming replication and standby behavior.
Primary documentation on replication topologies and failover considerations.