LESSON 04 · Copies and failover

Protect the database

Replicas add read capacity and provide a path through database failure.

SYSTEM VIEW

The architecture

Diagram view
Replicas—
Missing at failover—
Taking writes—

Watch writes go to one primary and reads to replicas, then see what lag and failover cost.

  1. In a primary/replica arrangement, every change goes to the primary, and copies called replicas apply it and serve many reads. Replication doesn’t move writes off the primary, so a write bottleneck needs a different remedy.

    What changed

    • Every write goes to the primary
    • 2 replicas copy it and serve reads
  2. A user renames their profile. Replication here is asynchronous, the Postgres default: the primary confirms first and ships the change afterward, so replicas lag. Replica A is two seconds behind.

    What changed

    • Rename confirmed by the primary
    • Replica A: 2 s lag
  3. The user reloads, and the read lands on replica A. It hasn’t applied the rename yet, so they see the old name.

    What changed

    • Read from replica A
    • It returns the old name
  4. A common fix routes a user’s reads to the primary for a few seconds after their own write. The reload now shows the new name.

    What changed

    • Reads after a write go to the primary
    • The new name shows
  5. The primary stops responding, so replica A is promoted and writes are redirected to it. At 500 writes a second, two seconds of lag means up to 1,000 confirmed writes may be missing. Synchronous replication loses none if A survives, but every commit pays an extra round trip.

    What changed

    • Primary unreachable
    • Replica A promoted
    • Writes go to A
    Replication
  6. If the old primary was only cut off, not dead, it still acts as primary when the network heals, and web servers that weren’t redirected keep writing to it. That’s split brain: two nodes take writes, and their copies diverge.

    What changed

    • Network heals; old primary still takes writes
    • The two copies diverge
  7. The old node must be fenced, meaning blocked from accepting writes. Tools such as Patroni use a majority-based store like etcd to allow one primary at a time. Failover is an operational process: plan for missing writes and split brain.

    What changed

    • Old primary fenced
    • One node takes writes

Space plays · ← → step · Home resets

THE IDEA

How it works

In a primary/replica arrangement, changes go to the primary, and copies called replicas apply them and serve many reads. If one read replica fails, reads can move to another or to the primary. Replication does not distribute writes away from the primary, so a write bottleneck needs a different remedy.

Replication timing is a choice. With synchronous replication, the primary waits for a replica to confirm a change before telling the client it succeeded. No confirmed write is lost if that replica survives, but every commit pays for the extra round trip, and a stalled replica can stall writes. With asynchronous replication, the primary confirms first and ships the change afterward: faster, but replicas lag behind. Postgres defaults to asynchronous and can be configured for synchronous commits.

Lag makes replica reads stale. If someone renames their profile and the next page reads from a replica two seconds behind, they see the old name. A common fix routes a user’s reads to the primary for a few seconds after their own write. Lag also sets the cost of losing the primary: at 500 writes per second, two seconds of lag means up to 1,000 confirmed writes may be missing from the replica you promote.

Promotion also means redirecting clients and preventing split brain, where an isolated old primary keeps accepting writes alongside the new one and the copies diverge. Tools such as Patroni use a majority-based store like etcd to allow one primary at a time, and the old node must be fenced, meaning blocked from accepting writes.

DECISION POINT

Monitoring loses contact with the primary and promotes a replica. Ten minutes later the network heals, and some web servers are still writing to the old primary.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Replicas help with reads and availability, but their reads can be stale.
  • Synchronous replication trades write speed for fewer lost writes; asynchronous does the reverse.
  • Failover is an operational process: plan for missing writes and split brain.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

A user saves a new display name, reloads, and sees the old one. Seconds later the new name appears. Reads go to asynchronous replicas. What is the likely cause?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.