Skip to content

Registries and supervision trees ​

Read first: Client-server with GenServer

2 “Under the hood” sections on this page

Source: Elixir guide, Registries and supervision trees.

The client refers to buckets by name (PUT shopping milk 1), so the 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 need names. You can use AtomA constant with a value that is its own name, such as :ok. Two atoms are equal if their names are equal.Read the page · Glossary as names (KV.Bucket.start_link(name: :shopping)). This is a bad idea for dynamic names. Never convert user input to atoms. The VM does not garbage collect atoms, so users can use up the atom limit or the memory of the VM.

Registry ​

Registry is a single-node registry that accepts any Elixir value as a name. Pass a :via TupleA fixed collection with curly brackets that can hold any value. Its elements are stored one after the other in memory, so it is fast to read one element by index or to get the size.Read the page · Glossary wherever a name is accepted. Agents, SupervisorA process that detects when its children die and starts new children in their place. Processes are often linked to a supervisor for this reason.Read the page · Glossary, TaskAn asynchronous computation. You start it now and read its result later.Read the page · Glossary, and other BehaviourA module such as GenServer or Supervisor that defines the callbacks that your module implements, so that your module can run as that kind of process. Your module declares it with @behaviour or with use.Read the page · Glossary support it:

elixir
Registry.start_link(name: KV, keys: :unique)
name = {:via, Registry, {KV, "shopping"}}
KV.Bucket.start_link(name: name)
KV.Bucket.put(name, "milk", 1)
KV.Bucket.get(name, "milk")   #=> 1
The registry maps any term to a pid. Atoms from user input are a memory leak.

Where must the code call Registry.start_link/1? In the ApplicationEvery Elixir project is an application. Elixir itself is an application (:elixir), and so are parts of it such as :ex_unit.Read the page · Glossary.

Under the hood Our own text. It is not from the official docs.

What a Registry is. Registry.start_link(keys: :unique, name: UthReg) created an ETSErlang Term Storage: the :ets and :dets modules store large data structures in memory or on disk.Read the page · Glossary table of type :set. The table has the name of the registry. The names and PIDA process identifier. spawn/1 returns it, and send/2 takes it to reach that process.Read the page · Glossary are in that table, outside the heap of any 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. A lookup is an ETS read that copies the pid to the caller (see the ETS section in the Erlang libraries chapter).

A Registry keeps its names in an ETS table.

Sources: :ets.all/0 and :ets.info/2 on Erlang/OTP 29 and Elixir 1.20.

Applications ​

Every Elixir project is an application. Elixir is the application :elixir, and ExUnit is the application :ex_unit. Each mix compile prints Generated kv app. This is the file _build/dev/lib/kv/ebin/kv.app. It holds Erlang terms for the version, the modules, and the applications that you depend on.

An application is the .beam files, the .app manifest, and a priv folder.

To change the .app file, use application/0 in mix.exs. You can start and stop applications. Mix starts your application and its dependencies for you (iex -S mix, mix test). If you start an application and its dependency is not running, you get an error:

elixir
Application.stop(:kv)
Application.stop(:logger)
Application.start(:kv)                #=> {:error, {:not_started, :logger}}
Application.ensure_all_started(:kv)   #=> {:ok, [:logger, :kv]}
A dependency must run before the application that needs it.

The application callback ​

To run code at startup, name a CallbackA function that your module defines and that a process behaviour calls under specific conditions, for example init/1 or handle_call/3 for a GenServer.Read the page · Glossary module with mod: in mix.exs:

elixir
def application do
  [extra_applications: [:logger], mod: {KV, []}]
end

The module calls use Application and implements start/2. The function start/2 must start a Supervision treeSupervisors that have supervisors as children. The processes of an application then form a tree.Read the page · Glossary and return {:ok, root_supervisor_pid}:

elixir
defmodule KV do
  use Application

  @impl true
  def start(_type, _args) do
    children = [
      {Registry, name: KV, keys: :unique}
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end

Do not call Registry.start_link/1 directly there. Start processes inside a supervisor, as a Child specificationA map that tells a supervisor how to start, shut down, and restart one child. You usually write it as {module, options} or only the module name.Read the page · Glossary. The specification is usually {module, options} or only the module. A child can be a supervisor, and this is how trees form. Start a new iex -S mix, because recompile() does not reload the tree.

The callback starts the root supervisor. The supervisor starts its children.

Supervised processes give you these benefits:

  • Introspection: you can see every process, its memory, and its message queue.
  • Resilience: the supervisor decides if and how to restart a child that fails.
  • Graceful shutdown: the children stop in the reverse of the order in which they started.
Under the hood Our own text. It is not from the official docs.

How a SupervisorA process that detects when its children die and starts new children in their place. Processes are often linked to a supervisor for this reason.Read the page · Glossary notices a death. A supervised child is LinkA relationship between two processes for the case of failure. If one process fails, the other process receives an exit signal. Without a link, a failure in one process never crashes another process.Read the page · Glossary to its supervisor, and the supervisor has trap_exit set (Process.info(sup, :trap_exit) returned {:trap_exit, true}). When a process dies, the VM sends an Exit signalThe message that a process sends along its links when it ends. A linked process that does not trap exits also ends, unless the reason is :normal.Read the page · Glossary to every process that is linked to it. Normally the signal also kills the receiver. A process that Trapping exitsThe call Process.flag(:trap_exit, true). Exit signals then arrive as ordinary messages and do not end the process. Supervisors do this to survive their children.Read the page · Glossary gets the signal as a message instead. The supervisor then starts a new child. In our measurement, a killed supervised child ran again about 150 to 200 microseconds later (146 and 199 in two runs).

A link carries the exit signal. When the supervisor traps the signal, it becomes a message that the supervisor can act on.

Sources: Process.info/2 and timing on Erlang/OTP 29. The restart time depends on the child and on the machine.

Project or application? ​

Project is the word of Mix for the thing that Mix compiles and tests. Application is the word of 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 for the thing that the runtime starts and stops as a whole. Our mix.exs defines a project that produces the :kv application.

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.