Distributed Systems Consensus


Distributed Systems Consensus

Simon
Imagine trying to coordinate a surprise party in a group chat, except the participants are thousands of computers. Messages can be delayed or lost, computers can crash, and two people can send contradictory plans at the same time. Distributed consensus is the problem of getting those independent processes to agree on one single value despite that chaos.

Corey
And the classic explanation we are using today is Leslie Lamport's 2001 paper, Paxos Made Simple. Paxos had originally been described in a way Lamport said was “Greek” to many readers. His later paper showed that beneath the intimidating details is a remarkably simple chain of ideas for reaching fault-tolerant agreement.

Simon
At its heart, consensus means that processes propose values, and the system chooses exactly one of them for a particular decision. One process might propose depositing one hundred dollars, while another proposes withdrawing fifty. The algorithm's job is to establish one final value that becomes the shared truth for that decision.

Corey
The same issue appears in an online game. If one server records a player picking up a rare sword while another records the player picking up a shield, the game world becomes inconsistent. Consensus ensures that one update wins and that the servers can use the same result.

Simon
Shared configuration services such as Google's Chubby or Apache ZooKeeper provide another example. They coordinate settings used by many applications, so one part of a cloud system does not operate with an old configuration while another part uses a new one.

Corey
Paxos separates two broad goals: safety and liveness. Safety means the system never reaches an incorrect or contradictory result. Liveness means the system eventually makes progress and that processes eventually learn the result.

Simon
The paper gives three safety requirements. First, only a value that was actually proposed may be chosen. There can be no mysterious value appearing from nowhere. Second, only one value may be chosen for a decision, so the system cannot maintain two conflicting realities.

Corey
The third safety rule is that a process must never learn that a value was chosen unless that value really was chosen. That prevents a server from acting on a false belief that a transaction, balance update, or other critical operation has been finalized.

Simon
Liveness has two corresponding ideas. Some proposed value should eventually be chosen rather than the system freezing forever. And once a value has been chosen, a process should eventually be able to learn what that value is.

Corey
A single central decision-maker seems tempting. Proposers could send their values to one acceptor, which chooses the first valid value it receives. But that acceptor is a single point of failure: if it crashes or becomes unreachable, no further decisions can be made.

Simon
Paxos therefore works with multiple acceptors. Its system model is asynchronous and non-Byzantine. Asynchronous means processes can run at arbitrary speeds, pause unpredictably, crash, and later restart. Messages can take an arbitrarily long time, be duplicated, or be lost.

Corey
Non-Byzantine means the participants are not malicious. A process may be slow or crash, but it does not intentionally lie or corrupt the protocol. Messages may be delayed or lost, but a message that arrives is not altered. Handling actively deceptive processes would require a different class of algorithms, such as Byzantine-fault-tolerant approaches.

Simon
Crashes create a crucial requirement: important information must survive a restart. Acceptors need stable storage, such as durable disk storage or another form of nonvolatile memory, for promises and accepted proposals. Techniques like write-ahead logging or journaling can ensure that the durable record is updated before in-memory state changes.

Corey
Without that durable memory, an acceptor could reboot, forget a promise, and accept a conflicting proposal. Proposers are different: they do not need stable storage for safety. If a proposer abandons an attempt, it can start again later with a new, higher proposal number.

Simon
Paxos describes three logical roles. Proposers suggest values. Acceptors receive proposals and accept them while enforcing the consistency rules. Learners find out which value was chosen and use that result to update their own state or take action.

Corey
Those are conceptual roles, not necessarily separate machines. In a practical system, one physical server can act as a proposer, an acceptor, and a learner at the same time.

Simon
A value is considered chosen when one proposal has been accepted by a majority of the acceptors. Majority is the key because any two majorities overlap. With five acceptors, any group of three shares at least one acceptor with any other group of three.

Corey
Suppose proposal A is accepted by a majority and proposal B is also supposedly accepted by a majority. Because those majorities overlap, at least one acceptor would have had to accept both. If acceptors are restricted so that they cannot accept two different values for the same decision, both values cannot be chosen.

Simon
The naive rule of accepting the first proposal an acceptor sees is not enough for liveness. Imagine one proposer sends X to acceptors one, two, and three, while another sends Y to three, four, and five. Some acceptors may accept X and others Y, leaving neither proposal with a majority. The system remains safe, but nothing gets chosen.

