After a failover, how does everyone agree on the new primary?
One failover with Patroni and etcd, animated, and the same idea in five other systems.
Press Play, or step with the arrows.
High availability needs one decision made reliably: which node is the primary now. If two nodes both believe it, both take writes and the data splits. That is the problem consensus solves.
| System | Who decides | Default timing |
|---|---|---|
| PostgreSQL + Patroni | A leader key in etcd, Consul or ZooKeeper, with a TTL | ttl 30 s, loop_wait 10 s |
| MongoDB replica set | The members vote among themselves (a Raft-based protocol) | electionTimeoutMillis 10 s, heartbeats every 2 s |
| MySQL InnoDB Cluster | Group Replication: members agree on every transaction (Paxos based) | A member is expelled after it stops responding |
| Redis Sentinel | A quorum of Sentinels agrees the primary is down, then one Sentinel leads the failover | down-after-milliseconds, 30 s in the example config |
| Cassandra | No leader: any replica takes writes; lightweight transactions use Paxos per partition (one partition key) | n/a |
| etcd, Consul, ZooKeeper | Raft or Zab among their own members | Their own election timeouts |
Two rules hold everywhere. A leader needs a majority, so three nodes survive one failure and five survive two. And a leader that loses the majority must stop acting as leader on its own, because the others cannot stop it. That step, self-demotion when the lease cannot be renewed, is what prevents split brain. True fencing, a token or STONITH that blocks the old leader's writes, is a separate, stronger mechanism.
patronictl -c /etc/patroni.yml list # Member Host Role State TL Lag in MB # pg2 ... Leader running 2 # pg1 ... Replica streaming 2 0
// MongoDB: who is primary, and the election term rs.status().members.map(m => [m.name, m.stateStr]) rs.status().term
The same mechanism, replication and failover, elsewhere on the site: in real incidents, in measurements, and in designs.
Pick an answer. You will see whether it is right, and why.
1. Why does Patroni wait for the leader key to expire before promoting?
The lease is the only safe signal that the old primary is no longer allowed to lead.
2. Two replicas try to take the leader key at the same moment. What happens?
Raft gives one order of operations, so the create succeeds once.
3. A five-node cluster can lose how many nodes and still elect a leader?
Three of five is a majority.
How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.