← build log
orchestrationpattern 14Orchestration & concurrency

One process per connection: what the BEAM buys messaging

A malformed frame on one connection shouldn't be able to touch anyone else's session.

The problem

realtime-gateway holds every client’s WebSocket connection — potentially tens of thousands concurrently — and needs to route messages, track typing indicators, and maintain presence state per connection without one misbehaving client degrading service for anyone else. A malformed frame, a slow client, or a decode error on one connection is a routine event at this scale, not an edge case, and the architecture has to treat it that way.

Approaches considered

Thread pool per connection. Each connection gets a worker thread from a shared pool, common in a Go or Java WebSocket server. Well-understood, but a thread that panics on a bad decode can take down state shared with other threads in the pool if isolation isn’t handled with real discipline everywhere.

Event loop, single-threaded per core. Node.js or a Go event-loop style server multiplexes many connections over one thread. High throughput per core, but an unhandled exception or a blocking call in one connection’s handler can stall the event loop for every other connection sharing it.

One lightweight process per connection. Each WebSocket connection is its own isolated unit of execution, supervised independently, with no memory shared with any other connection’s process.

Trade-offs

Process-per-connection at scale needs a runtime built for cheap process creation and scheduling — spawning an OS thread per connection would exhaust resources long before tens of thousands of connections. It’s not a portable pattern; it depends on the specific concurrency primitives the runtime provides.

The decision

realtime-gateway runs on Elixir/Phoenix. Every WebSocket connection is a Phoenix Channel backed by its own BEAM process.

Why

The BEAM’s processes are lightweight enough — kilobytes, not megabytes — that spawning one per connection at Vitrin’s expected scale is simply the normal way to write an Elixir server, not an optimization. A crash in one connection’s process — a malformed frame, a bad decode — is contained to exactly that process; the supervisor restarts it, and no other connection notices. This is the one place in the system where the language wasn’t a swappable preference: the isolation guarantee only exists because the BEAM’s scheduler makes per-process crash containment true by construction, not by discipline.

Implementation

# realtime-gateway — each client joins its own Channel process,
# isolated from every other connection.
defmodule VitrinWeb.ConversationChannel do
  use Phoenix.Channel

  def join("conversation:" <> conversation_id, _params, socket) do
    {:ok, assign(socket, :conversation_id, conversation_id)}
  end

  def handle_in("send_message", %{"body" => body}, socket) do
    # fire-and-forget: publish over RabbitMQ, no synchronous reply
    MessagingPublisher.publish_message_send(socket.assigns.conversation_id, body)
    {:noreply, socket}
  end

  def handle_in("typing", _params, socket) do
    # ephemeral broadcast only — no AMQP, no database write
    broadcast_from!(socket, "typing", %{user_id: socket.assigns.user_id})
    {:noreply, socket}
  end
end
# Presence is tracked per-process with no separate datastore —
# diffs broadcast automatically on connect and disconnect.
defmodule VitrinWeb.UserSocket do
  channel "user:lobby", VitrinWeb.PresenceChannel
end

defmodule VitrinWeb.PresenceChannel do
  use Phoenix.Channel
  alias VitrinWeb.Presence

  def join("user:lobby", _params, socket) do
    send(self(), :after_join)
    {:ok, socket}
  end

  def handle_info(:after_join, socket) do
    {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{online_at: System.system_time(:second)})
    {:noreply, socket}
  end
end