Distribution is built into the language rather than added by a library, and the headline is location transparency: Pid ! Message works identically whether the pid is on this node or another machine. You do not change the code to distribute it.
A node is a running BEAM with a name. -sname bank gives a short name within one DNS domain; -name bank@host.example.com gives a long one for across domains, and the two cannot talk to each other. Nodes find each other through the epmd daemon on port 4369, and connecting is implicit: net_adm:ping('other@host') or simply sending a message to a remote process brings the connection up. Connections are transitive by default - connect to one node in a cluster and you connect to all of them, which is a full mesh and is why Erlang clustering does not scale past a few dozen nodes without help.
Security is the part to take seriously. Authentication is a shared secret called a cookie, exchanged in the clear, and any node with the right cookie can execute arbitrary code on any other. There is no authorisation layer. Erlang distribution must never be exposed to an untrusted network: bind it to a private interface, firewall port 4369 and the distribution port range, and use TLS distribution if it crosses anything you do not control.
rpc:call/4 runs a function on another node and returns the result. global gives a cluster-wide process registry; pg does process groups; gproc is the richer third-party option.
Network partitions are where distributed Erlang stops being magic. If the link between nodes drops, each side sees the other as down - and both carry on. Nothing resolves this for you. If both halves accept writes you have a split brain, and when the link returns, two divergent states. Erlang gives you the mechanism to detect it, net_kernel:monitor_nodes(true), and leaves the policy to you, because the right answer genuinely depends on the application.
Mnesia is Erlang's own distributed DBMS: stores Erlang terms with no mapping layer, supports transactions, and replicates across nodes in memory, on disk, or both. Its strength is low-latency access to data that is already Erlang terms. Its limits are real - the disc_copies tables are capped by DETS's 2 GB, and recovery from a partition requires you to choose which side wins. Use it for configuration, session data and anything cluster-local; reach for PostgreSQL when you need a real database.
%% ---------------- Starting named nodes ----------------
%%
%% erl -sname alice -setcookie secret
%% erl -sname bob -setcookie secret
%%
%% -sname short name, one DNS domain: alice@laptop
%% -name long name, fully qualified: alice@host.example.com
%% They CANNOT talk to each other. Pick one for the whole cluster.
%%
%% In alice's shell:
%%
%% (alice@laptop)1> net_adm:ping('bob@laptop').
%% pong % pang if it failed
%% (alice@laptop)2> nodes().
%% ['bob@laptop']
%%
%% Connections are TRANSITIVE: connecting to one node in a cluster
%% connects you to all of them. That is a full mesh, and it is why
%% Erlang clustering is comfortable at tens of nodes and not hundreds.
%% ---------------- The cookie is the whole security model ------------
%%
%% erlang:get_cookie().
%% erlang:set_cookie(node(), secret).
%% %% or ~/.erlang.cookie, which must be mode 400
%%
%% A shared secret, exchanged in the clear, with NO authorisation layer
%% behind it. Any node holding the cookie can run arbitrary code on any
%% other node in the cluster:
%%
%% rpc:call('bob@laptop', os, cmd, ["whoami"]).
%%
%% That is not an exploit; it is the documented behaviour. Never expose
%% Erlang distribution to an untrusted network. Bind it to a private
%% interface, firewall epmd (4369) and the distribution port range, and
%% use TLS distribution for anything crossing a boundary.
%% ---------------- Remote calls, and remote spawning ----------------
%%
%% rpc:call('bob@laptop', lists, sum, [[1,2,3]]). % 6
%% rpc:call(Node, M, F, A, 5000). % with a timeout
%% rpc:multicall(nodes(), erlang, memory, [total]). % every node
%% rpc:cast(Node, M, F, A). % fire and forget
%%
%% Pid = spawn('bob@laptop', fun() -> ok end),
%% Pid ! hello. % identical to a local send. That is the point.
%%
%% Global registry, across the cluster:
%% global:register_name(bank, self()),
%% global:whereis_name(bank),
%% global:send(bank, Msg).
%%
%% Process groups:
%% pg:join(workers, self()),
%% pg:get_members(workers).
%% ---------------- Watching the cluster ----------------
%%
%% net_kernel:monitor_nodes(true),
%% receive
%% {nodeup, Node} -> handle_join(Node);
%% {nodedown, Node} -> handle_leave(Node)
%% end.
%% ---------------- Split brain ----------------
%%
%% The link between alice and bob drops. Both are healthy. Each sees
%% {nodedown, Other} and concludes the other has failed.
%%
%% If both keep accepting writes, you now have two divergent states and
%% no way to merge them when the link returns. Erlang gives you the
%% DETECTION and leaves the POLICY to you, because the right answer
%% depends entirely on the application:
%%
%% - a quorum: a minority partition stops serving writes
%% - a designated primary, and everyone else goes read-only
%% - CRDTs, if your data can be merged without a decision
%% - accept it, if losing one side's writes is tolerable
%%
%% Choosing nothing is choosing the last one, without meaning to.
%% ---------------- Mnesia ----------------
%%
%% -record(account, {id, owner, balance}).
%%
%% mnesia:create_schema([node() | nodes()]), % once, nodes stopped
%% mnesia:start(),
%%
%% mnesia:create_table(account, [
%% {attributes, record_info(fields, account)},
%% {disc_copies, [node() | nodes()]}, % in RAM AND on disk
%% {type, set},
%% {index, [owner]}
%% ]),
%%
%% %% A transaction. Retried automatically on a deadlock.
%% Transfer = fun(From, To, Amount) ->
%% [A] = mnesia:read(account, From, write), % a write lock
%% [B] = mnesia:read(account, To, write),
%% true = A#account.balance >= Amount, % abort if not
%% mnesia:write(A#account{balance = A#account.balance - Amount}),
%% mnesia:write(B#account{balance = B#account.balance + Amount})
%% end,
%% {atomic, ok} = mnesia:transaction(fun() -> Transfer(1, 2, 50) end).
%%
%% mnesia:dirty_read(account, 1). % no transaction. Fast, unsafe.
%%
%% ram_copies memory only, lost on restart
%% disc_copies memory AND disk - capped by DETS at 2 GB per table
%% disc_only disk only, slow
%%
%% Good for configuration, sessions and cluster-local state that is
%% already Erlang terms. Not a replacement for a real database, and
%% recovering from a partition means choosing which side wins.
Discussion
No questions on this topic yet. Sign in to ask the first one.