Skip to content
System design · Chapter 16 of 21 Day 14: Consistency and CAP by Example

Day 14: Consistency and CAP by Example

Goal: The primary fails, and kurz should still be able to write. You put a virtual IP managed by keepalived in front of the database, promote the replica, watch the old primary come back and two databases accept writes at the same time, and clean up the split brain by hand. In the Challenge, consistency costs availability · Duration: approx. 90 min · Prerequisite: Day 13

Briefing

  • Since Day 8, s3 has had a replica on s2. Reads already go there. Writes go only to s3, and if s3 fails, nobody can create links anymore. A failover turns the replica into the new primary.
  • So that kurz doesn’t have to be reconfigured, it writes to an address that always sits with the current primary: a virtual IP (VIP), here 172.16.0.11. keepalived on s2 and s3 negotiates via VRRP who holds it.
  • The CAP theorem says: when the network is split (partition), a distributed system cannot be consistent (everyone sees the same data) and available (every node accepts writes) at the same time. Today you see both sides: a system that would rather not write, and one that writes on both sides and afterwards no longer knows what’s true.
Term Meaning
VRRP protocol several servers use to assign an IP among themselves
promote a replica becomes primary and accepts writes
split brain two servers consider themselves primary, both accept writes
fencing reliably keeping the old primary from writing
RPO how much data a failover may lose at most
synchronous / asynchronous commit waits for the replica / commit doesn’t wait

The VIP 172.16.0.11 lies outside the DHCP range of your network. By the end of the day, s3 is primary again, s2 is the replica, and keepalived is off again.

Drill 1: One IP for the Primary

First check that nobody is using the address already:

# on s1
mkdir -p ~/sd/tag14 && cd ~/sd/tag14 && cp ~/sd/tag13/anlegen.js ~/sd/tag13/klick.js ~/sd/tag13/codes.txt .
sudo apt-get install -y iputils-arping
sudo arping -c 3 -w 4 172.16.0.11

Expected (shortened):

ARPING 172.16.0.11 from 172.16.0.126 eth0
Sent 3 probes (3 broadcast(s))
Received 0 response(s)

keepalived should give the VIP only to the server that actually runs PostgreSQL as primary. Two checks per second: does PostgreSQL accept connections, and is the file standby.signal missing, which every replica has? The two servers talk to each other via unicast instead of multicasting into your whole home network. On s3, with priority 150:

# on s3
sudo apt-get install -y keepalived
keepalived --version 2>&1 | head -1
sudo tee /etc/keepalived/keepalived.conf >/dev/null <<'EOF'
global_defs {
    enable_script_security
    script_user root
}

# PostgreSQL nimmt Verbindungen an
vrrp_script laeuft {
    script "/usr/lib/postgresql/18/bin/pg_isready -q -h 127.0.0.1"
    interval 1
    fall 2
    rise 2
}

# kein Replikat: ohne standby.signal ist dieser Server Primär
vrrp_script primaer {
    script "/usr/bin/test ! -e /var/lib/postgresql/18/main/standby.signal"
    interval 1
    fall 2
    rise 2
}

vrrp_instance DB {
    state BACKUP
    interface eth0
    virtual_router_id 11
    priority 150
    advert_int 1
    unicast_src_ip 172.16.0.83
    unicast_peer {
        172.16.0.228
    }
    virtual_ipaddress {
        172.16.0.11/24 dev eth0
    }
    track_script {
        laeuft
        primaer
    }
}
EOF
sudo keepalived -t -f /etc/keepalived/keepalived.conf && echo "Konfiguration ok"

Expected:

Keepalived v2.2.8 (04/04,2023), git commit v2.2.7-154-g292b299e+
Konfiguration ok

On s2, the same file with priority 100, unicast_src_ip 172.16.0.228 and 172.16.0.83 as peer. Then start it on both servers and check who holds the address:

# on s1
for s in s3 s2; do ssh $s "sudo systemctl enable -q keepalived && sudo systemctl restart keepalived"; done
sleep 6
for s in s3 s2; do ssh $s 'hostname; ip -br -4 a show eth0; sudo journalctl -u keepalived --since "-20s" -o cat | grep -E "VRRP_Script|Entering" | tail -4'; done
sudo arping -c 2 -w 3 172.16.0.11

Expected (your MAC address will differ):

