Consensus & coordination

Getting independent machines to agree on a single truth, even when some are dead or lying.

The idea

In distributed systems, there is no "global clock" or shared memory. If you need exactly one node to be the Leader (e.g., to process payments), they must agree via Consensus.

Consensus algorithms (like Raft or Paxos) rely on Quorums—a strict majority (e.g., 3 out of 5 nodes). If a candidate gets votes from a quorum, it becomes leader. Because any two quorums must overlap by at least one node, it's mathematically impossible for two nodes to win an election in the same term (preventing Split-Brain).

Quorum (3/5)
Cluster has 5 nodes. No leader elected yet.

How it works (Quorum Math)

def request_votes():
    votes = 1 # Vote for self
    cluster_size = 5
    quorum = (cluster_size // 2) + 1 # 3
    
    for peer in peers:
        if peer.request_vote() == "GRANTED":
            votes += 1
            
        if votes >= quorum:
            return "WON_ELECTION"
            
    return "LOST_ELECTION"