Skip to content

Lesson 1 of 9 · 27m

BEAM Processes and Message Passing Without Fear

Understand lightweight processes, isolated state, mailboxes, scheduling, links, and the communication patterns that replace shared memory.

The first mental adjustment in Elixir is not syntax. It is giving up shared mutable memory. A BEAM process is cheap, isolated, preemptively scheduled, and communicates by copying messages. Once that clicks, concurrency stops being a locking exercise and becomes system architecture.

A process is not an operating-system process

BEAM can run millions of Erlang processes because the VM manages them, not the kernel. Each has its own heap, stack, mailbox, and control block:

pid = spawn(fn ->
  receive do
    {:ping, caller} -> send(caller, :pong)
  end
end)
send(pid, {:ping, self()})

Do not build production systems from raw spawn/1 loops. They are the assembly language of the BEAM. GenServer, Supervisor, Task, and Phoenix processes package the primitives with lifecycle and error behavior you will need later.

Mailboxes are queues, not magic

Messages are copied into the receiver's mailbox. A receive clause selects matching patterns; unmatched messages remain there. That is powerful for selective protocols and dangerous when an unexpected shape arrives forever.

receive do
  {:vote, voter_id, choice} -> tally(voter_id, choice)
  :flush -> reset_counts()
after
  5_000 -> :timeout
end

The operational question is not whether a process can receive messages. It is whether every message shape has an owner, a timeout, and a bounded queue. A process that never matches common input is a memory leak disguised as concurrency.

Isolation is the point

If two processes share no mutable state, one cannot corrupt the other's memory. It can only send bad input. Validate at the boundary and keep state transitions explicit:

def handle_call({:set_limit, limit}, _from, state) when limit in 1..1_000 do
  {:reply, :ok, %{state | limit: limit}}
end

def handle_call({:set_limit, _limit}, _from, state) do
  {:reply, {:error, :invalid_limit}, state}
end

That pattern feels repetitive until the first time it prevents garbage from propagating across a node. Validate early, normalize once, and keep the interior boring.

BEAM schedulers preempt processes using reductions, so one busy process should not freeze unrelated work. A single process can still become a bottleneck because messages to it are serialized. Isolation gives containment, not infinite capacity.

spawn_link/1 propagates abnormal exits between linked processes. Linking says "our fates are related." Supervision says "restart this into known state." Most application code should use OTP abstractions such as Task.start_link/1 rather than scattering links by hand.

Field notes

  • Messages are copied, so huge binaries and large terms need attention on hot paths.
  • A selective receive that never matches is still a queue-growth bug.
  • Never run a slow external call inside the process whose responsiveness you care about.
  • Process isolation does not protect shared ETS tables, databases, files, or external services.
  • Use Process.monitor/1 when you need death notification without linking fates together.

Was this lesson helpful?

Sign in to rate