Lab · Data

Replication

One primary database takes every write and copies it to two replicas. Replicas spread the read load and survive failures — but copying takes time. Explore stale reads, the latency cost of waiting for replicas, and what failover can lose.

Try it

Lab time 3.0 s
App (you), 1 writes/s · 2 reads/sApp (you)1 writes/s · 2 reads/sconfirmed likes: 5N1 · Primary, takes every write, PRIMARYN1 · Primarytakes every writelikes = 5PRIMARYN2 · Replica, lag 600 msN2 · Replicalag 600 mslikes = 3 · 2 behindN3 · Replica, lag 1.8 sN3 · Replicalag 1.8 slikes = 2 · 3 behind
About each part:
  • App (you) — 1 writes/s · 2 reads/s — healthy — confirmed likes: 5
  • N1 · Primary — takes every write — healthy — PRIMARY — likes = 5
  • N2 · Replica — lag 600 ms — healthy — likes = 3 · 2 behind
  • N3 · Replica — lag 1.8 s — healthy — likes = 2 · 3 behind
  • Request/response
  • Replication link
  • Write
  • Read
  • Copied change
  • Acknowledgement
  • Response
  • Stale data
  • Error / rejected
Confirmed writes
5
Write latency,
310 ms
Waiting for replicas
0
Stale reads
1
Problem: 33% of 3 reads
Went back in time
0
older than a version this reader already saw
Lost writes
0
after failover
Failed writes
0

Numbers add up:every replica's log is a prefix of the primary's (primary at offset 5); confirmed ≤ writtenThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Writes
Replication mode

The primary confirms the write at once. Replicas catch up in the background.

1/s
Reads
Read routing

Spreads reads, but a lagging replica returns old data.

2/s
Network & failures
600 ms
1.8 s

Click a database node to crash or recover it.

Each node's copy

  • N1 (primary)5
  • N23
  • N32

Bars show the number of writes applied. A shorter bar means that node would return an older value.

What just happened

  1. 1.1sWarning: Stale read from N3: returned 0 likes, but 2 were already confirmed.
  2. 0.0sInfo: N1 is the primary. N2 and N3 replicate from it.

Compare two replication modes on the same requests

Both sides get the same writes and reads at the same moments, and both send reads to the replicas (N2 lags 0.6 s, N3 lags 1.8 s). Only the replication mode differs, so any gap comes from the mode. Both sides start with 20 seconds already run.

Side 1 replication mode

The primary confirms the write at once. Replicas catch up in the background.

Side 2 replication mode

The primary waits until every replica has the write.

2/s
Lab time 20.0 s
Side 1 Asynchronous
App, 2 writes/s · 4 reads/sApp2 writes/s · 4 reads/sN1 · Primary, takes every writeN1 · Primarytakes every writelikes = 40N2 · Replica, lag 600 msN2 · Replicalag 600 mslikes = 39 · 1 behindN3 · Replica, lag 1.8 sN3 · Replicalag 1.8 slikes = 37 · 3 behind
  • App — 2 writes/s · 4 reads/s — healthy
  • N1 · Primary — takes every write — healthy — likes = 40
  • N2 · Replica — lag 600 ms — healthy — likes = 39 · 1 behind
  • N3 · Replica — lag 1.8 s — healthy — likes = 37 · 3 behind
Side 2 Synchronous
App, 2 writes/s · 4 reads/sApp2 writes/s · 4 reads/sN1 · Primary, takes every writeN1 · Primarytakes every writelikes = 40N2 · Replica, lag 600 msN2 · Replicalag 600 mslikes = 39 · 1 behindN3 · Replica, lag 1.8 sN3 · Replicalag 1.8 slikes = 37 · 3 behind
  • App — 2 writes/s · 4 reads/s — healthy
  • N1 · Primary — takes every write — healthy — likes = 40
  • N2 · Replica — lag 600 ms — healthy — likes = 39 · 1 behind
  • N3 · Replica — lag 1.8 s — healthy — likes = 37 · 3 behind
  • Request/response
  • Replication link
  • Write
  • Read
  • Copied change
  • Acknowledgement
  • Response
  • Stale data
  • Error / rejected

Each box is one write or read: w12 is the 12th write the app sent, and v12 is the version it became on the primary. Each side draws only a few at a time. The table counts every request.

Compare two replication modes on the same requests: the numbers for each side and the gap between them
MeasureSide 1 · AsynchronousSide 2 · SynchronousGap
Writes sent4141Same requests
Reads sent7575Same requests
Writes confirmed4037Asynchronous higher by 3
Write latency, p50 (median)310 ms2.2 sAsynchronous lower by 1.9 s
Reads that returned old data590Synchronous lower by 59
Writes that failed00Same
Confirmed writes lost in a failover00Same

Changing a setting starts both sides again, so the numbers always describe one setup from its start. After a crash, a replica is promoted about 2.5 s later; writes in that gap fail. Synchronous mode then waits for every replica, the crashed one included, so its new writes wait. A write is lost only if it was confirmed but had not reached the replica that was promoted. Reads: 4 a second on both sides.

What this model simplifies
  • The stored value is a simple counter so staleness is easy to see. Real replication ships row changes (or WAL records), but the timing behaves the same way.
  • Lag is a fixed delay per replica. Real lag varies with load, long transactions and network hiccups — it can spike from milliseconds to minutes.
  • Failover promotes the most up-to-date live replica after a fixed 2.5 s detection time. Real systems use health checks, consensus (e.g. Raft) or an operator to decide, and must fence the old primary so it cannot keep accepting writes.
  • Semi-synchronous mode here never falls back to async. MySQL, for example, falls back after a timeout — trading safety for availability.