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
- 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
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
Follow a request
Pause to pick a request that is on screen.
Each node's copy
Bars show the number of writes applied. A shorter bar means that node would return an older value.
What just happened
- 1.1sWarning: Stale read from N3: returned 0 likes, but 2 were already confirmed.
- 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.
The primary confirms the write at once. Replicas catch up in the background.
The primary waits until every replica has the write.
- 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
- 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.
| Measure | Side 1 · Asynchronous | Side 2 · Synchronous | Gap |
|---|---|---|---|
| Writes sent | 41 | 41 | Same requests |
| Reads sent | 75 | 75 | Same requests |
| Writes confirmed | 40 | 37 | Asynchronous higher by 3 |
| Write latency, p50 (median) | 310 ms | 2.2 s | Asynchronous lower by 1.9 s |
| Reads that returned old data | 59 | 0 | Synchronous lower by 59 |
| Writes that failed | 0 | 0 | Same |
| Confirmed writes lost in a failover | 0 | 0 | Same |
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.