[Cluster] 1. RAFT Algorithm: A Complete Evolution from Single Node to Distributed Consensus
[Cluster] 1. RAFT Algorithm: A Complete Evolution from Single Node to Distributed Consensus
Introduction
- RAFT (Raft Consensus Algorithm) is a distributed consensus algorithm designed to solve the problem of achieving data state agreement among multiple nodes in distributed systems.
- Compared to the renowned Paxos algorithm, RAFT’s design philosophy emphasizes “understandability” through clear role separation and straightforward state transitions, making it easier for developers to comprehend and implement.
- This article demonstrates the complete evolution of the RAFT algorithm from single-node to multi-node clusters through 11 detailed diagrams, covering key scenarios including normal operation, failure handling, network partitions, and conflict resolution.
Core RAFT Concepts
- Before diving into the analysis, let’s familiarize ourselves with several core concepts of RAFT:
Node States:
- Leader: Handles client requests and replicates log entries to other nodes
- Follower: Passively receives log replication requests from the Leader
- Candidate: Temporary state during the Leader election process
Key Data Structures:
- Log: Ordered sequence storing operation commands
- Term: Monotonically increasing logical clock used to detect stale information
- CommitIndex: Index of the highest log entry known to be committed
- ApplyIndex/LastApplied: Index of the highest log entry applied to the state machine
- State: Actual business data state
Stage 1: Single Node Startup (Diagram 1)
|
|
In a single-node cluster, the node automatically becomes the Leader as it constitutes a “majority” (1 > 1/2). This illustrates a crucial property of the RAFT algorithm: at most one Leader can exist at any given moment.
Stage 2: Handling Client Requests (Diagram 2)
|
|
This demonstrates RAFT’s fundamental workflow: log append → replicate → commit → apply. In single-node scenarios, this process completes instantaneously.
Stage 3: Nodes Joining the Cluster (Diagram 3)
|
|
New nodes join in Follower state and require synchronization of historical data from the Leader. This demonstrates RAFT’s dynamic membership change capability.
Stage 4: Log Synchronization (Diagram 4)
|
|
This stage showcases RAFT’s core mechanism: log replication. The Leader ensures all Followers maintain log consistency with itself.
Stage 5: Cluster Expansion (Diagram 5)
|
|
Clusters can expand dynamically, with new nodes automatically completing historical data synchronization upon joining.
Stage 6: Batch Operations and Failures (Diagram 6 & 6-Result)
Diagram 6 illustrates the problem scenario:
|
|
Diagram 6-Result shows recovery state:
|
|
This demonstrates RAFT’s fault tolerance: the cluster continues operating as long as a majority of nodes remain functional.
Stage 7: Election Failure Scenario (Diagram 7)
|
|
This illustrates RAFT’s network partition handling mechanism: nodes with newer logs are more likely to become the new Leader.
Stage 8: Election Within Partition (Diagram 8)
|
|
- The partitioned old Leader still processes requests, but these operations will not be committed due to inability to obtain majority confirmation.
Stage 9: New Leader Established with Existing Partition (Diagram 9)
|
|
This proves RAFT’s partition tolerance: even with some unreachable nodes, the majority can continue functioning normally.
Stage 10: Conflict Detection (Diagram 10)
|
|
This state demonstrates the split-brain problem: the system contains both new and old Leader states.
Stage 11: Partition Healing and Conflict Resolution (Diagram 11)
|
|
This represents the classic log conflict resolution scenario in RAFT:
- Higher term Leaders are authoritative
- Operations not confirmed by majority are discarded
- All nodes eventually achieve strong consistency
Key Characteristics Analysis of RAFT Algorithm
Through these 11 diagrams, we can summarize the important characteristics of RAFT:
1. Strong Consistency
- All nodes eventually reach identical states
- Data reliability ensured through majority commit
2. Partition Tolerance
- During network partitions, majority partition continues service
- Minority partition cannot handle write operations
3. Leader Election
- At most one Leader at any given time
- Latest nodes elected through term and log comparison
4. Log Replication
- Leader replicates logs to all Followers
- Ensures log ordering and consistency
5. Failure Recovery
- Nodes can automatically synchronize after rejoining from failure
- Conflicting logs are correctly overwritten
Practical Application Considerations
Performance Characteristics:
- Write operations require majority confirmation, resulting in higher latency
- Read operations can be performed from Leader or Followers
- Network partitions affect availability
Suitable Use Cases:
- Configuration management systems (such as etcd)
- Distributed databases (such as TiKV)
- Distributed lock services
Important Considerations:
- Odd-numbered node clusters are preferable (avoid split-brain)
- Network quality significantly impacts performance
- Need to consider safety of membership changes
Conclusion
The RAFT algorithm elegantly solves distributed consensus problems through clear role separation and simple rules. This article comprehensively demonstrates the progression from single-node to complex failure scenarios through 11 progressive diagrams. Understanding these scenarios is crucial for designing and implementing reliable distributed systems.
RAFT’s success lies in its understandability: compared to Paxos’s complex proofs, RAFT employs intuitive concepts (terms, elections, log replication) that enable developers to truly comprehend and correctly implement distributed consistency systems.