1. Home
  2. Explore
  3. Computer Science
  4. Paxos Consensus Algorithm

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Paxos Consensus Algorithm

Paxos: Reaching Agreement Across Distributed Systems

A distributed system uses multiple computers, or nodes, to store information or carry out work. This can make a system larger and more resilient than a single computer, but it also creates a coordination problem: different nodes need to agree on what happened and in what order.

Paxos is a family of distributed algorithms for reaching that agreement, called consensus. This lesson focuses on the basic, or “vanilla,” version of Paxos. Its central purpose is to let a group of nodes agree on one value even when messages may be delayed or lost.

What consensus means

Consensus means agreeing on one result. The participants may begin with different proposals, but the process is designed so that a successful decision has a single agreed value.

Imagine a group deciding what to do together. One person suggests going to a movie, while another suggests dinner. More people may support either choice. Eventually, if the group reaches agreement, the participants settle on one activity. Consensus does not mean that everyone originally preferred the same option; it means that the group ends up agreeing on one outcome.

Paxos uses a majority to determine when a value has been accepted. In a group of three acceptors, for example, two form a majority. Once a majority accepts the same value, consensus has been reached on that value. Other participants may learn about the result later, so agreement can be reached before every node knows it.

This distinction matters in distributed systems: reaching consensus and every node learning that consensus was reached are separate events.

Consensus is agreement on one value. In Paxos, a majority of acceptors accepting a value is enough to establish that agreement.

Why consensus is useful

A single computer can serve a growing number of users only up to a point. Upgrading its processor or memory may help temporarily, but one machine has limits. A system that must handle more work can spread its data or responsibilities across multiple computers.

Two broad arrangements illustrate why distributed systems need consensus:

  • In a leader-and-replicas arrangement, one node acts as the leader and other nodes keep replicas of the data. The leader orders proposed changes and sends them to replicas. If the leader becomes unavailable, the system may need agreement on a replacement; otherwise, work can stall.
  • In a peer-to-peer arrangement, nodes have equivalent roles. They must coordinate to determine which proposed operation comes next, so that the operations are applied in the same order across the system.

In either arrangement, nodes need a reliable way to agree on decisions. Without that coordination, different parts of the system could apply conflicting changes or apply the same changes in different orders.

Think About It: Why might a system with several replicas need agreement on the order of operations, even if every replica eventually receives the same operations?

Paxos participants and assumptions

Paxos describes three roles:

  • A proposer suggests a value for the group to decide on.
  • An acceptor participates in deciding whether a proposal can be accepted.
  • A learner finds out which value has been agreed upon and can provide that result when queried.

These are roles, not necessarily separate machines. One server can act as a proposer, an acceptor, and a learner at the same time.

Majorities and overlap

Paxos depends on majorities. A majority is more than half of the acceptors. The important property is that any two majorities overlap in at least one acceptor.

For example, with three acceptors, a majority has two members. Any two groups of two drawn from the same three acceptors must share at least one member. That overlap lets a later proposal discover information recorded by an earlier one. It is a key reason Paxos can prevent conflicting decisions.

Persistent acceptor state

Acceptors must remember important commitments and accepted proposals. In particular, an acceptor cannot simply forget that it promised to ignore proposals below a certain identifier or that it accepted a value. If it forgot this information, it might later act as though an incompatible proposal were still eligible.

The basic Paxos algorithm therefore assumes that acceptor state persists. Some variants handle different assumptions, but forgetting such state is not part of the basic version taught here.

One decision per Paxos run

A single Paxos run decides one value. Once that decision has been made, the same run does not move on to decide a different value. If the system needs to change a value over time, it must run Paxos again for another decision.

This is why a changing data item, such as a bank balance, cannot be represented by one unchanging Paxos decision. A distributed storage system can instead use a sequence of decisions, one for each position in a log.

How a Paxos run works

