Skip to content

Process-related anti-patterns ​

Read first: Processes, Client-server with GenServer

2 “Under the hood” sections on this page

Source: Elixir guide, Process-related anti-patterns.

These anti-patterns are about ProcessThe unit of concurrency in Elixir. All code runs inside a process. Processes are isolated and concurrent, and they communicate by messages. They are not operating system processes. They are light enough that you can run hundreds of thousands of them.Read the page · Glossary and abstractions that use processes. There are four.

Code organization by process ​

Problem: a process organizes code. It must model a runtime property (concurrency, shared resources, error isolation) instead. All calls to one process wait in its MailboxThe place where the messages that were sent to a process wait. receive/1 searches it for a message that matches a pattern. A send does not block: the sender puts the message in the mailbox and continues.Read the page · Glossary, and the mailbox becomes a bottleneck.

elixir
defmodule Calculator do
  use GenServer
  def add(a, b, pid), do: GenServer.call(pid, {:add, a, b})
  def handle_call({:add, a, b}, _from, state), do: {:reply, a + b, state}
end

Refactoring: organize code with modules and functions:

elixir
def add(a, b), do: a + b

A library must not force behavior such as parallelism on its users. Let them choose.

Functions run in the caller. A process serializes all callers.
Under the hood Our own text. It is not from the official docs.

What the bottleneck looks like in numbers. We wrote the Calculator of the chapter in both ways. One caller made 500,000 additions. The plain function took 20 to 30 ms, and GenServer.call took about 600 ms (two runs). Each call is two messages and a wait. With 16 callers at the same time, the difference grows. The function ran in every caller, on the scheduler where the caller ran, and did 8,000,000 additions in 230 to 280 ms.

All callers of the GenServerA "generic server": a process that holds state and offers synchronous (call) and asynchronous (cast) requests, code reloading, and more.Read the page · Glossary waited in one MailboxThe place where the messages that were sent to a process wait. receive/1 searches it for a message that matches a pattern. A send does not block: the sender puts the message in the mailbox and continues.Read the page · Glossary for one ProcessThe unit of concurrency in Elixir. All code runs inside a process. Processes are isolated and concurrent, and they communicate by messages. They are not operating system processes. They are light enough that you can run hundreds of thousands of them.Read the page · Glossary. That process did 2,000,000 additions in 1.3 to 1.4 seconds. This is the "bottleneck" that the chapter names: a process handles one message at a time, however many cores there are. A GenServer is still the correct tool when you need what a process gives, such as state or serialized access. This test shows what it costs when you do not need it.

A function runs in each caller. A GenServer makes every caller wait in one queue.

Sources: we measured with :timer.tc/1 and Task.async/1 on Erlang/OTP 29 with 16 logical CPUs. The timings change from machine to machine.

Scattered process interfaces ​

Problem: many modules talk directly to an Agent or GenServer. This duplicates code and leaves the shape of the shared data uncontrolled. A list, a map, and an integer can all end up in one agent.

elixir
Agent.update(process, fn _list -> 123 end)                 # module A
Agent.update(process, fn content -> %{a: content} end)     # module B
Agent.update(process, fn content -> [:atom_value | content] end)  # module C

Refactoring: one module owns the interaction and limits the data format:

elixir
defmodule Foo.Bucket do
  use Agent
  def start_link(_opts), do: Agent.start_link(fn -> %{} end)
  def get(bucket, key), do: Agent.get(bucket, &Map.get(&1, key))
  def put(bucket, key, value), do: Agent.update(bucket, &Map.put(&1, key, value))
end

The same is true for scattered GenServer.call/3 and cast/2. KV.Bucket did this in the Mix & OTPOpen Telecom Platform: the set of libraries in Erlang that you use to build fault-tolerant applications. It includes supervision trees and event managers.Read the page · Glossary guide.

One module is the interface. All other modules call it.

Sending unnecessary data ​

Problem: the system copies a message completely into the receiving process (share-nothing). This covers send/2, GenServer.call/3, start_link/3, spawn/1, Task.async/1, and more. It is subtle with anonymous functions: the system copies everything that the closure captures.

elixir
spawn(fn -> log_request_ip(conn) end)             # copies conn
spawn(fn -> log_request_ip(conn.remote_ip) end)   # still copies all of conn!

The second call also captures conn, and only extracts the field afterward.

Refactoring: send the minimum:

elixir
ip_address = conn.remote_ip
spawn(fn -> log_request_ip(ip_address) end)

Alternatively, let the receiving process fetch what it needs, or share data that rarely changes with :persistent_term.

Copy the field and not the struct that contains it.
Under the hood Our own text. It is not from the official docs.

What the closure captures. We built a stand-in for conn: a map of about 104 KB. A process that we spawned with fn -> conn.remote_ip end held 139 KB. The closure carries the whole map and the spawn copies it into the new process. A process that we spawned with a closure over only ip = conn.remote_ip held 2 KB. The closure object and the data that it captured are one thing in memory, as in the Anonymous functions chapter. All of it crosses to the other process.

The system copies a closure with everything that it captured, so capture only what the process needs.

Sources: we measured with Process.info(pid, :memory) and :erts_debug.flat_size/1 on Erlang/OTP 29. The Efficiency Guide describes message copying.

Unsupervised processes ​

Problem: many long-running processes that you start outside a Supervision treeSupervisors that have supervisors as children. The processes of an application then form a tree.Read the page · Glossary are hard to observe, monitor, and control. Dependent processes need ad-hoc ordering, and nothing guarantees when they stop.

Refactoring: start every process in a supervision tree:

elixir
children = [
  Counter,
  Supervisor.child_spec({Counter, name: :other_counter, initial_value: 15}, id: :other_counter)
]
Supervisor.start_link(children, strategy: :one_for_one, name: App.Supervisor)

You get a deterministic start order and a shutdown in reverse order for cleanup. You also get configurable strategies for failures and introspection (Phoenix LiveDashboard, :observer).

The tree gives the start order, the shutdown order, and the visibility.

Site code: MIT. Pages and diagrams come from the Elixir documentation (Apache-2.0, snapshot of 2026-10-08, Elixir 1.20). This project is not affiliated with the Elixir Team.