How do raft nodes learn about peers?
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. It is used to ensure that multiple nodes (servers) in a distributed system agree on their shared state, even in the face of failures. One of the crucial elements in a Raft cluster is how nodes, known as "Raft nodes," discover and communicate with each other to maintain consensus. This process involves learning about peer nodes, handling dynamic changes in the cluster, and ensuring consistent system operation. Below, we explore the mechanisms and strategies employed by Raft nodes to learn about their peers.
Discovery of Peers in Raft
Static Configuration
Initially, the simplest method for Raft nodes to learn about each other is through static configuration. When setting up a Raft cluster, the configuration of all participating nodes is typically predefined. This configuration includes the IP addresses and ports of all peers.
This setup is conducive for environments where the network configuration is stable. However, it lacks flexibility in dynamic environments where nodes might come and go due to failures or scaling operations.
Dynamic Peer Discovery
To address the limitations of static configuration, Raft can be enhanced with mechanisms for dynamic peer discovery. This might include integration with service discovery systems such as Consul, ZooKeeper, or Etcd. These systems maintain a registry of service instances and their network locations, updating this registry as instances change due to scaling, failures, or other operational reasons.
When a Raft node starts, it queries the service discovery system to fetch the current list of peers. This approach allows the cluster configuration to be updated dynamically as the state of the service instances changes.
Membership Changes
Raft handles changes to the cluster membership (i.e., nodes being added or removed) using a two-phase approach:
- Joint Consensus: During the first phase, the cluster operates in a transitional configuration that includes both old and new members. This ensures that the system continues to operate normally during the change.
- New Configuration: Once the joint consensus phase has been safely navigated, the cluster moves to the new configuration which excludes departed nodes and includes new ones.
Log Replication
Once nodes are aware of their peers, they communicate primarily through log replication. The leader node, which is elected among the nodes, takes charge of managing the replicated log. It sends out AppendEntries RPCs to follower nodes containing log entries to be replicated. This process ensures that all nodes in the cluster maintain identical copies of the log, thus preserving the consistency of the system's state.
Examples:
In the example above, a static configuration for a Raft cluster with three nodes is shown in JSON format, each node identified by an id and an address.
Failure Handling
Raft is designed to handle failures gracefully. If a peer node fails (detected through a timeout mechanism), the leader will attempt to reestablish communication and continue log replication once the node comes back online. If the leader itself fails, the remaining nodes will hold a new election to choose a successor.
Raft and Real-world Applications
Various distributed systems use Raft for managing cluster state robustly. For example, Etcd, a key-value store widely used in Kubernetes for storing configuration data and state, implements Raft for data replication.
The table below summarizes the methods through which Raft nodes learn about and manage their peers:
| Method | Description |
| Static Configuration | Nodes are pre-configured with the addresses of their peers. |
| Dynamic Peer Discovery | Nodes use external service discovery systems to dynamically update their list of peers. |
| Membership Changes | Nodes handle additions and removals of peers through a two-phase approach for safe transitions. |
| Log Replication | Leader node replicates log entries to follower nodes to ensure state consistency across the cluster. |
| Failure Handling | Nodes handle failures by reestablishing connections and through leader reelection. |
In conclusion, Raft nodes learn about their peers mainly through initial static configurations or dynamic systems integrated with service discovery platforms. They maintain robustness and consistency through log replication, handle membership changes meticulously, and manage failures effectively to ensure the distributed system remains functional and consistent.
Related reading
- 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 does an odd number solve a split brain in a distributed system?
- How do SO_REUSEADDR and SO_REUSEPORT differ?
- How do you add CloudFront in front of API Gateway
- How do recommendation systems work?
- How do we achieve substring-match under On time?

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.