s3
eth0             UP             172.16.0.83/24 metric 100 172.16.0.11/24
VRRP_Script(primaer) succeeded
VRRP_Script(laeuft) succeeded
(DB) Entering BACKUP STATE
(DB) Entering MASTER STATE
s2
eth0             UP             172.16.0.228/24 metric 100
VRRP_Script(primaer) failed (exited with status 1)
VRRP_Script(laeuft) succeeded
ARPING 172.16.0.11 from 172.16.0.126 eth0
Unicast reply from 172.16.0.11 [BC:24:11:79:DE:21]  1.402ms
Unicast reply from 172.16.0.11 [BC:24:11:79:DE:21]  1.164ms
Sent 2 probes (1 broadcast(s))
Received 2 response(s)

s3 holds the VIP. s2 is a replica, its primaer check fails, so it doesn’t even compete.

On the first attempt, laeuft used the path /usr/bin/pg_isready. It worked in the terminal, but under keepalived it reported failed (exited with status 1), and nobody got the VIP. On Ubuntu, /usr/bin/pg_isready is not a program but the postgresql-common wrapper, which first looks for the matching version. The path to the version 18 program works everywhere.

Save your progress? Sign up for free

Drill 2: kurz Writes to the VIP

So far, kurz writes via PgBouncer on s3. But PgBouncer runs only there and would be gone too if s3 failed. For today, kurz connects directly to PostgreSQL behind the VIP, with 16 connections per instance, well below max_connections:

# on s1
export PGPASSWORD=lab-kurz-123
psql -h 172.16.0.11 -U kurz -d kurz -tAc "select inet_server_addr(), pg_is_in_recovery()"
for s in s1 s2; do
  ssh $s "printf '[Service]\nEnvironment=KURZ_DB=postgres://kurz:[email protected]:5432/kurz?pool_max_conns=16\n' \
    | sudo tee /etc/systemd/system/kurz.service.d/pool.conf >/dev/null && sudo systemctl daemon-reload"
  echo "set server kurz/$s state drain" | sudo nc -U /run/haproxy/admin.sock >/dev/null; sleep 2
  ssh $s "sudo systemctl restart kurz"
  until curl -sf http://$s:8080/readyz >/dev/null; do sleep 0.5; done
  echo "set server kurz/$s state ready" | sudo nc -U /run/haproxy/admin.sock >/dev/null
done
curl -s -o /dev/null -w "%{http_code}\n" -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/ueber-vip-$(date +%s)\"}"
psql -h s3 -U kurz -d kurz -tAc "select client_addr, count(*) from pg_stat_activity where usename = 'kurz' and client_addr is not null group by 1 order by 1"

Expected:

172.16.0.11|f
201
172.16.0.126|2
172.16.0.228|1

The connections now arrive directly from s1 and s2, no longer via PgBouncer. Reads stay with the replica on s2 (KURZ_DB_LESEN), and zaehler keeps writing via PgBouncer on s3. Both will matter in a moment.

Save your progress? Sign up for free

Drill 3: The Primary Fails

A ticker writes a link every half second and logs the status; alongside, 200 redirects per second are running. Then you stop PostgreSQL on s3. The 12-second pause simulates how long it takes until someone promotes the replica:

# on s1
cd ~/sd/tag14
while true; do echo "$(date +%T) $(curl -s -o /dev/null -m 3 -w '%{http_code}' -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/takt-$(date +%s%N)\"}")"; sleep 0.5; done > takt.log 2>&1 &
TAKT=$!
k6 run -q -e RATE=200 -e DAUER=50s klick.js > klick.log 2>&1 &
sleep 8; ssh s3 "sudo systemctl stop postgresql"; echo "$(date +%T) PostgreSQL auf s3 gestoppt"
sleep 3; ssh s3 "ip -br -4 a show eth0"; ssh s2 "ip -br -4 a show eth0"
sleep 12; ssh s2 "sudo -u postgres psql -tAc 'select pg_promote()'"; echo "$(date +%T) s2 befördert"
sleep 5; ssh s2 "ip -br -4 a show eth0"
wait %2 2>/dev/null; sleep 20; kill $TAKT
awk '{print $2}' takt.log | uniq -c
awk 'prev != $2 {print} {prev = $2}' takt.log
grep -E 'checks_succeeded' klick.log

Expected:

