Skip to content
System Design Explorer
concepts

Consensus (Paxos & Raft)

By Ankur NayakReviewed

Consensus lets a group of servers agree on one value, or one sequence of log entries, even when some of them crash. Paxos and Raft do it with majorities: any two majorities overlap, so once a majority has accepted something, no later majority can decide differently. It is how replicated databases keep their copies identical.

Consensus (Paxos & Raft) diagramComponents: Client, Leader, Follower 1, Follower 2. Connections: Client to Leader (write); Leader to Follower 1 (append); Leader to Follower 2 (append).ClientClientLeaderCoordinationFollower 1SQL databaseFollower 2SQL database
A write committed by a majority

How it works

The replicated state machine idea: every server keeps a log of commands and applies them in order. If all servers agree on the same log, they all end up in the same state. Consensus is the protocol that makes them agree, and it must stay safe when messages are lost or servers crash: if one server applies a command as the nth entry, no server may apply a different nth entry.

Paxos (Lamport) works in two phases. A proposer picks a proposal number and asks a majority of acceptors to promise not to accept anything older (prepare / promise); it then asks them to accept a value (accept). Because any two majorities share at least one acceptor, a value accepted by a majority can never be contradicted by another majority.

Raft was designed to be easier to understand while matching Paxos's fault tolerance and performance. It first elects a leader, which then has full responsibility for the replicated log; leadership is numbered in terms, and an entry is committed once a majority has stored it. A five-server cluster keeps working with two servers down.

Production systems lean on long-lived leaders. Spanner runs a Paxos group per tablet with leader leases, and DynamoDB's partitions form replication groups that use Multi-Paxos for leader election; the leader writes a log record and acknowledges once a quorum of replicas has persisted it.

Key points

  • Safety comes from overlapping majorities
  • 2f + 1 servers tolerate f failures (5 servers survive 2)
  • Paxos: prepare/promise, then accept
  • Raft: elect a leader, replicate its log, commit at a majority
  • Leader leases make reads and lock management efficient

When to use it, and pitfalls

Use it for

  • Replicating a database partition (Spanner tablets, DynamoDB partitions)
  • Cluster metadata and configuration: who is the leader, where data lives
  • Distributed locks and leader election

Watch out for

  • Spreading replicas across distant regions makes every write pay the round trip
  • Even numbers of replicas add cost without adding failure tolerance
  • Implementing consensus yourself: use a proven library or service
  • Forgetting that a majority must be reachable: a minority partition can't make progress

Trade-offs

Consensus vs. asynchronous replication

Use consensus for data that must never diverge; asynchronous replication for data that can lag.

Pros: No lost writes on failover; Replicas never disagree

Cons: Every write waits for a majority; A majority must be up to accept writes

Used in

Frequently asked questions

What is the difference between Paxos and Raft?

They solve the same problem with the same fault tolerance and performance. Raft was designed to be easier to understand: it separates leader election from log replication and gives a single elected leader full control of the log, whereas classic Paxos is described as a protocol for agreeing on a single value.

How many servers do I need for consensus?

2f + 1 to survive f failures, because a majority must remain. Three servers tolerate one failure, five tolerate two. Adding a fourth server doesn't increase tolerance over three, since a majority of four is three.

Sources