...

/

Cluster Your BEAM Nodes Together

Cluster Your BEAM Nodes Together

Learn how to cluster your BEAM nodes together.

BEAM nodes

We must connect our BEAM nodes when deploying a real-time application to production. We covered in previous sections that a WebSocket- based application broadcasts outbound messages to all nodes in the cluster, using PubSub so that connections on other nodes receive the message for connections they own. If our nodes can not communicate, some messages will be missed and not sent to clients.

There are two ways to implement clustering in our application. The first way OTP comes out of the box is to connect nodes with distributed Erlang. This creates a direct peer-to-peer connection between all nodes in the cluster. However, some deployment environments may not network nodes this way due to connectivity restrictions. In these cases, it’s possible to use Redis as an alternative to clustering.

We’ll cover libraries that help us with clustering, and what to do if native clustering isn’t available to us.

Ready your application for clustering

One of the best things about Elixir/Erlang is that it comes with support for node-to-node connectivity out-of-the-box. In addition to being provided as a standard in the language, it works well.

The :net_kernel.connect_node/1 function connects the node that invoked it to a specified remote node. Once the TCP connection is established, the connection is kept fresh with a heartbeat and will automatically become disconnected if the nodes can no longer talk to each other. This comes out of the box, but there are a few things that we must set up to use it.

The first requirement of connecting nodes is to have our BEAM nodes talk over a given port and IP. We can hard-code the port that BEAM uses for distribution, or we can have the Erlang Port Mapper Daemon running and available between the remote nodes. ...