RAFT term condition to commit an entry
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The RAFT consensus algorithm is widely used for managing a replicated log in distributed systems. It achieves consensus by ensuring that all the changes to the replicated log are made in a safe, consistent manner, even in the event of network failures or node crashes. One critical aspect of RAFT's design is the term condition to commit an entry. This describes how log entries are replicated across nodes and the conditions under which these entries are considered committed.
How RAFT Works
At a high level, RAFT organizes the operational structure of a distributed system into terms. Each term begins with an election, in which one of the nodes becomes the leader. The rest of the nodes become followers. The leader handles all client requests. If the leader crashes or becomes unreachable, a new leader is elected. Each entry in the leader's log has an index and a term number. The term numbers in the logs allow RAFT to detect inconsistencies between copies of the log and ensure that logs on different nodes are eventually consistent.
The Commit Process in RAFT
An entry from a client is first appended to the leader's log. This entry is considered uncommitted initially. To commit the entry, the following process occurs:
- Replication: The leader replicates the log entry to the follower nodes.
- Acknowledgment: The follower nodes respond to the leader after they have added the entry to their logs.
- Committing The Entry: Once the leader has received acknowledgment from the majority of the nodes ("majority consensus"), the log entry is marked as committed by the leader, and then applied to the state machine.
The process ensures that the system maintains consistency: a log entry committed in this way will be present in the logs of a majority of the nodes.
Safety and Consistency
The design of RAFT ensures that committed entries are durable and eventually replicated across all nodes. This allows RAFT to ensure several important properties:
- Election Safety: At most one leader can be elected in a given term.
- Leader Append-Only: A leader can only append new entries to its log; it cannot rewrite or delete entries.
- Log Matching: 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 given index.
- Leader Completeness: If a log entry is committed in a given term, then that entry will be present in the logs of the leaders for all higher-numbered terms.
Example of Committing an Entry
Consider a RAFT cluster with five nodes, labeled A through E. Suppose node A is the leader and receives a client request to add an entry. The procedure might look something like this:
- Step 1: Node A adds the entry at index 5 with term 3 to its log.
- Step 2: Node A sends AppendEntries RPCs to nodes B, C, D, and E.
- Step 3: Nodes B, C, and D respond affirmatively after adding the entry to their logs, while E fails to respond.
- Step 4: Once affirmations are received from the majority (i.e., A, B, C, D), the entry is committed on node A and an acknowledgment is sent back to the client. Node A also sends a message to all nodes to commit the entry.
Summary Table
| Term | Action | Description |
| 1 | Leader Election | A leader is elected among the cluster nodes. |
| 2 | Log Entry Creation | Leader appends the entry to its log. |
| 3 | Log Entry Replication | Entry is sent to follower nodes. |
| 4 | Acknowledgment from Majority | Leader receives confirmation from a majority. |
| 5 | Entry Commitment | Entry is committed and applied to the state machine. |
Conclusion
RAFT's term condition to commit an entry provides a robust mechanism for ensuring that a distributed system can continue operations despite failures. By waiting for a majority to store a log entry before it is committed, RAFT ensures that every committed entry can be recovered and remains consistent across partial system failures, preserving the integrity and availability of the distributed system.
Related reading
- RAFT What happens when Leader change during operation
- Rails Postgresql replication via Octopus gem when in Development env
- Randomly generated group id for Kafka Consumer
- Re-Consume Kafka messages from a given time
- Ramer-Douglas-Peucker path simplification algorithm
- Random-first search?
- Re-doing a reverted merge in Git
- Rebase feature branch onto another feature branch

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.