Distributed Erlang
Erlang makes certain types of distributed programming extremely easy; in no time at all, and with very little code, you can have a number of machines across a network happily chatting with one another. Two fundamental features of Erlang make this possible:
- Process communication by copying
- Location Transparency
Nodes and Clustering
Starting a Node
To start an Erlang node in distributed mode, you run erl
with one of the flags
-name
or -sname
:
# Used in normal environments with FQDN erl –name node_name # Used when FQDNs don't work erl –sname node_name
Nodes with short names and long names work in different communication modes and can't be part of the same Erlang cluster. All connected nodes must use the same mode. (Logan, Merritt, and Carlsson 2010, 194)
Connecting Nodes
Hidden Nodes
It's possible to connect clusters via special nodes to form a larger, not fully connected cluster. Such nodes are configured not to propagate information about other nodes, or even to be invisible to the other nodes, which can be useful for nonintrusive inspection of a cluster. (Logan, Merritt, and Carlsson 2010, 195)
EPMD
EPMD is the Erlang Port Mapper Daemon. Whenever you start a node, the node checks that EPMD is running on your local machine and starts it otherwise. EPMD keeps track of which nodes are running on the local machine and what ports they have been assigned. When an Erlang node on one machine wants to talk to a remote node, the local EPMD talks to the EPMD on the remote machine (by default using TCP/IP on port 4369) and asks if it has a node by that name up and running. If so, the remote EPMD replies with the port used for communicating directly with the remote node. But EPMDs never try to locate each other automatically - communication must always be triggered by one node looking for another. (Logan, Merritt, and Carlsson 2010, 197)
The Cookie Protection System
For two distributed Erlang nodes to communicate, they must have the same magic cookie. We can set the cookie in three ways:
- Store the same cookie in the file
$HOME/.erlang.cookie
. - Using the
-setcookie C
command line argument:
erl -setcookie <cookie>
- The BIF
erlang:set_cookie(node(), C)
sets the cookie of the local node to the atomC
.