How does Raft guarantee log consistency?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Raft is a consensus algorithm designed to be easy to understand and provides a more efficient approach to managing a replicated log than other algorithms like Paxos. It ensures that the log entries are replicated across the nodes in a cluster in a consistent and reliable manner—keeping data in sync across all the servers in the system. The key to Raft's operation and its ability to guarantee log consistency lies in its architecture and consensus protocol.
Key Components of Raft
Raft divides time into terms, where each term begins with an election to choose a leader. Once a leader is elected for the term, it remains in place until it fails or the term ends. The leader handles all client requests (appends to the log) and replicates them to the follower nodes. Here are some of the crucial components and states within Raft:
- Leader: Manages all client interactions and log replication
- Follower: Nodes that replicate logs and follow instructions from the leader
- Candidate: A node that has initiated an election to become the leader
- Log Entries: Comprise a command to be executed, and the term when the entry was received by the leader
Log Replication Process
To understand how log consistency is maintained, we should explore how log entries are replicated across the cluster:
- Log Entry Creation: The leader appends a new log entry in its log.
- Log Entry Replication: The leader sends this new log entry to all of the followers.
- Followers Append Entry: Each follower appends the entry and informs the leader of this action.
- Leader Waits for Majority: The leader waits until a majority of the followers acknowledge the entry.
- Log Entry Commit: Once a majority of followers have appended the log entry, the leader commits the entry and notifies followers to commit the entry.
Ensuring Consistency
Three primary mechanisms ensure consistency across all nodes in the Raft cluster:
- Leader Append-Only Rule: Leaders only append new entries to their logs never overwrite or delete entries. This simplifies log management and prevents discrepancies caused by conflicting entries.
- Log Matching Property: Raft maintains the invariant that if two logs contain an entry with the same index and term, then the logs are identical in all entries up to and including the index. This ensures that the leader and followers remain consistent with each other.
- Safety and Election Restriction: Raft ensures safety by the means that a candidate must have an updated log to be elected as leader. This implies that a node with the most complete log likely becomes the leader, ensuring the leader’s log consistency with the majority.
Handling Failures and Network Partitions
Raft ensures log consistency even in the face of network partitions and node failures:
- Election on Failure: If a leader fails, a new leader is elected from among the nodes with the most up-to-date logs.
- Log Cleanup on Leader Change: When a new leader comes to power, it forces all followers to duplicate its own log, thus erasing inconsistencies and divergences created by incomplete or conflicting log entries.
Summary Table
Here's a brief summary of the key points discussed:
| Aspect | Description |
| Leader Election | Ensures that the leader has the most up-to-date log entries. |
| Log Replication | New entries are replicated to all nodes, and only committed when a majority confirms reception and replication. |
| Failure Handling | Allows efficient recovery by promoting a new leader, ideally with the most complete log. |
| Consistency Rules | Leaders append only, logs match in terms of entries and terms, ensuring unified log states across the cluster. |
Conclusion
Raft's structured approach to time, leadership, and log management creates a robust system for ensuring log consistency across distributed systems. It manages to achieve this with less complexity and subtlety than many other consensus algorithms, making it both reliable and relatively straightforward to implement.
Related reading
- How does raft preserve safty when a leader commits a log entry and crashes before informing followers this commitment?
- How does raft prevent submitted logs from being overwritten
- How does tf.train.replica_device_setter work?
- How does the lock path parameter works in ZooKeeper (InterProcessMutex)?
- How does sorting a string in an array of strings and then sorting that array come out to be Oaslogalogs?
- How does stdsort work for list of pairs?
- How does the MapReduce sort algorithm work?
- How does vector clock work in leaderless (or peer-to-peer) architecture?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.