Erlang
Cluster Simulation
Netsplit
Programming
Node Management

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.

Browse interview questions

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

  1. Artificial Network Partitioning: Use firewall rules to block traffic between certain nodes.
  2. 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

  1. 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.
  2. Applying Firewall Rules: Use a firewall management tool or commands (like iptables on 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:
bash
   sudo iptables -A INPUT -p tcp --dport 4369 -j DROP
   sudo iptables -A OUTPUT -p tcp --sport 4369 -j DROP

Using Erlang Functions

Erlang provides the functionality to disconnect nodes programmatically:

erlang
% In node A's shell
net_adm:ping('nodeB@hostname').
net_kernel:disconnect_node('nodeB@hostname').

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.

MethodDescriptionUse Case
Firewall RulesBlock ports using system-level firewall settingsLarge, realistic network conditions
Erlang Functions (disconnect)Programmatically disconnect nodesSmall, 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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions