Lesson 3 of 9 · 25m
Designing OTP Applications That Survive Real Work
Structure applications, registries, dynamic workers, caches, and external dependencies around ownership, startup order, and operational failure.
An OTP application is not just a mix project with mod: in it. It is a lifecycle contract: what starts, in what order, under which supervisor, with what recoverable state. Strong Elixir systems feel calm in production because ownership was decided before the first incident.
Start with the ownership map
Classify state before writing modules:
connection state per websocket/process, transient
room state per room, reconstructable or persistent
rate limits per identity, time-dependent
background jobs durable outside the caller when possibleThe owner should have authority to mutate that state. If every module can update the same row or ETS table, you have shared memory wearing an Elixir costume.
A realistic application tree
children = [
Pulse.Repo,
{Phoenix.PubSub, name: Pulse.PubSub},
{Registry, keys: :unique, name: Pulse.RoomRegistry},
{DynamicSupervisor, strategy: :one_for_one, name: Pulse.RoomSupervisor}, PulseWeb.Endpoint
]
Supervisor.start_link(children, strategy: :one_for_one, name: Pulse.Supervisor)Pulse.Repo starts before web traffic because endpoint code expects the database. The endpoint is near the end because it is the front door, not because it is unimportant.
Registry plus DynamicSupervisor is the room pattern
For processes created on demand — rooms, games, sessions, import jobs — register them by name and supervise them dynamically:
def room_pid(room_id) do
case Registry.lookup(Pulse.RoomRegistry, room_id) do
[{pid, _}] -> {:ok, pid}
[] -> DynamicSupervisor.start_child(Pulse.RoomSupervisor, {Pulse.Room, room_id})
end
endTwo callers can race to start the same room. Production code handles that with a unique child name or treats {:error, {:already_started, pid}} as successful convergence.
Processes are not databases
A GenServer serializes access to memory. It does not provide durability, querying, replication, or indexing. If data matters after restart or deploy, put it in Postgres or another durable store. ETS is excellent fast memory for ephemeral presence and terrible truth for entitlements, billing, or audit records.
External services need explicit boundaries
Wrap payment, mail, object storage, and AI APIs in modules that expose failure semantics:
Req.post("https://api.stripe.com/v1/charges",
json: %{customer: customer, amount: amount, currency: "usd"},
headers: [authorization: "Bearer " <> api_key()], receive_timeout: 5_000, retry: false)Field notes
- A child spec maps to a runbook: process name, restart behavior, dependencies, and owner.
- Serve traffic only after dependencies the application truly requires have started.
- Avoid global singletons unless serialization is the goal.
- Umbrella applications can separate deployable concerns, but they do not fix unclear ownership.
- Put database constraints beside validations; application-only truth fails under concurrency.
Was this lesson helpful?