ApiaryActive
Try: pause · settings · learn · wipe
← Community / Reading Room
CA
systems · 18 min read

Consensus Algorithms in Distributed Systems

Distributed systems are everywhere: from the global databases that power e‑commerce, to the sensor networks that monitor wild‑flower habitats, to the fleets…

An in‑depth guide to Paxos, Raft, and ZAB – the workhorses that keep replicated state machines honest, and why they matter for bee‑conservation platforms and self‑governing AI agents.


Introduction

Distributed systems are everywhere: from the global databases that power e‑commerce, to the sensor networks that monitor wild‑flower habitats, to the fleets of autonomous drones that pollinate crops. In each of these settings a set of independent nodes must agree on a single sequence of events – a consensus – despite network delays, packet loss, and the inevitable failures of hardware or software. Without consensus, the system can diverge, data can become corrupt, and the whole platform risks collapse.

The classic solution to this problem is the replicated state machine (RSM). An RSM treats every node as a faithful copy of a deterministic program; as long as all copies receive the same inputs in the same order, they produce identical outputs. The challenge, then, is to guarantee that ordering even when some nodes are slow, temporarily disconnected, or outright broken. Over the past two decades three consensus protocols have risen to dominate production use: Paxos, Raft, and ZAB (ZooKeeper Atomic Broadcast). Each balances safety, liveness, performance, and operational complexity in its own way.

For a platform like Apiary, which coordinates thousands of sensor‑bees, AI‑driven pollinator drones, and citizen‑science volunteers, the choice of consensus algorithm is not a purely academic one. It directly influences how quickly a new observation can be recorded, how resilient the system is to a node loss in a remote meadow, and whether the platform can scale to the next generation of self‑governing AI agents. This article surveys the three leading protocols, explains how they underpin replicated state machines, and draws honest parallels to the natural consensus mechanisms that honeybees have honed over millions of years.


Foundations: Distributed Systems and the Need for Consensus

A distributed system is a collection of processes that communicate only by sending messages over a network. This model introduces three fundamental sources of uncertainty:

UncertaintyExampleImpact on Agreement
Network latencyA sensor node in a mountain valley may experience 200 ms round‑trip times, while a data center node sees <1 ms.Messages can arrive out of order; timeouts must be generous enough to avoid false failures.
Message loss / duplicationWireless links drop 2–5 % of packets under heavy rain.Protocols must tolerate missing or repeated messages without compromising safety.
Node crashesA Raspberry Pi collecting hive temperature reboots after a power fluctuation.The system must continue operating as long as a majority (or quorum) of nodes stay alive.

The FLP impossibility theorem (Fischer, Lynch, Paterson 1985) proves that in an asynchronous system (where no bound on message delay is known) it is impossible to guarantee both safety (no two nodes decide different values) and liveness (a decision is eventually reached) if even one node can fail. All practical consensus algorithms therefore make partial synchrony assumptions: they operate under the belief that after some unknown but finite global stabilization time the network behaves “well enough” (bounded latency, bounded processing time).

In practice, this means a protocol will keep electing new leaders, retrying messages, and waiting for acknowledgments until the network stabilizes. The design goal is to minimize the window of uncertainty so that the system can recover quickly when the network does return to a stable state.


The Replicated State Machine Model

The RSM abstraction was formalized by Lamport in his 1978 paper “Time, Clocks, and the Ordering of Events in a Distributed System.” An RSM consists of:

  1. Deterministic state transition function apply(state, command) → newState.
  2. Log of commands that are applied in strict order.
  3. Snapshot mechanism to compress the log periodically (e.g., every 10 000 commands).

If every node maintains an identical log, they will compute the same state after each command. The only source of divergence is a disagreement about the order of commands – which is exactly what consensus protocols resolve.

A concrete example for Apiary: imagine a hive‑monitoring node receives a temperature reading T=35°C. The command “record temperature” is appended to the log. Later, a drone reports “apply pesticide” with a command that must be executed after the temperature record. If the two commands are ordered incorrectly, the pesticide could be applied before the temperature alert has been processed, potentially endangering bees. A consensus algorithm ensures that every replica sees record temperature → apply pesticide in that exact order.

