Distributed Computing using Erlang - 1

Sakib Sami
LiveKlass
Published in
2 min readJan 4, 2017

In the article we will see how a distributed system works in Erlang.

Erlang is a functional programming language mostly known for distributed & concurrent computing.

In general Erlang program runs on a node ( by default nonode@nohost ). In a distributed system nodes communicate with each other. In the article we will use sakib@localhost and cloud@remote-host nodes. sakib@localhost node running on my local system and cloud@remote-host is on remote server. remote-host is my remote server’s hostname. To connect to remote node its important to be same node name as hostname.

Now open local system’s Erlang shell by

erl -name sakib@localhost -setcookie "12345"

And remote server shell,

erl -name cloud@remote-host -setcookie "12345"

Both cookie must be same or you can set node specific cookie later. If no cookie set for the remote node then local node’s cookie will be used to connect to remote node.

% To set cookie on specific node
erlang:set_cookie('cloud@remote-host', '12345').
% 1st param node name, 2nd param cookie value
% To connect to remote node
net_kernel:connect_node('cloud@remote-host').

if response is true then you are connected, if false then failed to connect.

% To list connected nodesnodes().% output : ['cloud@remote-host']
% as we are now connected to the remote node only

Now we can run module of remote node using RPC.

rpc:call('cloud@remote-host', os, cmd, [whoami]).

rpc:call() funcation has 4 parameter.

First one is node name, 2nd module name, 3rd module function and 4th function parameters.

In the example we run cmd() function from os module with parameter whoami. It will return current username of remote system.os is erlang system’s builtin module.

% To place multicall (same command across all connected nodes)
rpc:multicall(nodes(), os, cmd, [whoami]).
% To disconnect from server
erlang:disconnect_node('cloud@remote-host').

In the same way we can distributed different tasks across different nodes to process and get the result on master node. That’s how a distributed system works in Erlang.

Continue to Part 2.

--

--

Sakib Sami
LiveKlass

Senior Software Engineer @ Twilio | Entrepreneur | Tech Ninja