What Is Distributed Consensus, and Why Is It So Hard?

8 minute read

Imagine three replicas of a small configuration service. One of them must act as the leader and accept writes.

Simple enough, until the network between them gets slow.

Replica A stops hearing from replica B and decides B is dead. Replica B, which is perfectly healthy, stops hearing from A and reaches the same conclusion about A. Now both believe they should be the leader. Both accept writes. The data diverges, and nothing in the system can tell which version is “correct”.

This is split brain, and preventing it is the job of a consensus protocol.

Consensus is the problem of getting a group of processes to agree on a single value, even when some of them crash and the network delays, drops or reorders messages. It sounds modest. It turns out to be one of the deepest problems in distributed computing, and it sits underneath almost every reliable system you use: etcd and ZooKeeper, Kafka’s controller, CockroachDB and Spanner, and every “leader election” you’ve ever relied on.

In this article, we’ll look at what consensus actually guarantees, why it is provably hard, and what it costs to run in production.

1. Defining the problem precisely

Informally, consensus means “everyone agrees”. Formally, a consensus algorithm lets each process propose a value and eventually decide on one, while satisfying three properties:

Agreement

No two correct processes decide different values.

Validity

The decided value must have been proposed by some process. An algorithm that always decides 42, regardless of input, is useless, and validity rules it out.

Termination

Every correct process eventually decides.

Agreement and validity are safety properties: they say that nothing bad ever happens. Termination is a liveness property: it says that something good eventually happens.

That distinction matters a great deal. As we’ll see, practical algorithms never compromise on safety, but they are allowed to stall under bad enough conditions.

2. Failure models: what can go wrong?

How hard consensus is depends on what kinds of failures we expect.

Crash-stop

A process runs correctly until it halts, and then never comes back.

Crash-recovery

A process can crash and later restart, possibly having lost whatever wasn’t written to durable storage. This is the realistic model for most servers, and it’s why consensus implementations are obsessive about fsync.

Omission

A process or link silently drops some messages.

Byzantine

A process can behave arbitrarily: lie, send conflicting messages to different peers, or collude with others. This covers bugs, corrupted memory and malicious actors.

The failure model directly sets the cluster size you need:

Failure model Nodes needed to tolerate f failures
Crash / crash-recovery 2f + 1
Byzantine 3f + 1

Most infrastructure inside a single organization (etcd, ZooKeeper, Raft-based databases) assumes non-Byzantine failures. Byzantine fault tolerance is far more expensive, and it shows up mainly in blockchains and some safety-critical systems.

3. Timing models and the FLP impossibility result

The second dimension is time.

Synchronous systems have known upper bounds on message delay and processing speed. If a message hasn’t arrived within the bound, the sender has definitely failed.

Asynchronous systems have no bounds at all. A message might take a millisecond or an hour. Crucially, a slow process is indistinguishable from a crashed one.

In 1985, Fischer, Lynch and Paterson proved a result that shaped the whole field:

In a fully asynchronous system, no deterministic algorithm can guarantee consensus if even one process may crash.

This is the FLP impossibility result. The intuition is that the algorithm can never safely tell “slow” from “dead”. There is always some adversarial scheduling of message delays that keeps the system undecided forever.

Researchers also mapped which combinations of assumptions make consensus solvable:

Conditions under which distributed consensus is possible When is consensus possible? Results from Dolev, Dwork and Stockmeyer, as presented in Tanenbaum & van Steen’s Distributed Systems. Slide: ECE 454/751, University of Waterloo (Dr. Wojciech Golab).

The table shows how quickly consensus becomes impossible once you remove guarantees about timing or message ordering.

How real systems escape FLP

Production systems run on networks that are mostly well behaved. They use the partially synchronous model: the network may be arbitrarily slow for a while, but it eventually stabilizes.

Under that model, algorithms like Paxos and Raft make a deliberate trade:

  • Safety always holds, no matter how bad the network gets.
  • Liveness holds only when the network is behaving: elections succeed and progress resumes once messages flow again.

They use timeouts as imperfect failure detectors. A wrong timeout can cause an unnecessary election, which costs availability. It can never cause two leaders to commit conflicting values, because that would violate safety.

4. Why majority quorums work

Recall the naive approach from the introduction: each node decides on its own whether it’s the leader. It fails because two nodes can make independent decisions on disjoint information.

Consensus protocols fix this with quorums: a decision is only valid if a majority of nodes participated in it.

// Quorum returns the minimum number of nodes that must agree
// for a decision to be valid in a cluster of size n.
func Quorum(n int) int {
    return n/2 + 1
}

// FaultTolerance returns how many nodes can fail while the
// cluster can still form a quorum.
func FaultTolerance(n int) int {
    return n - Quorum(n)
}

The key property is intersection: any two majorities of the same cluster share at least one node.

