Reading preferences
Concept

Database Replication

Copy database changes to replicas while accounting for lag and failover.

system design, replication, consistency

A copy for another purpose

A primary database accepts writes; replicas receive copies of changes. Reads can be distributed to replicas when their consistency and capacity fit the request. With asynchronous replication a committed primary write can appear later on a replica, so a replica is not necessarily current.

Reporting: lag is visible

  1. The primary commits booking 42.
  2. A reporting replica has not applied it yet, so its report omits booking 42.
  3. After the change arrives and is applied, the next report includes it. Report consumers need a freshness expectation and lag monitoring.

See the reasoning

Watch replica lag

Compare the primary with a reporting replica. This is a prepared demonstration, not live code execution.

Commit on primary

Booking 42
  1. PrimaryBooking 42 committedCurrent focus
  2. ReplicaBooking 42 not applied

An asynchronous replica has not received or applied the change yet.

Booking service: failover

If a primary becomes unavailable, an eligible replica can be promoted and clients redirected. Promotion needs coordination and fencing of the former primary to avoid conflicting writers. With asynchronous replication, writes not yet present on the promoted replica can be lost; synchronous policies may reduce that window but can increase latency or reduce write availability. Measure recovery time and acceptable data loss, and rehearse failover.

Trade-offs

Replicas add infrastructure, lag monitoring and routing decisions. Send read-after-write or other freshness-sensitive reads to a suitably consistent source. Replication is not backup: deletion and corruption can propagate, so independently retained, tested backups still matter.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is a read replica in this example?
2. Why might a report miss a recent write?
3. What must a safe failover prevent?
4. Is a replica automatically a backup?

References and further reading