Replicated state machines and consensus logs
A way to keep several servers in step: a consensus protocol agrees on one ordered log of commands, and each server applies it to a deterministic state machine, so committed writes outlive a crashed node.
What it is
A replicated state machine is the same deterministic program running on several independent servers, fed the same inputs in the same order. Deterministic here has a precise meaning: copies that start in the same state and receive the same inputs in the same order end in the same state and produce the same outputs. The recipe, which Lamport proposed and Schneider elaborated in a 1990 tutorial, is to place copies of the machine on separate servers, treat client requests as inputs, choose one ordering for those inputs, execute them in that order on each server, and answer clients with the output. Choosing the ordering is the hard step, and one standard way to do it is to let each input's position in a series of consensus decisions define the order.
In Raft that series of decisions is a replicated log. Each incoming command is stored as a log entry, the log is copied to the other servers by a consensus module, and a command is applied to the state machine once it has been safely replicated. One server is the leader: it accepts client requests and drives replication, and a new leader is chosen if it fails. Each entry carries an index, a term number and the command. The safety property is stated per log slot: if one machine applies a given command at a given index, no other machine applies a different command there. The group stays available while any majority of its servers are up.
What committed means. The leader appends the command to its own log and sends it to the other servers in AppendEntries requests. The entry is committed once the leader has replicated it to a majority during its current term; the leader then applies it to its local state machine, returns the result to the client, and tells the other servers about the new commit point in later AppendEntries requests. HashiCorp's Consul 1.22 documentation describes the same sequence for its Raft group: the leader writes the entry to durable storage and replicates to a quorum of followers, the entry is committed when a quorum holds it durably, and the client's write is held until the entry is both committed and applied. A quorum is a majority, more than half of the servers: 2 of 3 or 3 of 5, so 3 servers tolerate one failure and 5 tolerate two, and the recommended production size is 3 or 5.
Followers trail the leader. Followers learn the commit point from the leader's later AppendEntries messages and heartbeats, and a follower applies an entry to its own state machine once it learns the entry is committed. There is a window, then, in which the leader has applied an entry and some followers have not, and a follower that falls far enough behind is brought up to date by a snapshot from the leader rather than entry by entry. So the replicas agree on the sequence of states, not on holding the same state at the same instant. In Consul, queries go to the leader when stale reads are not acceptable, which is the practical consequence.
| What a Raft server keeps | What it is for |
|---|---|
Log entries: index, term, command | The agreed order; if two logs share an entry's index and term, they hold the same command and match up through that entry |
Current term and most recent vote, on durable storage | Recovered after a crash so the server can take part in elections safely |
The log itself, on durable storage | A server that loses its durable state can no longer take part in the cluster |
A snapshot with its last included index and term | The state after compaction, positioned where the discarded entries ended |
Order comes from the log, not from clocks. Raft's rules about order are written in indexes and terms: Log Matching says two logs that share an index and term agree on that entry and on all earlier ones, and State Machine Safety says no two servers apply different commands at the same index. Time does appear in Raft, but in elections and heartbeats: a follower expects a heartbeat within a timeout, often 150 to 300 ms, and the timing requirements govern how steadily a leader is kept, which is an availability concern. Our reading: nothing in the ordering rules consults a wall clock, so "the state at a point in time" means "the state after applying entries 1 through N". Mapping a wall-clock moment to an index is the application's job: have the leader stamp a timestamp into the command payload before it appends the entry, so that each replica applies the same value, and treat the index as the authoritative order with the timestamp as a label on it. Reading a local clock at apply time gives values the replicas have no reason to agree on, which would break determinism. See clocks and ordering for why the two disagree.
The log is not an archive. Log compaction works by snapshots: each server writes its state, as of the committed entries, to stable storage together with the index and term of the last entry the snapshot covers, and drops the entries the snapshot supersedes. The leader also ships a snapshot to a server whose log has fallen behind, and that server discards the part of its log the snapshot replaces. The reason is practical: compaction keeps the log from growing without bound and keeps the cost of replicating it to other nodes down. A replica's log therefore normally holds just recent entries. History that must be kept, such as a record of transfers an auditor can check, needs its own store with its own retention rule, fed by the state machine as it applies entries. See event sourcing and ledgers for how that store is built.
When to use it
Reach for a replicated state machine when a small, critical piece of state must survive the loss of a server and still give one answer: who holds a lock, who is leader, the cluster configuration, which node owns which shard. Consul is a worked example: its server nodes form the Raft peer set that holds its catalog, while thousands of client agents stay outside the peer set so that scale does not slow consensus. The same machinery suits a wallet shard's balances and recent transfers: the group keeps serving while a majority of its servers are up, so losing one of the nodes that hold the records costs you at most a leader election, if the lost node was the leader, rather than data.
Use client serial numbers when a retried command must not apply twice. If the leader crashes while handling a request, the client retries with another server until it succeeds, and without care the command could execute more than once. The fix is a unique serial number on each request, a client id plus a request number, reused on retry; the servers keep the latest serial and its response for each client in durable storage, and when the leader sees a duplicate it returns the stored response instead of executing again. It is the same idea as idempotency keys, kept inside the replicated state so it survives failover too.
When not to
Consensus costs a round trip to a majority on each write, and in Raft writes and replication pass through a single leader, which can make that leader a bottleneck. It is a poor fit for high-volume data that tolerates some disagreement. Caches, metrics, feeds and most user content do better with replication and read replicas or quorums and tunable consistency, which trade staleness for throughput. Keep the consensus group for coordination state and the records that must give one answer, not for the bulk data beside them.
Don't route reads through the leader when a slightly old answer is fine. As noted above, leader-served queries are the mode for when stale reads are not acceptable; a dashboard or a count can take a cheaper path, a balance check that decides a transfer should not. And don't adopt a 3 or 5 node consensus group when one durable database with backups meets the requirement. A single server is the simplest way to run a service and is as fault tolerant as the machine under it, and one Raft-based product documents single-server deployments as suited to development, which is a fair rule for your own designs too.
Compared
| Option | What it gives | What it costs | When it is the right choice |
|---|---|---|---|
Consensus log plus state machine (Raft, Paxos) | One agreed order; committed entries survive while a majority is up | A majority round trip per write, a single leader, several servers to run and keep healthy | Small critical state that must give one answer after a node dies |
One server with durable storage | The simplest thing to run and reason about | Down while that server is down; as fault tolerant as its hardware | Development, internal tools, anything a backup restore covers |
Reads from a follower, or without the log | No leader round trip; spreads read load | May return state older than the leader's | Dashboards, counts and anything that tolerates a slightly old answer |
Append-only ledger beside the consensus group | Full history that outlives compaction | Another store to run and to reconcile against current state | Audit, reproducible past balances, long retention |
In a picture
Step 1 of 4
The leader appends
A client sends a command to the leader, which appends it to its own log as a new entry carrying the current term.