Each proposal has a unique, ordered identifier. The identifier helps the system distinguish competing proposals and determine which is newer. It is an internal coordination detail; the application’s decision is the value, not the identifier.

A proposer that times out can try again with a higher identifier. The proposer may generate identifiers in a way that prevents reuse—for instance, different proposers can use different number patterns. The essential requirements are uniqueness and the ability to compare identifiers.

Paxos proceeds in two main phases: a prepare phase and an accept phase.

Phase 1: Prepare and promise

A proposer begins by sending a PREPARE message containing its identifier to a majority, or to all, of the acceptors.

When an acceptor receives the message, it checks its recorded promise:

  • If it has already promised to ignore proposals with that identifier, it does not proceed with the request.
  • Otherwise, it promises to ignore proposals with lower identifiers and responds with a PROMISE message.

A promise is not an acceptance of the proposed value. It is a commitment about which future proposals the acceptor will consider. Once a majority has promised for a particular identifier, no lower-identifier proposal can receive promises from a majority. Any competing majority would have to overlap the promising majority, and an overlapping acceptor will reject the lower proposal.

Phase 2: Accept request and acceptance

After receiving promises from a majority, the proposer sends an ACCEPT REQUEST containing its identifier and a value to accept.

If an acceptor has not made a promise that rules out this request, it can accept it. It then sends an ACCEPT message with the identifier and value, typically to the proposer and the learners.

When a majority of acceptors have accepted the same value, consensus has been reached. The identifier helps coordinate proposals, but the value is what the system has agreed upon.

A proposer or learner that receives ACCEPT messages from a majority can conclude that the value has been decided. A learner may find out after the decision itself occurred, because messages can be delayed or lost.

When an earlier value has already been accepted

The proposer’s choice of value is not always unrestricted. This rule is what preserves safety when proposals overlap.

Suppose a proposer gets promises for a new identifier. Those promise responses may include information about values the acceptors previously accepted. This information is often called a piggybacked value: the promise carries both the promise and the acceptor’s relevant prior acceptance.

The proposer follows this rule:

  • If none of the promises reports a previously accepted value, the proposer may use its own proposed value.
  • If any promise reports a previously accepted value, the proposer must use the value associated with the highest accepted identifier reported among the promises.

The proposer may therefore use a newer identifier for the ACCEPT REQUEST while retaining a value chosen in an earlier proposal.

Consider a run in which a majority accepted the value “cat” under identifier 555. A later proposer starts a prepare phase using identifier 666. Because any majority overlaps the earlier accepting majority, at least one acceptor in the new majority can report the earlier accepted value. The proposer must then request acceptance of “cat” under identifier 666, rather than substitute a different value. This allows a later proposer to carry forward a decision even if it did not witness the original majority of ACCEPT messages.

Think About It: Why does Paxos require a proposer to reuse the highest previously accepted value it learns about, instead of freely replacing it with a new proposal?

How Paxos handles competing proposals

Messages in a distributed system do not necessarily arrive in the order they were sent. Nodes can be far apart, network delays can differ, and messages can be lost. Paxos uses identifiers, promises, and majority overlap to cope with this uncertainty.

A lower identifier loses to a higher promise

Suppose one proposer sends PREPARE with identifier 555 and receives promises from a majority. Another proposer sends PREPARE with identifier 444. Some acceptors may receive the two messages in a different order, and the second proposer might even receive a promise from one acceptor. But it cannot get promises from a majority: the two majorities must overlap, and acceptors in the first majority have promised to ignore lower identifiers.

The proposer using identifier 444 must time out and retry with a higher identifier. If it uses 666, it may obtain promises—provided a still higher proposal has not already won them.

Contention and retry delays

Several proposers can interfere with each other. One proposer might obtain promises for identifier 555, while another obtains promises for identifier 666. The first proposer’s lower-identifier request can no longer succeed with a majority. If it retries with an even higher identifier, it may in turn interfere with the second proposer.

