How do Raft guarantee consistency when network partition occurs?
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 for managing a replicated log across nodes of a distributed system. Its primary goal is to ensure system availability and data consistency, even in the face of network partitions. Here, we explore the mechanisms by which Raft guarantees consistency, particularly during such network partitions.
Raft Basics
Raft organizes system operations into terms, where each term starts with an election to choose a leader. The leader handles all client requests, log replication across the cluster, and log consistency enforcement. Below are some core features that help maintain consistency:
- Leader Election: At the start of a term, a leader is elected. Only one leader should be active at a time to prevent conflicts.
- Log Replication: The leader replicates its logs on the follower nodes. For a log entry to be committed, it must safely be stored on a majority (the quorum) of the nodes.
- Safety Mechanism: If two logs contain an entry with the same index and term, they store the same command.
Handling Network Partitions
Network partitions can isolate nodes or groups of nodes from each other, challenging the enforcement of consistency. Raft addresses this issue using the following features:
1. Leader Election and Quorums
During a partition, nodes might lose connection with the leader, or the leader itself could be part of the minority segment of the partition. In such cases, those nodes start a leader election. However, because Raft requires a majority of nodes (quorum) to elect a leader, no new leader will be elected if neither partition has more than half of the nodes. This ensures that there's only one active leader, or none, thereby preventing split-brain scenarios where two leaders believe they are in charge.
2. Log Commitment Rules
Raft only commits entries from the current leader’s term once a majority of the cluster has acknowledged them. This means that an entry from a term without a quorum cannot be committed, ensuring consistency across partitions. When the cluster reunites, the logs are reconciled. Any uncommitted entries from the old partitions will be overwritten with the new leader's log entries.
3. Term Numbers and Log Matching
Each Raft log entry includes the term number when the entry was received by the leader. The term numbers allow Raft to detect inconsistencies between logs and to ensure that the more up-to-date entries prevail in case of conflicts. This feature is crucial during a partition, where different nodes might have received different sets of updates. When partitions heal, the leader’s log dictates which entries are kept, guided by the highest term numbers.
Example
Consider a cluster of 5 nodes: A, B, C, D, and E. Suppose a partition occurs isolating A and B from C, D, and E:
- If A was originally the leader, A loses the majority quorum needed to make decisions.
- C, D, and E can elect a new leader among themselves (say, C becomes the leader).
- A and B, being a minority, cannot elect a new leader and thus become inaccessible in terms of processing write requests.
- Once the network is healed, nodes A and B will update their logs according to the entries in C, D, and E, led by the valid leader C.
Summary Table
| Feature | Description | Importance in Network Partitions |
| Leader Election | Ensures only one active leader through majority vote. | Prevents multiple leaders in split partitions. |
| Log Replication | Leader replicates log entries to the followers. | Ensures consistency when followers are reachable. |
| Log Commitment | Entries are committed only when a majority acknowledges them. | Prevents partial updates being committed during partitions. |
| Term Numbers | Tracks log entries with term numbers. | Resolves conflicts based on historical term information. |
Conclusion
Raft ensures consistency during network partitions by maintaining strict rules on leadership eligibility and log commitment. These mechanisms, complemented by term-based log management, safeguard the system against inconsistencies and data corruption during inevitable network failures and partitions in distributed systems.
Related reading
- How do raft nodes learn about peers?
- How do you set a default root object for subdirectories for a statically hosted website on Cloudfront?
- How do you use the MySQL replication driver in Grails on Tomcat?
- How does a new node join a group in the SWIM protocol?
- How do recommendation systems work?
- How do we achieve substring-match under On time?
- How does an odd number solve a split brain in a distributed system?
- How does any syncronous request works in asycrounous microservices enviorment?

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.