Home›Learn›Many servers 2: who is the leader
Course · Stay upLesson 23 of 330 read

Consensus and leaders

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.

How each system picks a leader

Go deeper
SystemWho decidesDefault timing
PostgreSQL + PatroniA leader key in etcd, Consul or ZooKeeper, with a TTLttl 30 s, loop_wait 10 s
MongoDB replica setThe members vote among themselves (a Raft-based protocol)electionTimeoutMillis 10 s, heartbeats every 2 s
MySQL InnoDB ClusterGroup Replication: members agree on every transaction (Paxos based)A member is expelled after it stops responding
Redis SentinelA quorum of Sentinels agrees the primary is down, then one Sentinel leads the failoverdown-after-milliseconds, 30 s in the example config
CassandraNo leader: any replica takes writes; lightweight transactions use Paxos per partition (one partition key)n/a
etcd, Consul, ZooKeeperRaft or Zab among their own membersTheir 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
Next · chapter 3CAP as settingsThe trade-off is a setting you choose← Chapter 1: Keep a copy running
Connected

Where this shows up

The same mechanism, replication and failover, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 3 questions

Check yourself

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?

2. Two replicas try to take the leader key at the same moment. What happens?

3. A five-node cluster can lose how many nodes and still elect a leader?

One database decision a weekA short email each week: one real decision, the trade-off, and the command to check it. Unsubscribe in one click.

Part 1 of 4 · Learn · 56 pagesAll pages

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.

Start 1: the basicsStart 2: ten conceptsStart 3: how a query travelsStart 4: napkin mathThe courseShort videosPostgreSQL 1: why PostgreSQLPostgreSQL 2: how it stores your dataPostgreSQL 3: what happens on every writePostgreSQL 4: high availability with PatroniPostgreSQL 5: running it as a DBREPostgreSQL 6: when it breaksPostgreSQL on Kubernetes: CloudNativePGMySQL 1: why MySQLMySQL 2: how it stores your dataMySQL 3: what happens on every writeMySQL 4: high availability with InnoDB ClusterMySQL 5: running it as a DBREMySQL 6: when it breaksMySQL failover with OrchestratorMySQL on Kubernetes: the Percona operatorsMongoDB 1: why MongoDBMongoDB 2: how it stores your dataMongoDB 3: what happens on every writeMongoDB 4: high availability with replica setsMongoDB 5: running it as a DBREMongoDB 6: when it breaksMongoDB on Kubernetes: the Percona operatorB-tree and LSM treeRedis 1: why RedisRedis 2: how it stores your dataRedis 3: high availabilityRedis 4: running it as a DBRERedis 5: when it breaksCassandra 1: why CassandraCassandra 2: how it stores your dataCassandra 3: high availabilityCassandra 4: running it as a DBRECassandra 5: when it breaksVector databases 1: why vector searchVector databases 2: how it stores your dataVector databases 3: high availabilityVector databases 4: running it as a DBREVector databases 5: when it breaksDatabase familiesEngines side by sideQueries that look fine and are notLost updates and write skewMany servers 1: keep a copy runningMany servers 2: who is the leaderMany servers 3: CAP as settingsMany servers 4: unique IDsMany servers 5: split the dataMany servers 6: across servicesMany servers 7: a thousand databasesStaff path, zero to architect