Understand consensus algorithms that keep distributed systems consistent. Covers Raft, Paxos, leader election, log replication, split-brain prevention, and the fundamental impossibility results that shape distributed system design.
Consensus algorithms solve the problem of achieving agreement among multiple nodes in a distributed system when individual nodes may fail, messages may be lost, or network partitions may occur. This matters when building systems that require reliable state coordination across multiple machines.
Use consensus when you need to maintain consistent state across multiple nodes and cannot tolerate divergent views of data. This includes:
Raft is a consensus algorithm designed for ease of understanding and implementation. It divides the consensus problem into three subproblems:
In Raft, each node can be in one of three states: follower, candidate, or leader.
To configure a Raft cluster, you typically define a set of peer addresses in your configuration:
raft.peers = ["node1:8080", "node2:8080", "node3:8080"]
raft.id = "node1"
raft.storage.path = "/var/lib/raft"
When a node starts, it begins as a follower. If no heartbeat is received from the leader within the election timeout, the node transitions to candidate and initiates an election.
If a majority of nodes vote for a candidate, that node becomes the leader. The leader then replicates log entries to followers.
For a Raft-based system, configure each node with:
# Example configuration file
[raft]
id = "node1"
peers = ["node1:8080", "node2:8080", "node3:8080"]
storage.path = "/var/lib/raft"
log.level = "info"
The node will attempt to join the existing cluster. If it's the first node, it creates a new cluster.
When leader election occurs:
If a node fails during election, it will miss heartbeat messages and trigger a new election cycle.
Once a leader is established:
Raft ensures several safety properties:
When network partitions occur, Raft handles them by:
Example error from a node in minority partition:
raft: failed to replicate entry to follower: rpc error: connection refused
If a node fails during election:
When nodes have divergent logs after a partition heals:
The election timeout is typically configured in milliseconds:
raft.election.timeout = 5000
If a node doesn't receive a heartbeat within this time, it starts an election.
Heartbeat interval is usually shorter than election timeout:
raft.heartbeat.interval = 1000
This controls how often leaders send heartbeats to followers.
Log entries are typically persisted to disk:
raft.log.persist = true
raft.log.storage.path = "/var/lib/raft/log"
Each node must be configured with a unique identifier and list of peers:
# Config file example
[raft]
id = "node2"
peers = ["node1:8080", "node2:8080", "node3:8080"]
storage.path = "/var/lib/raft"
The system will attempt to connect to all peers listed in the configuration.
To start a new cluster:
If multiple nodes are started simultaneously, the election process begins automatically.
When restarting a cluster:
If election timeouts are too short, the system will experience frequent elections:
raft.election.timeout = 1000
This can lead to instability if network conditions cause temporary delays.
If log persistence fails, nodes may lose committed entries:
raft.log.persist = false
This should be avoided in production systems.
When a new node joins the cluster, it must catch up on all committed entries. If this fails, it can cause consensus failures.
Raft requires a majority of nodes to form a quorum:
If fewer than the required number of nodes are available, the system cannot make progress.
Clients interact with the consensus system through a leader:
There may be a delay between when a request is made and when it's applied:
# Example response from cluster
{
"status": "success",
"committed_index": 100,
"applied_index": 95,
"timestamp": "2023-01-01T12:00:00Z"
}
The applied_index shows how far behind the node is in processing committed entries.
Key metrics to monitor include:
When a node fails:
When restarting a failed node:
[raft]
id = "node1"
peers = ["node1:8080"]
storage.path = "/var/lib/raft"
This creates a single-node cluster.
[raft]
id = "node2"
peers = ["node1:8080", "node2:8080", "node3:8080"]
storage.path = "/var/lib/raft"
election.timeout = 5000
heartbeat.interval = 1000
This configures a three-node cluster with standard timeouts.
The leader is responsible for:
Followers are responsible for:
When a node becomes a candidate:
Consensus implementations typically include:
The implementation should handle node failures gracefully and maintain consistency across all nodes in the cluster.
This page was rewritten on 15 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy