The Consensus Problem

Topics Covered

What Consensus Means

The definition

Why it is harder than it looks

Equivalent problems

One shot versus repeated consensus

Crash faults and Byzantine faults

Why majorities

The cost of tolerating faults

A worked example of the problem

Decided means decided forever

Who needs to learn the decision

Consensus is not the same as voting

Reading the definition carefully

The previous lessons kept running into the same need. A linearizable register needs one order of writes. Leader based replication needs exactly one leader at a time. A lock needs exactly one holder. Atomic commit needs every participant to make the same decision. Each of these reduces to a single problem: getting a group of processes, some of which may fail, to agree on one value. That problem is consensus, and it is the foundation of every coordination service, every strongly consistent database and every reliable leader election. This lesson defines consensus precisely, explains why it is hard and introduces the ideas shared by Raft, Paxos and their relatives.

The definition

In the consensus problem, each process proposes a value, and all processes must decide on a value, subject to three properties. Agreement: no two correct processes decide different values. Validity: the decided value was proposed by some process, which rules out trivial solutions such as always deciding zero. Termination: every correct process eventually decides. Some formulations add integrity, which says a process decides at most once. Agreement and validity are safety properties, meaning nothing bad ever happens. Termination is a liveness property, meaning something good eventually happens. The tension between them is the heart of the problem.

Consensus takes several proposals and produces one decided value that every correct process agrees on and never changes.

Why it is harder than it looks

On a single machine, agreement is trivial: one variable holds the decision. In a distributed system, processes communicate only by messages that can be delayed, and any process can crash at any moment. A process that proposes a value and then crashes may or may not have told others about it. A process waiting for a message cannot tell whether the sender is slow or dead. Any protocol that waits for every process to respond can block forever if one crashes. Any protocol that decides without hearing from everyone risks two groups deciding differently. Consensus protocols navigate between these failure modes with carefully designed quorums and phases.

Equivalent problems

Many coordination problems are equivalent to consensus, meaning a solution to one gives a solution to the other. Atomic broadcast, or total order broadcast, which delivers the same messages in the same order to every process, is equivalent to consensus: given consensus, agree on the next message in each slot, and given atomic broadcast, decide the first message delivered. Linearizable compare and set is equivalent too: the first successful compare and set decides. Leader election with a guarantee of at most one leader per term, and atomic commit in its non blocking form, reduce to consensus as well. This is why one well implemented consensus service, such as etcd or ZooKeeper, can support locks, leader election, configuration and membership for an entire infrastructure.

One shot versus repeated consensus

The basic problem is one shot: decide a single value once. Real systems need repeated consensus: decide an endless sequence of values, such as the commands of a replicated log, one per slot. Running an independent instance per slot works but is wasteful, since each instance pays the full cost of agreement. Practical protocols such as Multi Paxos and Raft elect a stable leader that proposes values for many slots in a row, paying the expensive leader election phase rarely and a single round trip per slot in the common case. The rest of this section of the course is largely about how they do that safely.

Crash faults and Byzantine faults

This lesson and the next ones assume crash faults: processes may stop at any time and may restart with their durable state, but they never send incorrect messages. Byzantine fault tolerant consensus, such as PBFT, Tendermint and HotStuff, handles processes that may lie or behave arbitrarily, and needs more replicas, three f plus one to tolerate f faults instead of two f plus one, and more message rounds. Byzantine protocols matter in blockchains and in systems spanning mutually untrusted organizations. Inside a single company data center, crash fault tolerance is the norm, with authentication and checksums guarding against corrupted messages.

Why majorities

Crash tolerant consensus protocols use majority quorums: with n processes, any group of more than n over two. Two majorities always share at least one process, so any two decisions made by majorities have a witness in common who can prevent them from conflicting. With five processes, majorities of three tolerate two crashes, since the remaining three still form a majority. With three processes, one crash is tolerated. Even numbers add cost without tolerance: four processes need three for a majority and still tolerate only one crash, which is why consensus clusters almost always have an odd size.

The cost of tolerating faults

Tolerating f crash failures requires 2f plus 1 processes, and every decision requires hearing from f plus 1 of them. The time to decide is at least one round trip from the proposer to the nearest majority, and electing a new leader after a failure takes additional time, typically a timeout plus a round trip. These costs, measured in replicas, latency and failover time, are why systems confine consensus to the data that truly needs agreement, as earlier lessons on linearizability and CAP argued.

A worked example of the problem

Consider three replicas of a job scheduler that must agree which worker runs a nightly billing job. Replica one proposes worker A, replica two proposes worker B and replica three has not yet proposed anything. If replica one simply decides A because it heard nothing to the contrary, and replica two decides B for the same reason, the billing job runs twice and customers are charged twice. Agreement is violated. If instead each replica waits to hear from all others before deciding, and replica three has crashed, nobody ever decides and the job never runs. Termination is violated. A correct protocol must find a path between these outcomes: decide as soon as enough replicas have spoken that no conflicting decision can be made elsewhere, without waiting for replicas that may never answer.

Decided means decided forever

A subtle part of agreement is that it covers time as well as space. A process that decides B must never later change its mind, and a process that recovers from a crash must not decide something different from what it decided before. This is why a decision must be recorded durably before it is acted upon. Many bugs in hand rolled coordination code come from treating a decision as soft state, kept in memory and lost on restart, so that a recovered process takes part in a second round and contributes to a different outcome.

Who needs to learn the decision

Processes in consensus often play different roles. Proposers suggest values, acceptors vote and remember votes and learners find out what was decided. In practice a single server usually plays all three roles, but separating them clarifies the protocol. A value is chosen at the moment a majority of acceptors accept it, even if no learner knows yet. Learning is a separate step: a learner may hear about the choice from the proposer, from a majority of acceptors or from a later round that rediscovers it. This distinction between chosen and known is a recurring source of confusion and a key to reading the Paxos papers.

Consensus is not the same as voting

Majority voting in the everyday sense counts votes once and announces a winner. Consensus is harder because the voters can crash mid count, messages can be delayed or reordered and several counts can be in progress at once with no global view of which started first. The protocols in the following lessons are what voting becomes when nobody can see the whole room and anyone can leave at any moment.

Reading the definition carefully

Each property is easy to satisfy alone. Always deciding your own proposal gives validity and termination but not agreement. Never deciding gives agreement and validity but not termination. Deciding a fixed constant gives agreement and termination but not validity. The difficulty, and the reason consensus deserves a whole section of this course, lies only in satisfying all three together while processes crash and messages are delayed.

Consensus is the core of coordination

Consensus makes processes agree on one value despite crashes, with agreement and validity as safety and termination as liveness. Atomic broadcast, compare and set, leader election and non blocking commit all reduce to it, so a single consensus service can coordinate a whole system.