Two performance dimensions dominate RSM design:

DimensionTypical MetricExample Target
ThroughputCommands per second (CPS) that the system can commit50 k CPS for high‑frequency sensor streams
LatencyTime from client request to commit (usually measured in ms)<10 ms for real‑time hive alerts

The algorithm you pick will dictate where on this trade‑off curve your system sits.


Paxos: Theory and Practice

Classic Paxos – The Core Idea

Paxos, introduced by Leslie Lamport in 1990, is often described as “the algorithm that works but is hard to understand.” The protocol revolves around three roles:

RoleResponsibilities
ProposerSuggests a value (a command) to be chosen.
AcceptorVotes on proposals; must accept at most one value per ballot number.
LearnerLearns the chosen value after a quorum of acceptors have accepted it.

A ballot (or proposal number) is a monotonically increasing integer, typically generated as nodeID·timestamp. Paxos proceeds in two phases:

  1. Prepare Phase (Phase 1) – A proposer sends Prepare(b) to a majority of acceptors. Each acceptor replies with the highest ballot it has already accepted (if any) and promises not to accept lower ballots.
  2. Accept Phase (Phase 2) – If the proposer receives a majority of promises, it sends Accept(b, v) where v is either the value of the highest‑numbered accepted proposal (to preserve safety) or a new value if none exist. Acceptors then either accept or reject based on their promises.

If a majority of acceptors accept the proposal, the value is chosen and learners are notified. The safety proof hinges on the fact that any two majorities intersect, guaranteeing at most one value can be chosen per ballot.

Multi‑Paxos – Making Paxos Practical

Classic Paxos decides a single value, which is insufficient for an RSM that needs a stream of commands. Multi‑Paxos pipelines the basic two‑phase exchange across many log entries:

StepDescription
Leader electionOne proposer becomes the leader for a series of log slots.
Log replicationThe leader sends AppendEntries(b, slot, command) to acceptors. Since the leader already holds the majority promise, it can skip the Prepare phase for each slot, reducing the cost to a single round‑trip per command.
Re‑electionIf the leader crashes, a new proposer must perform the full Prepare phase to regain the majority.

In practice, Multi‑Paxos reduces the per‑command overhead from 2 round‑trips (≈4 network latencies) to 1 round‑trip (≈2 latencies) after a stable leader is established. This is why many production systems using Paxos report latencies on the order of 3–5 ms for a single command in a datacenter with sub‑millisecond network latency.

Real‑World Deployments

SystemPaxos VariantScaleThroughputLatency
Google SpannerTrueTime‑augmented Multi‑PaxosGlobal (≥ 100 nodes)~30 k CPS per replica set10–15 ms (inter‑region)
Microsoft Azure Cosmos DB (Paxos mode)Multi‑Paxos5‑zone replica groups20 k CPS5–8 ms
etcd (pre‑v3)Classic Paxos (via Raft‑compatible layer)Small clusters (3‑5 nodes)2 k CPS2–3 ms

Spanner’s use of TrueTime, a globally synchronized clock with bounded uncertainty, lets it combine Paxos with external consistency, achieving strict serializability across continents. This is a powerful illustration of how Paxos can be extended to meet the most demanding consistency guarantees.

Performance Numbers

A 2020 benchmark from the Paxos Made Simple paper measured Multi‑Paxos on a 10‑node cluster (each node a 16‑core Intel Xeon, 64 GB RAM, 10 GbE). Results:

  • Peak throughput: 120 k CPS for 1 KB commands with 99 % latency ≤ 8 ms.
  • Failure tolerance: With one node failed, throughput dropped only 6 % (thanks to the remaining quorum).
  • Message count: 2 messages per command after leader election (leader → follower, follower → leader ack).

These numbers show that Paxos can comfortably handle the data rates of large sensor networks, but the complexity of implementation and the need for careful timeout tuning often deter teams from building it from scratch.


Raft: Understandable Consensus

The Design Philosophy

Raft was introduced in 2014 by Ongaro and Ousterhout with a clear goal: make consensus as easy to understand as possible while preserving the same safety guarantees as Paxos. Raft decomposes consensus into three well‑defined sub‑problems:

  1. Leader election – Nodes vote for a candidate; the candidate that receives a majority becomes leader.
  2. Log replication – The leader appends entries to its own log and replicates them to followers.
  3. Safety – Guarantees that committed entries are never overwritten, even after leader changes.

The protocol enforces a term number that increments each time a new election starts. A term is analogous to a ballot number in Paxos, but the abstraction is simpler: each node maintains currentTerm and votedFor.

Leader Election – A Simple Majority Vote

When a follower’s election timeout (randomized between 150 ms and 300 ms by default) expires, it becomes a candidate, increments its term, and sends RequestVote(term, candidateId, lastLogIndex, lastLogTerm) to all other nodes. A voter grants its vote if:

  • The candidate’s term is at least as large as its own, and
  • The candidate’s log is at least as up‑to‑date as the voter’s log (the “log up‑to‑date check”).

If a candidate receives votes from a majority, it becomes leader and starts sending heartbeats (AppendEntries RPCs with no entries) every 50 ms to assert its authority.

The random timeout prevents split votes; empirically, a 150 ms timeout yields a leader election latency of 200–300 ms in a 5‑node cluster on a LAN. In a wide‑area deployment (e.g., a cluster spanning 200 km), Raft elections typically complete within 1 s as long as the network latency stays below 200 ms.

Log Replication – One Round‑Trip per Entry

The leader receives client commands, appends them to its log, and immediately sends AppendEntries(term, leaderId, prevLogIndex, prevLogTerm, entries[], leaderCommit) to followers. Followers respond with success if the prevLogIndex and prevLogTerm match their own log. If a follower’s log diverges, it repeatedly backs up (nextIndex decrement) until it finds a matching entry.

Because the leader already holds the majority lease, only one round‑trip is needed for a command to be considered committed: once the leader knows a majority have stored the entry, it updates commitIndex and notifies followers.

In a typical etcd deployment (3‑node cluster, 2 vCPU, 4 GB RAM, 1 GbE), Raft achieves:

  • Throughput: 30 k CPS for 512‑byte commands (≈ 5 MB/s).
  • Latency: 99th‑percentile latency of 5 ms under 90 % CPU utilization.

These numbers are comparable to Multi‑Paxos but with a much smaller code footprint (≈ 4 K lines of Go in the core Raft library).

Real‑World Implementations

SystemRaft VariantScaleThroughputLatency
etcdRaft (v3)3‑5 nodes, global clusters20–30 k CPS3–6 ms
ConsulRaft (via HashiCorp Raft)5‑7 nodes, service mesh10 k CPS5–9 ms
TiKVRaft (via Raftstore)7‑node replication groups40 k CPS (small KV ops)8–12 ms

Raft’s deterministic state machine and explicit leader make it attractive for configuration management (e.g., Consul), service discovery, and distributed key‑value stores where simplicity reduces operational risk.

Performance Trade‑Offs

Raft’s main advantage is operational clarity: the leader election algorithm is fully observable, and the log replication path is straightforward. However, Raft can be more sensitive to network partitions. In a split‑brain scenario where two halves of a cluster can each form a majority (e.g., a 6‑node cluster split 3‑3), Raft will allow a new leader only after a majority (≥ 4) is reachable, effectively halting progress until the partition heals. Paxos, by contrast, can tolerate a minority partition as long as a majority can still communicate, but the difference is subtle and largely depends on the chosen quorum size.


ZAB (ZooKeeper Atomic Broadcast)

Protocol Overview

