Autoreconnect nodes

Is there a best practise for keeping two nodes connected?

How do I detect when a node has disconnected and automatically try to reconnect?

I see a Node.monitor function but the documentation doesn’t really explain how to use it or what it does.

2 Likes

BEAM nodes connect to each other when a message is sent between them. Due to this, disconnected nodes automatically try to reconnect as long as messages are still pending for the disconnected node, there is no additional action needed from your side.

The best practice for keeping two nodes connected is to have TCP bonding through different NICs at the OS level.

Regarding Node.monitor : the docs say to read erlang:monitor_node/1. monitor_node/1 document says that the process calling this function would be sent a {:nodedown, node} message when the node goes down. You either have a specific receive clause for this message or handle it in GenServer.handle_info/2.

In general you monitor a node only if you have something specific you need to do should that specific node go down (e.g. resource clean-up). Typically it isn’t common to require monitoring a node.

3 Likes