In a five-node cluster, a quorum is three. If one leader was elected by {A, B, C} and a competing candidate tries to win with {C, D, E}, node C is in both groups. C will refuse to vote twice in the same election round (or term), so the second candidate cannot win.

That single overlapping node is what carries information from one decision to the next, and it’s the foundation of every crash-fault-tolerant consensus protocol.

It also explains a rule of thumb that surprises people:

Cluster size Quorum Failures tolerated
3 2 1
4 3 1
5 3 2
6 4 2
7 4 3

Even-sized clusters buy you nothing. A four-node cluster tolerates exactly as many failures as a three-node cluster, while adding latency and another machine that can fail. This is why consensus clusters are almost always 3 or 5 nodes.

5. Consensus isn’t enough on its own: fencing

Even with a correct consensus protocol, the clients of the system can still cause split brain.

Consider a distributed lock built on etcd or ZooKeeper:

  1. Worker 1 acquires the lock with a 10-second lease.
  2. Worker 1 hits a long garbage-collection pause.
  3. The lease expires and Worker 2 acquires the lock.
  4. Worker 1 wakes up, still believing it holds the lock, and writes to storage.

The consensus system did its job perfectly. The bug is that the storage layer has no idea which lock holder is current.

The fix is a fencing token: a number that increases every time the lock is granted (etcd’s revision numbers and ZooKeeper’s zxid work well). Every write carries the token, and the storage layer rejects anything older than the newest token it has seen.

type FencedStore struct {
    mu        sync.Mutex
    highToken uint64
    data      map[string]string
}

var ErrStaleToken = errors.New("stale fencing token: lock was lost")

func (s *FencedStore) Put(token uint64, key, value string) error {
    s.mu.Lock()
    defer s.mu.Unlock()

    if token < s.highToken {
        return ErrStaleToken
    }

    s.highToken = token
    s.data[key] = value
    return nil
}

Worker 1’s delayed write arrives with token 33 after Worker 2 has already written with token 34, so it is rejected.

The broader lesson: consensus gives you agreement inside the cluster. Extending that guarantee to the rest of the system is your responsibility.

6. Where consensus shows up

You rarely implement consensus yourself, but you depend on it constantly:

  • Coordination services: etcd (Raft), ZooKeeper (Zab) and Consul (Raft) provide leader election, distributed locks, service discovery and configuration.
  • Kubernetes: every object in the cluster is stored in etcd. When etcd loses quorum, the control plane stops accepting changes.
  • Messaging: Kafka’s KRaft mode replaced ZooKeeper with a Raft-based controller quorum.
  • Databases: CockroachDB and TiKV run a Raft group per data range, and Google Spanner runs Paxos groups per shard.

7. What consensus costs in production

Consensus buys strong guarantees, and it charges for them.

Latency

Every committed write needs at least one round trip from the leader to a majority, plus a durable disk write on each of them. Within a region that’s typically a few milliseconds. Across regions it’s tens to hundreds of milliseconds, so geography is a first-order design decision.

Throughput

In leader-based protocols, every write flows through a single node. To scale beyond that, systems split data into many independent consensus groups (CockroachDB’s ranges, Spanner’s splits) instead of making one group bigger.

Availability under partitions

A partitioned minority must stop accepting writes. That is the CAP theorem in action: when the network partitions, a consensus system chooses consistency over availability.

Operational sensitivity

Slow disks, noisy neighbours and long GC pauses all show up as leader elections. etcd’s own tuning guide singles out disk write latency as the most common cause of instability, which is why it recommends dedicated SSDs.

8. What this means for production systems

A few practical rules follow from all this:

  • Don’t implement consensus yourself unless that is your product. Use etcd, ZooKeeper or a well-tested Raft library.
  • Keep consensus off the hot path. Use it for metadata, leadership and configuration, not for every user request.
  • Run 3 or 5 nodes, spread across failure domains such as availability zones.
  • Use fencing tokens whenever a lease or lock protects a side effect outside the consensus system.
  • Monitor the signals that predict trouble: leader changes, proposal commit latency, and disk fsync duration.

Conclusion

Consensus is how a distributed system turns a collection of unreliable machines into something that behaves like one reliable one.

It is provably impossible in a fully asynchronous world, so practical protocols make a careful trade: they never violate safety, and they make progress whenever the network is cooperative. Majority quorums, and the guaranteed overlap between them, are what make that possible.

Understanding these foundations makes the behaviour of real systems less mysterious. You can see why etcd refuses writes during a partition, why your cluster has five nodes and not four, and why a lock alone doesn’t protect your data.

What’s next?

In the next post, we’ll look at the consensus algorithm behind etcd, Consul and CockroachDB:

Raft: Understanding Consensus Through Leader Election and Log Replication

We’ll walk through Raft’s election and replication rules with Go code, and look at what breaks under real network partitions.


Further reading:

Comments