This competition is called contention. If it continues, the run can stall because each proposer keeps superseding the other’s prepare phase before either finishes the accept phase.

One possible response is exponential backoff: a proposer waits progressively longer before retrying after repeated conflicts. The pauses can give one proposal time to complete both phases. Once a majority accepts a value, the decision is established, and further proposals for that run must preserve that value.

Why a decided value stays safe

After a majority accepts a value under one identifier, two safeguards protect that decision:

  1. A lower-identifier proposal cannot later be accepted by a majority, because it cannot obtain promises from a majority after the higher promise has been established.
  2. A later, higher-identifier proposal must learn about the earlier accepted value through the overlap between majorities. The proposer must carry forward the value associated with the highest accepted identifier it learns about.

Thus later identifiers may appear, but they cannot use the protocol to replace an already-established decision with a conflicting value.

The algorithm’s phases also distinguish three important milestones:

  1. A majority promises for an identifier, ruling out lower identifiers from succeeding.
  2. A majority accepts an identifier and value, establishing consensus on the value.
  3. A proposer or learner receives enough ACCEPT messages to know that consensus was established.

The third milestone can happen after the second because communication is not instantaneous and messages may be lost.

Using Paxos for changing data: a replicated log

A system often needs to make many decisions over time, even though one Paxos run decides only one value. A common solution is a replicated log: an ordered sequence of positions, with a separate Paxos decision for each position.

Each log position records one decision. The first position can represent the initial state of an account, such as its balance when opened. Later positions record changes in order. A balance that changes over time is therefore represented not by changing one Paxos decision, but by adding decisions at successive log positions.

For example:

  • Log position 000 records the account’s initial state.
  • Position 111 records a deposit of $50. The system may also record the resulting balance, such as $150, for efficiency.
  • Position 222 records a withdrawal of $20, leaving a balance of $130.

The precise application data stored in each position can vary. The essential idea is that each new change gets its own consensus decision, while the log preserves the sequence of changes.

A simplified distributed storage example

Suppose an account is replicated across three storage servers: replica A, replica B, and replica C. Each server can act as a proposer, acceptor, and learner.

A client sends a read or a proposed write to one server. If a client requests a withdrawal of $30, that server can act as a proposer and start a Paxos run for the next appropriate log position. The servers act as acceptors. If a majority accepts the operation, consensus has been reached, and the agreed entry can be written and propagated.

The system can make progress even if one replica has not yet recorded the decision. For example, two of the three replicas can form a majority. The third replica can catch up later by using an available recovery mechanism or by participating in a later proposal. In the latter case, the promises can reveal the previously accepted value, requiring the proposer to carry that value forward. In this way, the lagging replica can learn the agreed entry rather than introduce a conflicting one.

The replicated log connects Paxos’s single-decision rule to practical storage. Paxos decides one log entry at a time; the sequence of entries represents the evolving state of the account.

Think About It: How does a sequence of consensus decisions allow a replicated bank account to change while each individual Paxos run still decides only one value?

Comprehensive review

Paxos lets distributed nodes agree on a value despite unreliable communication and competing proposals. It does so by dividing the work among proposers, acceptors, and learners, while relying on acceptors that remember their promises and prior acceptances.

A proposer first requests promises for a unique identifier. A majority’s promises prevent lower identifiers from succeeding. The proposer then requests acceptance of a value. If a majority accepts that value, consensus is established. If the proposer learns that an earlier value was already accepted, it must carry forward the value associated with the highest accepted identifier it learns about. Majority overlap makes that information available to later proposals and prevents an established decision from being replaced by a conflicting one.

Paxos can experience contention when proposers repeatedly supersede one another, and retry delays such as exponential backoff can give a proposal time to complete. A single run decides one value; systems that need changing data use repeated decisions, often arranged as positions in a replicated log. This combines consensus on individual entries with an ordered history of changes.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent