Concepts

Agreeing under failure

Consistency

Consensus

Getting several machines to agree on one value — a leader, a log entry, a lock — when any of them can crash and messages can be late. A majority is the whole idea.

A majority quorum survives minority failures and never splits, at the cost of a round trip to most of the cluster on every decision.

Raftleader electionquorumsplit brain

Try it

Move the dials — the sentence under the picture changes.
Partition
Click a node to crash itN1can commitN2can commitN3can commitN4can commitN5can commitA decision counts when 3 of 5 accept it. Two majorities always share a node, so two decisions cannot both count.
5 of 5 up, majority is 3: the cluster commits. It can lose 2 more before it stops.

In plain words

Some things must have exactly one answer across a whole cluster: which node is the leader, whether a lock is held, what the next entry in a shared log is. Consensus is how a group of machines agrees on one answer even when some of them crash, some are slow, and messages get delayed — and, crucially, even when nobody can tell which of those is happening.

The idea is smaller than its reputation

A decision counts only when a majority of nodes has accepted it. Five nodes: three must agree. That one rule does all the work, because:

n1n2n3n4n5Five nodes, all connected. Any three can decide.Partition: n1 and n2 on one side……n3, n4, n5 on the other. Three is a majority: this side keeps working.Two is not. This side can only wait — it cannot elect a leader or commit anything.Link restored: n1 and n2 learn what they missed. Nothing ever forked.
A split never produces two working halves, because at most one half can have a majority.

How Raft makes it concrete

Raft is the protocol you will most likely meet (etcd, Consul, CockroachDB). It turns "majority" into three moving parts: a leader, numbered terms, and a replicated log.

  1. One leader per term

    A term is a numbered period with at most one leader. Followers accept writes only from the leader of the current term.

  2. A write is committed when a majority has it

    The leader appends the entry to its log and sends it to everyone. Once a majority has written it to disk, it is committed and the client gets an answer. A committed entry can never be lost, because any future majority includes someone who has it.

  3. Heartbeats, then an election

    The leader sends heartbeats. If a follower hears nothing for an election timeout (150–300 ms, randomised), it starts a new term with a higher number and asks for votes. A majority of votes makes it leader. A node votes at most once per term — so two leaders in the same term are impossible.

  4. An old leader steps down

    A leader that was partitioned away still thinks it leads an old term. When it reconnects it hears the higher term number, steps down, and discards anything it accepted in isolation — nothing it did there was ever committed, because it could not reach a majority.

ClientLeader (n3)n4n5SET lock = heldappend entry 17 (term 4)append entry 17 (term 4)writtenleader + n4 = majority of 5 → committedOKn5's ack can arrive late; it does not matter
One write in Raft: the client is answered as soon as a majority has the entry on disk.
The heart of a Raft leader, simplifiedGo
func (r *Raft) propose(entry Entry) error {
    entry.Term = r.currentTerm
    r.log = append(r.log, entry)
    acks := 1                                   // the leader has it
    for _, peer := range r.peers {
        if r.sendAppend(peer, entry) {          // follower wrote it to disk
            acks++
        }
        if acks > len(r.peers)/2 {              // majority (peers excludes self)
            r.commitIndex = len(r.log) - 1
            return nil                          // safe to answer the client
        }
    }
    return ErrNoMajority                        // could not commit; do not answer OK
}

What it costs, and so where it is used

Every committed write is a round trip from the leader to a majority. Across a region that is a millisecond; across continents it is a hundred. So consensus is used for the small, critical things and not for the bulk data path:

Through consensus
small, must be right

Cluster membership. Who is leader. Locks and leases. Configuration. Schema changes. etcd, ZooKeeper and Consul exist to be this small, slow, correct core.

Not through consensus
big, can be a bit behind

User rows, events, cache entries — replicated the cheaper ways in replication, often with a consensus system holding just the "who is leader" decision.

Where it goes wrong

  • Losing the majority. Three of five down and the cluster stops — correctly. It cannot tell whether the missing three are dead or off deciding on their own. Add capacity before you lose it.
  • Slow disks. A commit waits for a majority to write to disk, so one slow disk in a three-node cluster is on the critical path half the time. Consensus clusters get the fast disks.
  • Reading from a follower. It may be behind. A read that must be fresh (linearisable) goes through the leader, or through a lease — a time-bounded promise from a majority that this node is still the leader.
  • Running it across regions without thinking. A 100 ms commit is fine for a config change and ruinous for a request path.

Take this with you

  • The one idea: a decision counts only when a majority accepts it, and two majorities always overlap — so there can never be two answers.
  • In an interview, explain majorities, odd cluster sizes, and why consensus is used for small things (leader, locks, config) rather than bulk data.
  • At work, keep the consensus cluster small, odd, and on fast disks — and never run one across a slow link without knowing what a commit costs.