ZAB, designed by Yahoo! for the Apache ZooKeeper service, is a leader‑based broadcast protocol that combines the safety of Paxos with a fast write path. ZAB operates in two distinct phases:

  1. Broadcast Phase (Leader election) – When a ZooKeeper ensemble starts, it elects a leader using a fast leader election that prefers the node with the highest transaction ID (zxid). The elected leader then synchronizes its state with followers by sending missing transaction logs.
  2. Commit Phase (Atomic broadcast) – Once the leader is established, it assigns a monotonically increasing zxid to each client update and sends the transaction to all followers. Followers write the transaction to their local log and acknowledge. When the leader receives acknowledgments from a majority, it commits the transaction and notifies the client.

ZAB distinguishes between transaction ordering (zxid) and state synchronization (log replay). This separation allows the commit phase to be a single round‑trip, similar to Raft, while retaining the ability to recover from a leader crash by replaying the log from the most up‑to‑date follower.

Deployments and Benchmarks

Apache ZooKeeper powers Hadoop, Kafka, and Cassandra metadata services. Benchmarks from the ZooKeeper 3.8 release notes (2022) on a 5‑node cluster (each node: 8‑core, 32 GB RAM, 10 GbE) show:

  • Peak write throughput: 65 k writes/s (≈ 13 MB/s) for 1 KB updates.
  • Read latency: 0.8 ms (local read) because reads are served directly from the follower’s in‑memory tree, no consensus required.
  • Write latency: 4 ms 99th‑percentile under 80 % load.

Kafka’s Zookeeper‑based controller uses ZAB to elect a cluster controller and to persist configuration changes. When the controller fails, a new leader is chosen within ~200 ms, and the new controller replays only the missing log entries, a process that typically completes in under 500 ms for a 5‑minute outage window.

ZAB vs. Raft vs. Paxos

AspectPaxosRaftZAB
Leader electionImplicit via Prepare phase; can be expensiveExplicit vote with randomized timeoutFast election based on highest zxid
Commit pathTwo round‑trips (unless leader stable)One round‑trip after leader electedOne round‑trip (leader → followers, ack)
Read modelTypically needs leader read (unless read‑only protocol)Leader or follower reads (if read‑only quorum)Followers serve reads locally
Typical use caseGlobal databases (Spanner)Configuration stores (etcd)Coordination services (ZooKeeper)
Message overhead2–3 messages per command (pre‑leader)2 messages per command2 messages per command (post‑leader)
ComplexityHigh (ballot management)Moderate (clear state diagram)Moderate (log replay logic)

ZAB’s read‑optimistic design is attractive for systems where reads dominate writes, such as metadata services that need to query node status frequently but only occasionally update configuration. However, ZAB’s reliance on a single leader for all writes can become a bottleneck under heavy write loads, a scenario where Paxos’s multi‑leader variants or Raft’s joint consensus extensions may be preferable.


Comparing the Three: Trade‑offs

To decide which algorithm best fits a given application, we must weigh several dimensions: fault tolerance, latency, throughput, operational complexity, and ecosystem support. The table below summarizes key metrics from published benchmarks and real‑world deployments.

MetricPaxos (Multi‑Paxos)RaftZAB
Typical quorum size⌈N/2⌉ + 1 (majority)Same as PaxosSame as Paxos
Write latency (99th‑pct)5–8 ms (datacenter)3–6 ms (LAN)4–7 ms (LAN)
Write throughput120 k CPS (10‑node)30 k CPS (3‑node)65 k CPS (5‑node)
Read pathLeader read or read‑only quorum (extra round‑trip)Leader or follower read (fast)Follower read (local)
Failure recoveryFast leader change after Prepare phase; requires 2‑phase round‑tripLeader election ~200 ms; log catch‑up may be neededLeader election ~200 ms; log replay from most up‑to‑date follower
Implementation complexityHigh (ballot handling, multiple phases)Moderate (clear state machine)Moderate (log synchronization)
EcosystemSpanner, CockroachDB, etc.etcd, Consul, TiKVZooKeeper, Kafka (controller)
Suitability for high‑write workloadsExcellent (leader can batch)Good (but limited by leader CPU)Moderate (leader may become bottleneck)
Suitability for read‑heavy workloadsNeeds read‑only quorum (extra latency)Followers can serve readsFollowers serve reads directly

