Erlang
Erlang was created at Ericsson in the 1980s to run telephone switches that must never go down. Its answer is a very simple concurrency model: a program is thousands or millions of tiny isolated processes that share nothing and talk only by sending messages.
Paradigm: Concurrent and Actor Programming
What Makes Erlang Special
- Processes are cheap. An Erlang process needs about 300 words of memory, so a node can run millions. They are not operating system threads and are scheduled by the runtime (the BEAM).
- Share nothing. Each process has its own heap. Data is copied in messages and immutable, so there are no locks and no data races.
- Message passing.
Pid ! Messagesends andreceive ... endpicks a message from the mailbox with pattern matching. - Let it crash. Instead of defensive code everywhere, a failing process dies, and a supervisor process restarts it. Fault tolerance is structural.
- Hot code loading and distribution. Code can be replaced in a running system, and sending a message to a process on another machine uses the same syntax.
Example: A counter process
-module(counter).
-export([start/0, increment/1, value/1]).
start() -> spawn(fun() -> loop(0) end).
increment(Pid) ->
Pid ! increment,
ok.
value(Pid) ->
Pid ! {value, self()},
receive
{count, N} -> N
end.
loop(N) ->
receive
increment ->
loop(N + 1);
{value, From} ->
From ! {count, N},
loop(N)
end.
How It Works
spawnstarts a new process runningloop(0)and returns its process id. The stateNlives only in the arguments of the loop.loopreceives one message, handles it and calls itself (a tail call), so state is kept without variables being changed.value/1sends its own pid with the request and waits for the reply. The reply is matched by pattern,{count, N}.- In the shell:
P = counter:start(), counter:increment(P), counter:increment(P), counter:value(P).returns2.
History and Where It Is Used
Erlang runs WhatsApp's servers, RabbitMQ, parts of Ericsson's telecom network, and Riak and CouchDB. It is famous for "nine nines" availability claims. The BEAM virtual machine also hosts Elixir and Gleam, and Erlang's OTP library provides the supervision patterns.