Skip to content

Lesson 7 of 9 · 27m

Distribution, Clustering, and Backpressure

Connect BEAM nodes safely, understand distributed PubSub and process placement, and design queues that shed load before mailboxes explode.

Distribution is where BEAM demos become operational systems. Connecting nodes is easy. Knowing what happens during network partitions, hot processes, node loss, and thundering reconnects is the discipline. Distribution does not remove backpressure; it makes uncontrolled backpressure harder to see.

Nodes are scaling units and failure domains

libcluster automates discovery with DNS polling, Kubernetes endpoints, or explicit host lists:

topologies = [
  prod: [strategy: Cluster.Strategy.DNSPoll,
    config: [polling_interval: 5_000, query: "pulse.internal", node_basename: "pulse"]]
]
{Cluster.Supervisor, [topologies, [name: Pulse.ClusterSupervisor]]}

Once connected, distributed Erlang makes remote process messaging look local. Transparent does not mean free: large terms serialize, networks partition, and ownership becomes ambiguous.

PubSub distributes events, not truth

Phoenix PubSub broadcasts ephemeral topic messages across nodes. It is not a durable event log. If a subscriber process is dead or a node is partitioned, the message is gone.

Phoenix.PubSub   low-latency ephemeral broadcast
Postgres         transactional truth
Oban             durable asynchronous work
Kafka/NATS       external broker when interoperability or retention demands it

Do not use PubSub as the only delivery mechanism for billable side effects, audit events, or messages the user must eventually receive.

Process placement is architecture

Where a room process runs affects latency and failover. Local naming is simple but single-node. Hashing keys to nodes is predictable but reshuffles painfully. Distributed registries such as Horde provide uniqueness at the cost of operational complexity. Database ownership is slower but explicit.

The decision matters when two nodes both believe they own the same room during a partition. If state cannot diverge safely, you need stronger fencing than hope.

Mailboxes are unbounded queues

Producers can outpace consumers until memory pressure kills the node. Backpressure means producers slow down, retry later, shed acceptable work, or write to a durable bounded queue:

case GenServer.call(worker, {:process, item}, 2_000) do
  :ok -> :ok
  {:error, :busy} -> schedule_retry(item)
end

Synchronous calls create natural backpressure because callers wait. Casts do not. Every cast-based pipeline needs a queue-depth plan.

Demand-aware pipelines exist for a reason

GenStage and Broadway model pull-based demand: consumers request work when they can handle it. Broadway also standardizes batching, acknowledgments, and processor concurrency for external queues. That is very different from dumping messages into a mailbox and hoping the scheduler creates fairness.

Field notes

  • Distributed Erlang relies on a shared cookie and network trust; never expose distribution ports to untrusted networks.
  • Measure mailbox lengths before assuming CPU is the bottleneck.
  • Add jitter and caps to reconnect retry storms.
  • Presence may be eventually consistent; payments cannot be.
  • A test that passes on one node may fail during ownership migration or partition.

Was this lesson helpful?

Sign in to rate