How to simulate temporary netsplit in erlang cluster from local nodes?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Simulating a netsplit in an Erlang cluster can be essential for testing the resiliency and fault tolerance of your distributed systems. Erlang/OTP clustering is built to handle failures and network partitions gracefully, but it’s crucial to ensure that your application behaves as expected under such circumstances.
Understanding Erlang Clustering
Before diving into netsplit simulation, let’s understand the basics of an Erlang cluster:
- Nodes: In Erlang, a node is an instance of the Erlang runtime system (ERTS). Nodes can communicate with each other using distributed Erlang messaging.
- Cluster: A cluster refers to a group of interconnected Erlang nodes that can communicate with each other.
- Netsplit: This occurs when there is a network failure between nodes, causing some nodes to become unreachable from others. This split can result in the cluster being divided into two or more separate sub-clusters.
Techniques to Simulate Netsplit
- Artificial Network Partitioning: Use firewall rules to block traffic between certain nodes.
- Controlling Inter-node Communication: Use Erlang’s built-in capabilities to control communication between nodes.
Step-by-Step Guide to Simulate Netsplit in Erlang
Using Firewall Rules
- Identifying Node IPs: First, you need to find out the IP addresses of the nodes involved in the cluster. This can usually be done by executing
inet:getifaddrs()in the Erlang shell on each node. - Applying Firewall Rules: Use a firewall management tool or commands (like
iptableson Linux) to block the ports used by Erlang for inter-node communication. By default, Erlang uses port range 4369 for EPMD (Erlang Port Mapper Daemon) and dynamically allocated high-numbered ports for node-to-node communication.Example:
Using Erlang Functions
Erlang provides the functionality to disconnect nodes programmatically:
This method does not simulate network conditions but forcefully disconnects nodes within the Erlang virtual machine, which is suitable for controlled environments.
Monitoring and Observing the Netsplit
Once a netsplit has been simulated, it’s crucial to monitor how the nodes behave. This can be done by:
- Observing the logs for errors or warnings.
- Using
net_adm:world()which will attempt to connect to all known nodes and can be used to verify which nodes are currently seen as connected from a node's perspective. - Checking the clusters status using the
nodes()command in Erlang’s shell.
Recovery from Netsplit
After testing, you should restore the network connectivity or remove the rules you applied to simulate the netsplit. This can typically be done by reversing the firewall rules or reconnecting the nodes using Erlang commands.
| Method | Description | Use Case |
| Firewall Rules | Block ports using system-level firewall settings | Large, realistic network conditions |
Erlang Functions (disconnect) | Programmatically disconnect nodes | Small, controlled experiments |
Conclusion
Simulating a netsplit in an Erlang cluster allows developers and system architects to observe and improve the behavior of distributed applications under failure conditions. By employing either system-level manipulations (like firewall rules) or programming-based disconnections, you can ensure that your application is robust and resilient enough for production environments.
By incorporating these techniques into your testing procedures, you can catch potential bugs and address issues before they impact your users, thereby enhancing the reliability of your Erlang applications in real-world scenarios.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.