Corey
The solution is to let an acceptor accept more than one proposal, but to distinguish proposals by unique, increasing natural-number identifiers. A proposal is now a pair: a number and a value, such as proposal 101 carrying X. A particular numbered proposal and its value are chosen only when that exact proposal is accepted by a majority.

Simon
Lamport derives increasingly strong safety properties. P2 says that if a proposal carrying value V is chosen, then every higher-numbered proposal that is chosen must also carry V. Once X has been chosen, a later winning proposal cannot change the decision to Y.

Corey
P2A strengthens that requirement: if a proposal with value V is chosen, then every higher-numbered proposal accepted by any acceptor must carry V. That pushes the consistency rule down from winning proposals to individual acceptances.

Simon
P2B strengthens it again: if a proposal with value V is chosen, then every higher-numbered proposal issued by any proposer must carry V. The challenge is enforcing that when a proposer might not know that an earlier value has already been chosen.

Corey
That is what P2C addresses. Before issuing proposal number N with value V, a proposer must consult a majority of acceptors. Either none of those acceptors has accepted a lower-numbered proposal, in which case the proposer may use its own value, or the proposer must use the value from the highest-numbered proposal previously accepted by that majority.

Simon
In plain English, a new proposer has to learn the relevant history before choosing its value. If the majority reports that an earlier proposal carrying Z has the highest accepted number, then the new proposal must carry Z as well. That is how a potentially chosen value is carried forward instead of being replaced.

Corey
The proposer also needs to prevent older proposals from being accepted during that information-gathering process. Paxos handles both needs with two phases: first prepare, which gathers history and obtains promises, and then accept, which attempts to establish the proposal as chosen.

Simon
In phase one, the proposer selects a new proposal number N, higher than any number it has previously used for that consensus instance. It sends a prepare request containing N to acceptors, typically seeking a majority of responses.

Corey
When an acceptor receives prepare N, it compares N with the highest proposal number it has already promised to honor. If N is higher, it records N in stable storage as its new maximum promise. It promises not to accept proposals numbered less than N, and it reports the highest-numbered proposal it has already accepted, if any.

Simon
If the incoming prepare number is less than or equal to the acceptor's existing promise, the acceptor ignores the request. It has already promised to reject proposals below a higher number, so responding positively to the older prepare would undermine that promise.

Corey
After receiving promises from a majority, the proposer selects the value for phase two. If any response reports an earlier accepted proposal, the proposer must use the value associated with the highest-numbered one. If no response reports an accepted proposal, the proposer may use its own intended value.

Simon
The proposer then sends an accept request containing the pair N and V to the same majority. An acceptor accepts it only if it has not made a promise for a number higher than N. Because its phase-one promise was to reject numbers less than N, it may accept N itself, but not if a later prepare has raised its promise above N.

Corey
If the acceptor accepts, it records the proposal pair in stable storage and can send an accepted message to the proposer and learners. Once a majority has accepted that same numbered proposal and value, the value is officially chosen.

Simon
Learners can discover the result in several ways. The simplest is for every acceptor to send each accepted proposal to every learner. A learner watches for the same proposal to be reported by a majority, but this can create a large number of messages when there are many learners.

Corey
An optimization is to use a distinguished learner. Acceptors send their accepted messages to that learner, which determines when a majority has accepted a proposal and then informs the other learners. This reduces communication from roughly the product of acceptors and learners to roughly their sum.

Simon
The trade-off is that one distinguished learner can become a bottleneck or a failure point for learning. A small group of distinguished learners offers better reliability while avoiding the full message cost of having every acceptor contact every learner.

Corey
If a learner suspects that a value was chosen but never received the news, it can ask acceptors about their highest accepted proposals. Or it can initiate another Paxos round with a sufficiently high number. The P2C rule forces that round to preserve any value that was already chosen, while ensuring that a value is chosen if none had been chosen before.

Simon
Safety does not automatically guarantee liveness. Paxos can suffer from livelock, where processes remain active and exchange messages but never complete a decision. For example, proposer P may obtain promises for number 100, only for proposer Q to obtain promises for 101 before P's accept request arrives.

