replication and consensus
leaders, terms and a replicated log. raft in pictures, and why it needs a majority.
- address
- everything.scale.consensus
- part
- part 17, scale and distribution
- chapter
- chapter 9 of 10
- reading time
- 8 min
consensus gets a group of servers to agree on one sequence of commands even when some of them crash. raft does it with a leader elected for a numbered term, a replicated log, and one rule underneath everything: nothing counts without a majority. any two majorities share a server, so two leaders can't both win the same term, and a committed entry is never lost. postgres itself doesn't run consensus; high-availability tools around it do.
the picture
you know how streaming replication gives you a standby, but someone still has to decide when the primary is dead and which standby takes over? get that decision wrong and you have two primaries both taking writes (split brain). consensus is how a group makes that kind of decision safely.
think of a committee that keeps minutes (the log). one member chairs each meeting (the leader for a term). the chair proposes each line of the minutes, and a line is official once a majority has written it into their own copy. if the chair vanishes, anyone can call a new meeting with a higher number, and becomes chair if a majority agrees. a member who missed a meeting copies the minutes from the chair.
five servers, no leader yet. press time out on any of them to start an election.
things to try: elect S1, write twice, replicate (committed). crash S1 and time out S3: it wins term 2 without S1. partition 2 | 3 and try electing S1 on the small side: two votes out of five isn't enough, ever.
raft, in three rules
Diego Ongaro and John Ousterhout designed raft (USENIX ATC 2014) to be easier to understand than Paxos, and split it into three parts.
leader election. every server is a follower, a candidate or the leader. a follower that hears nothing from a leader for a random timeout becomes a candidate: it increments the term, votes for itself and asks the others. each server gives at most one vote per term, and only to a candidate whose log is at least as up to date as its own. a candidate with votes from a majority becomes the leader for that term. the random timeouts make split votes rare; when they happen, someone times out again with a higher term.
log replication. clients send commands to the leader. it appends each one to its log and sends it to the followers. once a majority store an entry from the current term, it's committed: the leader applies it to its state machine, answers the client, and tells the followers, who apply it too. a follower whose log disagrees is overwritten with the leader's.
safety. the voting rule is what keeps committed entries alive: a committed entry is on a majority, a winner needs votes from a majority, the two overlap, and a voter refuses a candidate with an older log. so every new leader already has every committed entry.
quorums
"a majority" is a special case of a quorum: a set of replicas big enough that any two of them overlap. with n replicas, if every write waits for w acknowledgements and every read asks r replicas, then w + r > n guarantees that every read reaches at least one replica that has the latest acknowledged write. dynamo-style stores (DeCandia et al., SOSP 2007) expose n, w and r as knobs:
any 3 replicas and any 3 replicas share at least 1, so the read always meets the latest write and can pick it by its version. a write survives 2 slow or dead replicas, a read 2.
overlap is necessary, not sufficient. with concurrent writers, a failed write that reached fewer than w replicas, or replicas that disagree, a quorum read alone isn't linearizable: you also need versions to tell which value is newest, and usually a repair step. raft avoids that by funnelling everything through one leader and one ordered log.
postgres doesn't do this, its tools do
a postgres primary and its standbys are not a consensus group. there's no election: a standby becomes a primary when something runs pg_ctl promote (or pg_promote()), and postgres trusts that something to be right. synchronous replication with synchronous_standby_names = 'ANY 2 (a, b, c)' gives quorum commit (a commit waits for two of three standbys), but it doesn't pick a new primary.
so high-availability setups put a consensus system next to postgres. patroni, the common choice, keeps the cluster's state and a leader lock in a distributed configuration store (etcd, consul, zookeeper, or the kubernetes API), which run raft or a similar protocol themselves. only the node holding the lock may run as primary:
hover a stage.
keylines
- consensus: a group agrees on one log, despite crashes.
- raft: a leader per term, a replicated log, and majorities for everything.
- an entry is committed once a majority stores it, from the leader's current term.
- quorums overlap when w + r > n; that alone isn't linearizability.
- postgres doesn't elect anyone. patroni and friends use etcd, consul or zookeeper to decide.