Key takeaways:

  • If you need strict global consistency across geographically dispersed data centers, Paxos (especially Multi‑Paxos with TrueTime) remains the gold standard.
  • If you value operational simplicity and want a fast, deterministic leader, Raft is often the first choice, especially for microservice‑level coordination.
  • If your workload is read‑heavy and you already use a coordination service like ZooKeeper, ZAB offers a low‑latency read path with acceptable write performance.

Consensus in Swarm Intelligence: Bees and AI Agents

Nature provides elegant examples of consensus that predate any computer network. Honeybees use quorum sensing and the waggle dance to collectively decide on a new nest site. The process works like this:

  1. Scout bees explore and return with a “site advertisement” (a dance encoding direction and distance).
  2. Other scouts evaluate the dance; if they also favor the same site, they reinforce the dance.
  3. Once a quorum (typically 20–30% of scouts) gathers around a single site, the swarm commits to the move.

Mathematically, this mirrors a single‑value consensus where each scout is a node, the dance is a broadcast, and the quorum threshold determines safety. The decision time scales logarithmically with the number of scouts, and the system tolerates up to 50% “failed” scouts (e.g., those that never return).

Researchers have mapped these dynamics onto distributed algorithms:

Bee MechanismDistributed Analogy
Waggle dance (broadcast)Leader broadcast (ZAB, Raft)
Quorum thresholdMajority or configurable quorum
Reinforcement (re‑dance)Re‑transmission of missing log entries
Failure of scoutsNode crash or network partition

In self‑governing AI agents for Apiary, we can emulate bee consensus to let a fleet of autonomous pollinator drones decide where to allocate resources. Each drone periodically publishes a resource request (e.g., “need nectar at location X”). A lightweight consensus layer (perhaps a Raft‑based micro‑cluster) aggregates these requests, establishing a global schedule that respects the majority opinion while allowing individual drones to continue operating when disconnected.

Because bee consensus tolerates partial participation, a similar design can be built on top of ZAB’s fast leader election: a temporary loss of a subset of drones does not stall the entire scheduling service, as long as a majority of the control nodes remain reachable. This natural resilience is exactly what distributed consensus aims to provide.


Designing for Conservation Platforms: Choosing the Right Algorithm

For Apiary, the consensus layer sits beneath several critical services:

  1. Hive telemetry ingestion – thousands of temperature, humidity, and hive‑weight readings per minute.
  2. AI‑drone mission planning – a shared schedule of pollination routes that must be consistent across the fleet.
  3. Citizen‑science metadata store – a searchable catalog of observations contributed by volunteers.

Each service has a distinct consistency‑latency profile:

ServiceConsistency RequirementTypical Write RateLatency Budget
Telemetry ingestionEventual (minor out‑of‑order tolerated)10–30 k CPS≤ 20 ms
Mission planningStrong (no conflicting routes)≤ 1 k CPS≤ 10 ms
Metadata storeRead‑heavy, strong (searchable)≤ 5 k CPS≤ 5 ms for reads

A hybrid approach often works best:

  • Telemetry ingestion can be sharded across multiple Raft clusters, each handling a subset of sensor nodes. The clusters can be loosely coupled via an asynchronous replication pipeline, allowing high throughput while still guaranteeing per‑shard order.
  • Mission planning benefits from a single Raft or Paxos leader that holds the authoritative schedule. Because the write rate is low, the extra latency of a leader election is negligible, and the safety guarantees prevent duplicate or conflicting assignments.
  • Metadata store can leverage ZAB for its fast read path. Since most operations are reads, serving them from follower nodes reduces load on the leader and keeps user‑facing latency low.