Corey
P's proposal is then rejected because the acceptors promised not to accept numbers below 101. P retries with 102, but Q may preempt that with 103, and the cycle can continue indefinitely. Unlike deadlock, where activity stops, livelock involves continual activity without forward progress.

Simon
This connects to the FLP impossibility result from 1985. In a purely asynchronous system where messages can be delayed arbitrarily and even one process may crash, no deterministic consensus algorithm can guarantee both perfect safety and guaranteed termination. To guarantee progress, a system must introduce something beyond that pure model, such as randomness or real-time assumptions and timeouts.

Corey
Practical Paxos systems usually break the symmetry by electing a distinguished proposer, or leader. The leader is the main process allowed to initiate proposals, so competing proposers do not continually preempt one another. If the leader is stable and can communicate with a majority, it can eventually complete proposals.

Simon
Leader election is for liveness, not safety. Even if two servers temporarily believe they are leaders, or leaders repeatedly crash, Paxos still prevents two different values from being chosen for the same instance. The system may pause or livelock, but it does not become inconsistent.

Corey
Paxos becomes especially useful when repeated for a sequence of decisions. This is state machine replication. A deterministic state machine produces the same outputs and final state whenever it starts from the same initial state and receives the same commands in the same order.

Simon
Instead of running that state machine on one server, a system runs identical replicas on several servers. The replicas remain consistent if they execute the same command sequence. Paxos provides agreement on that sequence by using a separate Paxos instance for each command slot.

Corey
For example, instance one chooses the first command, instance two chooses the second, and instance three chooses the third. Each server can act as proposer, acceptor, and learner for every instance, creating a fault-tolerant, agreed-upon log of operations.

Simon
In the normal case, a leader receives a client command and assigns it to the next log slot, such as slot 135. It uses Paxos to choose that command for instance 135. Once every replica learns the command for slot 135 and has processed slots one through 134, it executes the command locally.

Corey
Because the state machine is deterministic and every replica executes commands in the same order, the replicas maintain identical states. A deposit followed by a withdrawal can produce a different result from a withdrawal followed by a deposit, so agreeing on order is just as important as agreeing on each individual command.

Simon
A new leader may discover gaps after a failure. It might find that slots one through 134 and 138 through 139 are known, while slots 135 through 137 are uncertain. It cannot simply skip those slots, because the replicated state machine needs a contiguous command sequence.

Corey
The leader runs phase one for each missing slot. If acceptors report that an earlier command was partially accepted for slot 135, the leader must carry that command forward in phase two. If no earlier value was accepted for slot 136, it proposes a no-op, which fills the slot without changing the state.

Simon
After recovering values or filling gaps with no-ops, the leader can resume with later slots. The fact that the value is not selected until after phase one gives the leader the flexibility to choose a client command, a recovered command, or a no-op based on the history returned by acceptors.

Corey
A stable leader can also make the common case more efficient. If it already has standing promises from acceptors for future slots, it may need only phase two to propose a command. That can reduce agreement to essentially one round trip: a message from the leader and responses from a majority.

Simon
The same safety story applies to replicated logs. If leader election fails completely, new commands stop being proposed, but the existing log remains consistent. If multiple leaders compete for one slot, they may cause livelock, but P2C and the other safety rules ensure that two different commands cannot both be chosen for that slot.

Corey
Paxos can even support changing the membership of the consensus group. The configuration itself can be stored as part of the replicated state machine. A special command can add a server or remove one, and once that command is chosen, every replica updates its configuration and later decisions can use the new acceptor set.

Simon
So the big picture is that Paxos builds robustness from unreliable components. Majority overlap prevents conflicting choices, numbered proposals and the P2 properties preserve one value over time, two phases gather history and establish promises, and stable storage preserves those promises through crashes.

Corey
Leader election and timeouts help with progress, but they are not part of the core safety guarantee. Paxos may temporarily stop making decisions in the face of failures or competing leaders, yet it will not choose two different values for one consensus instance.

Simon
And by repeating consensus across numbered slots, the single-value algorithm becomes a distributed log capable of supporting replicated databases, coordination services, distributed file systems, and other fault-tolerant infrastructure. That is the lasting lesson of Paxos Made Simple: careful logical rules can turn a chaotic distributed environment into a consistent shared system.

Ask anything
We've read this whole study set, so questions about it get the best answers.