Distributed Systems
Paxos Algorithm
Consensus Protocols
Computer Science
Algorithm Understanding

Paxos understanding

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Paxos is a consensus algorithm that's fundamental in the field of distributed computing and fault-tolerant systems. The algorithm was first described by Leslie Lamport in 1990 and serves as a method to achieve consensus among a collection of unreliable processors (nodes) in a network. Consensus algorithms are crucial because they ensure that a cluster of distributed systems can agree on a single data value such as the state of a database or the order of transactions, which is essential for system consistency and reliability.

What is Consensus?

In distributed systems, consensus involves multiple nodes agreeing on a single data value among many. This is challenging especially in an environment where system components may fail or messages may be lost or corrupted. Consensus algorithms must be:

  • Fault-tolerant: They should be able to reach an agreement even if some of the nodes fail.
  • Consistent: All non-failing nodes must agree on the same value.
  • Available: The system should continue to operate, despite some failures.

The Paxos Algorithm Explained

Roles in Paxos

Paxos divides nodes into three roles:

  1. Proposers
  2. Acceptors
  3. Learners
  • Proposers advocate for a particular value to be agreed upon.
  • Acceptors vote for proposals and agree to values.
  • Learners find out what value has been chosen.

Basic Process

The Paxos protocol operates in two main phases to reach a consensus:

  1. Phase 1: Prepare
    • A proposer selects a proposal number nn and sends a prepare request to a majority of acceptors.
    • If an acceptor receives a prepare request with a proposal number greater than any it has previously seen, it promises not to accept any proposals numbered less than nn and responds to the proposer with the highest-numbered proposal (if any) that it has accepted so far.
  2. Phase 2: Accept
    • If the proposer receives responses from a majority of acceptors to its prepare requests, it sends an accept request to each of those acceptors. This request includes a proposal number nn and the value vv proposed or null if no value has yet been accepted.
    • Acceptors confirm this accept request unless they have subsequently promised to respond to a prepare request with a higher number.

Consensus Achievement

Consensus is achieved when a majority of acceptors have accepted the same proposal.

Example of Paxos in Action

Imagine a distributed database with data replicas, needing to agree on the version of a particular record. If several nodes propose updates, Paxos helps ensure that the database extracts a single consistent update sequence, despite node failures or network issues.

Benefits and Drawbacks

BenefitExplanation
Fault-tolerancePaxos can tolerate up to (N1)/2(N-1)/2 nodes failing (for N\textit{N} total nodes).
Consistency GuaranteedOnce consensus is reached, all complying nodes agree on the same value.
Minimally BlockingProgress can be made as long as a majority of nodes operate correctly.
DrawbackExplanation
ComplexityPaxos is notoriously difficult to understand and implement correctly.
LatencyIn scenarios with many nodes or high message loss, Paxos can have high latency.
Resource IntensiveRequiring multiple phases and messages can strain network and compute resources.

Practical Use Cases

Paxos has been used in various high-profile distributed systems:

  • Google Chubby - a lock service for loosely-coupled distributed systems.
  • Microsoft’s Autopilot - for cluster management of large datacenter applications.

Conclusion

Paxos plays a central role in distributed systems where ensuring data consistency and fault tolerance is crucial. Despite its operational and conceptual complexity, when implemented correctly, Paxos provides a reliable method for diverse systems to reach consensus efficiently. Optimizations and variations of Paxos, such as Multi-Paxos, have been developed to handle specific scenarios or improve efficiency, confirming the algorithm’s foundational importance in computer science.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.