21:00:00 PostgreSQL auf s3 gestoppt
eth0             UP             172.16.0.83/24 metric 100
eth0             UP             172.16.0.228/24 metric 100
t
21:00:20 s2 befördert
eth0             UP             172.16.0.228/24 metric 100 172.16.0.11/24
     19 201
     14 503
      4 201
      1 503
     58 201
20:59:49 201
21:00:00 503
21:00:24 201
21:00:27 503
21:00:28 201
    checks_succeeded...: 99.69% 9970 out of 10001

Three seconds after the stop, nobody holds the VIP: s3 fails laeuft, s2 fails primaer. That is intentional. keepalived only moves the address; a human decides the role. Only after pg_promote() does standby.signal disappear on s2, its checks succeed, and it takes over the VIP. Four seconds later, the first create goes through. The single 503 three seconds after that still belongs to this transition period; after that, everything runs.

For 24 seconds nobody could write, 20 of which the human needed. The redirects kept running almost undisturbed: cache and replica don’t need the primary.

Save your progress? Sign up for free

Drill 4: The Old Primary Comes Back

Someone starts PostgreSQL on s3 again, say after a reboot of the machine. s3 knows nothing about the failover: as far as it’s concerned, it’s still primary.

# on s1
ssh s3 "sudo systemctl start postgresql"; echo "$(date +%T) PostgreSQL auf s3 wieder gestartet"
sleep 6
for s in s2 s3; do ssh $s "hostname; ip -br -4 a show eth0"; done
for i in 1 2 3; do curl -s -o /dev/null -w "%{http_code}\n" -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/gespalten-$i-$(date +%s)\"}"; sleep 1; done
for s in s2 s3; do echo "== $s"; psql -h $s -U kurz -d kurz -tAc "select pg_is_in_recovery(), count(*) filter (where url like 'https://example.org/tag14/takt-%' and angelegt > '2026-11-23 21:00:20+00') as nach_failover, count(*) filter (where url like 'https://example.org/tag14/gespalten-%') as gespalten from links"; done

Expected:

21:01:18 PostgreSQL auf s3 wieder gestartet
s2
eth0             UP             172.16.0.228/24 metric 100
s3
eth0             UP             172.16.0.83/24 metric 100 172.16.0.11/24
201
201
201
== s2
f|62|0
== s3
f|0|3

Two primaries (f means: not in recovery). Both checks on s3 succeed and it has the higher priority, so it takes the VIP back. It doesn’t know the 62 links created on s2 after the failover. The three new links exist only on s3. Anyone creating one of the 62 URLs again would get a second code: sucheURL asks s3 via the VIP.

It gets worse. zaehler writes via PgBouncer on s3, bypassing the VIP:

# on s1
for s in s2 s3; do echo "== $s"; psql -h $s -U kurz -d kurz -tAc "select sum(anzahl) from klicks"; done

Expected:

== s2
142368
== s3
150401

While s3 was down, zaehler couldn’t write, and JetStream collected the clicks (Day 10). As soon as s3 was back, zaehler worked through the backlog and acknowledged it, on the wrong server. The 8,033 clicks exist only on s3. That is a split brain: both sides are right, and neither has everything.

Save your progress? Sign up for free

Drill 5: Cleaning Up After the Split Brain

Fence first: s3 must not accept anything anymore. Stop keepalived on s3 (the VIP goes back to s2) and pause zaehler until the data is merged. The clicks wait in JetStream in the meantime.

# on s1
ssh s3 "sudo systemctl stop keepalived"; ssh s2 "sudo systemctl stop zaehler"
sleep 4
for s in s2 s3; do ssh $s "hostname; ip -br -4 a show eth0"; done

Expected:

s2
eth0             UP             172.16.0.228/24 metric 100 172.16.0.11/24
s3
eth0             UP             172.16.0.83/24 metric 100

Then decide who wins. s2 has the longer history since the failover, so s2 wins, and whatever exists only on s3 gets carried over: links since the outage and the click difference per code.

# on s1
psql -h s3 -U kurz -d kurz -c "\copy (select code, url, angelegt from links where angelegt > '2026-11-23 21:00:00+00') to 'nur-s3-links.csv' csv"
psql -h s3 -U kurz -d kurz -c "\copy klicks to 'klicks-s3.csv' csv"
psql -h s2 -U kurz -d kurz <<'EOF'
\copy links (code, url, angelegt) from 'nur-s3-links.csv' csv
CREATE TEMP TABLE k3 (code text, anzahl bigint);
\copy k3 from 'klicks-s3.csv' csv
INSERT INTO klicks SELECT k3.code, k3.anzahl - coalesce(k.anzahl, 0)
  FROM k3 LEFT JOIN klicks k USING (code) WHERE k3.anzahl > coalesce(k.anzahl, 0)
  ON CONFLICT (code) DO UPDATE SET anzahl = klicks.anzahl + excluded.anzahl;
EOF

Expected:

COPY 4
COPY 1003
ERROR:  duplicate key value violates unique constraint "links_pkey"
DETAIL:  Key (code)=(NWJbhZg) already exists.
CONTEXT:  COPY links, line 1
CREATE TABLE
COPY 1003
INSERT 0 975

The clicks were carried over, the links weren’t: one of the four was written in the last second before the outage and had still reached s2. A reconciliation has to expect overlaps. So go through a staging table and ON CONFLICT DO NOTHING:

# on s1
psql -h s2 -U kurz -d kurz <<'EOF'
CREATE TEMP TABLE l3 (code text, url text, angelegt timestamptz);
\copy l3 from 'nur-s3-links.csv' csv
INSERT INTO links SELECT * FROM l3 ON CONFLICT DO NOTHING;
EOF
for s in s2 s3; do echo "== $s"; psql -h $s -U kurz -d kurz -tAc "select (select count(*) from links where url like 'https://example.org/tag14/%'), (select sum(anzahl) from klicks)"; done

Expected:

CREATE TABLE
COPY 4
INSERT 0 3
== s2
85|150401
== s3
23|150401

s2 now has everything. This worked here because links have unique codes and clicks are just counters. Had both sides changed the same record differently, someone would have to decide which change counts.

s3 comes back only as a replica. For that, s2 needs permission for replication connections from the network and a slot for s3. On s3, the old data directory is moved aside and copied fresh from s2:

# on s1
ssh s2 "echo 'host  replication  replikat  172.16.0.0/24  scram-sha-256' | sudo tee -a /etc/postgresql/18/main/pg_hba.conf >/dev/null && sudo systemctl reload postgresql && sudo -u postgres psql -tAc \"select pg_create_physical_replication_slot('s3')\""
ssh s3 "sudo systemctl stop postgresql && sudo mv /var/lib/postgresql/18/main /var/lib/postgresql/18/main.gespalten"
time ssh s3 "sudo -u postgres env PGPASSWORD=lab-repl-123 pg_basebackup -h s2 -U replikat -D /var/lib/postgresql/18/main -R -S s3 -X stream"
ssh s3 "sudo systemctl start postgresql && sleep 2 && sudo -u postgres psql -tAc 'select pg_is_in_recovery()' && sudo systemctl start keepalived"
sleep 4
ssh s2 "sudo -u postgres psql -tAc 'select application_name, client_addr, state, sync_state from pg_stat_replication'"
for s in s2 s3; do ssh $s "hostname; ip -br -4 a show eth0"; done

Expected:

(s3,)

real    4m36.844s
user    0m0.345s
sys 0m0.014s
t
18/main|172.16.0.83|streaming|async
s2
eth0             UP             172.16.0.228/24 metric 100 172.16.0.11/24
s3
eth0             UP             172.16.0.83/24 metric 100

Four and a half minutes for 714 MB; on Day 8 it was four seconds for 400 MB. The reason: pg_basebackup waits for a checkpoint, and without -c fast that’s the next regular one, spread over minutes. That’s why the switch-back in the Clean-up includes -c fast. s3 is now a replica of s2, its primaer check fails, and keepalived on s3 is running again without wanting the VIP.

Save your progress? Sign up for free

Exercise: Speed drills

  1. Who holds the VIP right now? Target time: 30 s
  2. Who is primary, who replicates from it, synchronously or asynchronously? Target time: 1 min

Solution

# on s1
for s in s2 s3; do ssh $s "hostname; ip -br -4 a show eth0"; done
ssh s2 "sudo -u postgres psql -tAc 'select application_name, client_addr, state, sync_state from pg_stat_replication'"

Expected (state after Drill 5):

s2
eth0             UP             172.16.0.228/24 metric 100 172.16.0.11/24
s3
eth0             UP             172.16.0.83/24 metric 100
18/main|172.16.0.83|streaming|async

pg_stat_replication exists only on the primary. If it shows a row, you know both: who is primary and who follows it.

Save your progress? Sign up for free

Checkpoint

# on s1
ssh s3 "sudo -u postgres psql -tAc 'select pg_is_in_recovery()'"

Expected: t, s3 is a replica.

# on s1
psql -h s2 -U kurz -d kurz -tAc "select (select count(*) from links where url like 'https://example.org/tag14/%'), (select sum(anzahl) from klicks)"

Expected: the same numbers as at the end of Drill 5, in my case 85|150401.

# on s1
ssh s2 "sudo -u postgres psql -tAc 'select state from pg_stat_replication'"

Expected: streaming

Challenge

Asynchronous replication can lose the last commits during a failover: whatever s2 hasn’t received yet is gone. Make replication synchronous and then cut the connection between s2 and s3. What happens to link creation, and what is in the database afterwards?

Hint: synchronous_standby_names = '*' on the primary. The replication data flows from port 5432 of the primary to the replica.

Solution

# on s1
ssh s2 "sudo -u postgres psql -qc \"ALTER SYSTEM SET synchronous_standby_names = '*'\" -c 'select pg_reload_conf()' >/dev/null && sudo -u postgres psql -tAc 'select application_name, sync_state from pg_stat_replication'"
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/sync-1-$(date +%s)\"}"
ssh s3 "sudo iptables -I INPUT -s 172.16.0.228 -p tcp --sport 5432 -j DROP"
U="https://example.org/tag14/sync-2-$(date +%s)"
curl -s -w " %{http_code} %{time_total}s\n" -X POST localhost/api/links -d "{\"url\":\"$U\"}"
psql -h s2 -U kurz -d kurz -tAc "select count(*) from links where url = '$U'"
ssh s2 "sudo tail -n 20 /var/log/postgresql/postgresql-18-main.log | grep -iE 'synchron' | tail -2"
ssh s3 "sudo iptables -D INPUT -s 172.16.0.228 -p tcp --sport 5432 -j DROP"
ssh s2 "sudo -u postgres psql -qc 'ALTER SYSTEM RESET synchronous_standby_names' -c 'select pg_reload_conf()' >/dev/null && sudo -u postgres psql -tAc 'show synchronous_standby_names'"
sleep 3
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/sync-3-$(date +%s)\"}"

Expected:

18/main|sync
201 0.053016s
Datenbankfehler
 503 2.004228s
1
2026-11-23 21:08:16.104 UTC [29864] replikat@[unknown] LOG:  standby "18/main" is now a synchronous standby with priority 1
2026-11-23 21:08:19.714 UTC [30165] kurz@kurz WARNING:  canceling wait for synchronous replication due to user request

201 0.043008s

In normal operation, synchronous costs hardly anything (53 ms for the first create). With the connection cut, every commit waits for a replica that never answers, until kurz gives up after 2 seconds. The system chooses consistency and loses availability: nobody can write.

And yet the row is there. PostgreSQL committed locally and only waited for the acknowledgment. The cancellation doesn’t roll back the commit, as the warning in the log says. The client got 503, the data is in the database, but not yet on the replica. So synchronous means: no loss on failover, but a timeout means “maybe stored”. The idempotency key from Day 11 makes the retry safe. After the reset, kurz writes in 43 ms again.

Save your progress? Sign up for free

Clean-up

Back to the state of Day 13: s3 primary, s2 replica, kurz via PgBouncer, keepalived off. Switching back is a planned failover: stop s2 cleanly (it sends the last WAL data to s3 while doing so), promote s3, copy s2 fresh from s3. The max_slot_wal_keep_size setting from Day 8 lived in s3’s data directory and disappeared with the rebuild, so set it again.

# on s1
ssh s2 "sudo systemctl stop postgresql"; echo "$(date +%T) s2 gestoppt"
ssh s3 "sudo -u postgres psql -tAc 'select pg_promote()'"; echo "$(date +%T) s3 befördert"
sleep 4
for s in s2 s3; do ssh $s "hostname; ip -br -4 a show eth0"; done
ssh s3 "sudo -u postgres psql -qc \"ALTER SYSTEM SET max_slot_wal_keep_size = '2GB'\" -c 'select pg_reload_conf()' -c \"select pg_create_physical_replication_slot('s2')\" >/dev/null && echo slot s2 angelegt"
ssh s2 "sudo mv /var/lib/postgresql/18/main /var/lib/postgresql/18/main.alt"
time ssh s2 "sudo -u postgres env PGPASSWORD=lab-repl-123 pg_basebackup -h s3 -U replikat -D /var/lib/postgresql/18/main -R -S s2 -X stream -c fast"
ssh s2 "sudo systemctl start postgresql && sleep 2 && sudo -u postgres psql -tAc 'select pg_is_in_recovery()'"
ssh s3 "sudo -u postgres psql -tAc 'select application_name, client_addr, state from pg_stat_replication'"