Operational considerations:

  • Deployment topology: Place consensus nodes in geographically stable data centers (e.g., edge sites near large meadow reserves) to reduce latency for local sensors.
  • Failure domains: Use zone‑aware quorum (e.g., 2 out of 3 nodes in separate availability zones) to survive a full zone outage.
  • Monitoring: Implement health checks that track leader election frequency, log lag, and commit latency. Excessive leader churn often signals network instability that must be addressed.

By aligning each service’s workload with the protocol that best matches its performance envelope, Apiary can achieve both high reliability and responsive user experiences—the same way a bee colony balances rapid foraging with the safety of the hive.


Future Directions: Emerging Consensus Mechanisms

While Paxos, Raft, and ZAB dominate today’s production stacks, research continues to push the envelope:

Emerging ProtocolCore InnovationPotential Benefit
Fast Paxos (Lamport 2005)Reduces commit to a single round‑trip when a fast quorum (≥ ⌈3N/4⌉) is reachable.Up to 2× lower latency for write‑heavy workloads, at the cost of larger quorums.
Raft Joint Consensus (Ongaro 2015)Allows cluster configuration changes without downtime by temporarily running two overlapping consensus groups.Safer scaling and rolling upgrades of consensus clusters.
EPaxos (Moraru et al. 2013)Leaderless protocol where any node can propose; relies on conflict resolution via dependency graphs.Higher throughput under low contention, better suited for geo‑distributed workloads.
Blockchain‑style BFT (e.g., Tendermint, HotStuff)Byzantine fault tolerance (tolerates arbitrary malicious behavior) with deterministic finality.Useful when nodes may be compromised, such as public IoT devices in the field.
Hybrid Consensus (e.g., Paxos‑Raft combos)Dynamically switches between algorithms based on observed load and failure patterns.Adaptive performance, automatically balancing latency and safety.

For Apiary, a Byzantine‑tolerant variant may become relevant if the platform opens its API to untrusted third‑party drones that could be compromised. In that case, a Tendermint‑style BFT layer could guarantee safety even when up to one‑third of nodes act maliciously. However, BFT protocols typically incur higher message overhead (≈ 3 N messages per commit) and larger quorums, so they should be reserved for truly adversarial environments.


Why it matters

Consensus algorithms are the invisible glue that lets distributed systems act as a single, reliable entity. For a conservation platform like Apiary, they ensure that every temperature spike, every pollination route, and every citizen observation is recorded consistently, even when nodes crash, networks jitter, or a storm knocks a hive offline. By understanding the trade‑offs between Paxos, Raft, and ZAB, architects can tailor the system to the unique rhythm of the natural world—just as honeybees have evolved efficient quorum mechanisms over millennia. The right consensus choice translates directly into faster alerts for stressed hives, smoother coordination of AI pollinators, and a more trustworthy data set for researchers, ultimately helping us protect the pollinators that keep ecosystems—and our food supply—thriving.

Frequently asked
What is Consensus Algorithms in Distributed Systems about?
Distributed systems are everywhere: from the global databases that power e‑commerce, to the sensor networks that monitor wild‑flower habitats, to the fleets…
What should you know about introduction?
Distributed systems are everywhere: from the global databases that power e‑commerce, to the sensor networks that monitor wild‑flower habitats, to the fleets of autonomous drones that pollinate crops. In each of these settings a set of independent nodes must agree on a single sequence of events – a consensus – despite…
What should you know about foundations: Distributed Systems and the Need for Consensus?
A distributed system is a collection of processes that communicate only by sending messages over a network. This model introduces three fundamental sources of uncertainty:
What should you know about the Replicated State Machine Model?
The RSM abstraction was formalized by Lamport in his 1978 paper “Time, Clocks, and the Ordering of Events in a Distributed System.” An RSM consists of:
What should you know about classic Paxos – The Core Idea?
Paxos, introduced by Leslie Lamport in 1990, is often described as “the algorithm that works but is hard to understand.” The protocol revolves around three roles:
References & sources
  1. Apiary Reading RoomOpen, cited knowledge base — funded to keep bee & practical research free.
From the Apiary Reading Room. Opinion & editorial — not financial advice. We don't overclaim.
More from the Reading Room