Expected:

21:08:43 s2 gestoppt
t
21:08:46 s3 befördert
s2
eth0             UP             172.16.0.228/24 metric 100
s3
eth0             UP             172.16.0.83/24 metric 100 172.16.0.11/24
slot s2 angelegt

real    0m5.634s
user    0m0.394s
sys 0m0.016s
t
18/main|172.16.0.228|streaming

With -c fast, 5.6 seconds instead of four and a half minutes. Now kurz back to PgBouncer, keepalived off, zaehler on again, old data directories gone:

# on s1
for s in s1 s2; do
  ssh $s "printf '[Service]\nEnvironment=KURZ_DB=postgres://kurz:lab-kurz-123@s3:6432/kurz?pool_max_conns=16\n' \
    | sudo tee /etc/systemd/system/kurz.service.d/pool.conf >/dev/null && sudo systemctl daemon-reload"
  echo "set server kurz/$s state drain" | sudo nc -U /run/haproxy/admin.sock >/dev/null; sleep 2
  ssh $s "sudo systemctl restart kurz"
  until curl -sf http://$s:8080/readyz >/dev/null; do sleep 0.5; done
  echo "set server kurz/$s state ready" | sudo nc -U /run/haproxy/admin.sock >/dev/null
done
for s in s2 s3; do ssh $s "sudo systemctl disable -q --now keepalived"; done
sleep 2
sudo arping -c 3 -w 4 172.16.0.11 | tail -1
ssh s2 "sudo systemctl start zaehler"
sleep 5
until [ "$(nats consumer info KLICKS zaehler --json | jq '.num_pending + .num_ack_pending')" = 0 ]; do sleep 1; done
curl -s -o /dev/null -w "%{http_code}\n" -X POST localhost/api/links -d "{\"url\":\"https://example.org/tag14/danach-$(date +%s)\"}"
psql -h s3 -U kurz -d kurz -tAc "select pg_is_in_recovery(), (select sum(anzahl) from klicks)"
psql -h s2 -U kurz -d kurz -tAc "select pg_is_in_recovery(), (select sum(anzahl) from klicks)"
ssh s3 "sudo rm -rf /var/lib/postgresql/18/main.gespalten"; ssh s2 "sudo rm -rf /var/lib/postgresql/18/main.alt"
ssh s3 "sudo -u postgres psql -tAc 'show max_slot_wal_keep_size'"

Expected:

Received 0 response(s)
201
f|150401
t|150401
2GB

Nobody answers on 172.16.0.11 anymore, s3 is primary, s2 replica, both with the same clicks. keepalived is installed but disabled. The permission for replication from the network in s2’s pg_hba.conf stays; it does no harm and saves a step next time.

Interview note

  • Use CAP correctly: it’s about the partition case. Then the choice is: reject writes (consistent, CP) or accept them on both sides and merge later (available, AP). Without a partition, PACELC fits better: latency versus consistency, like synchronous versus asynchronous.
  • Failover needs three things: detection (health check), decision (who becomes primary, and exactly one) and redirection (VIP, DNS, proxy). The decision is the hard part.
  • Preventing split brain: fence the old primary before the new one writes (power off, network closed, credentials locked), majority vote with at least three nodes, and nobody may write bypassing the VIP.

Enterprise note

  • Patroni (with etcd or Consul) and CloudNativePG in the Kubernetes world automate exactly these steps: majority vote on the primary, promotion, fencing, rebuild with pg_rewind. keepalived alone decides nothing; it only moves addresses.
  • RPO and RTO are part of every operations concept: how much data a failover may lose and how long it may take. Today: RTO 24 seconds with a human, RPO with asynchronous replication “whatever hadn’t arrived yet”, with synchronous zero.
  • Databases that are deliberately AP (Cassandra, DynamoDB) resolve conflicts with rules such as “last write wins” or with data types that merge automatically (CRDTs). The counter reconciliation in Drill 5 was such a case, just by hand.

Contents

More