Skip to content

Supervising dynamic children ​

Source: Elixir guide, Supervising dynamic children.

Buckets have names. Now the system must supervise them and must be able to create them at any time.

Child specs ​

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 learns how to start a child from its 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. A module or {module, options} in the list of children is short for a call to the child_spec/1 function of that module:

elixir
Registry.child_spec(name: KV, keys: :unique)
#=> %{id: KV,
#     start: {Registry, :start_link, [[name: KV, keys: :unique]]},
#     type: :supervisor}

:id and :start (a triplet of module, function, and arguments) are required. type and the other keys are optional.

A tuple is short for a map. The supervisor uses the map.

use Agent (and use GenServer and use Supervisor) define a default child_spec/1. For this reason a supervisor can already supervise KV.Bucket:

elixir
KV.Bucket.child_spec(name: :shopping)
#=> %{id: KV.Bucket, start: {KV.Bucket, :start_link, [[name: :shopping]]}}

Supervisor.start_link([{KV.Bucket, name: :shopping}], strategy: :one_for_one)
pid = Process.whereis(:shopping)
Process.exit(pid, :kill)
Process.whereis(:shopping)    #=> #PID<0.50.0>   (a new pid)

4 steps

When you kill the bucket, the supervisor starts a new bucket with the same name.

Dynamic supervisors ​

If you list buckets in start/2, 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 starts a fixed set of buckets. Users must be able to create buckets at any time. Supervisor can add children later, but it is not built for millions of children. DynamicSupervisor is built for them: it starts children after the supervisor starts.

elixir
{:ok, sup} = DynamicSupervisor.start_link(strategy: :one_for_one)
DynamicSupervisor.start_child(sup, {KV.Bucket, name: :another_list})

Give the dynamic supervisor a name too, and use it with the registry. Then put both in the tree and add two functions to KV:

elixir
def start(_type, _args) do
  children = [
    {Registry, name: KV, keys: :unique},
    {DynamicSupervisor, name: KV.BucketSupervisor, strategy: :one_for_one}
  ]

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

def create_bucket(name) do
  DynamicSupervisor.start_child(KV.BucketSupervisor, {KV.Bucket, name: via(name)})
end

def lookup_bucket(name), do: GenServer.whereis(via(name))

defp via(name), do: {:via, Registry, {KV, name}}
The dynamic supervisor is a fixed child. Its own children start and stop.

A second create_bucket/1 with the same name returns {:error, {:already_started, pid}}. Tests use a unique name to prevent clashes.

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

What it costs to start a child. A 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 is not an OS thread. It is a heap of a few hundred words and a 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 that the VM sets up in memory. In two measurements, it took about 2.5 to 4 microseconds to spawn each of 1,000,000 processes. This time includes the time to build the list of PIDA process identifier. spawn/1 returns it, and send/2 takes it to reach that process.Read the page · Glossary. Each idle process held about 2.6 KB. For this reason a DynamicSupervisor can start one child for each bucket or each connection. It is also cheap to restart a child: a killed supervised child ran again after 150 to 200 microseconds. We did not measure where that time goes.

To start or restart a process, the VM does a small amount of work.

Sources: :timer.tc/1 and Process.info/2 on Erlang/OTP 29, 64-bit Linux. These are single measurements. The values will change from run to run.

start_supervised ​

In tests, do not call start_link/1. ExUnit starts a Supervision treeSupervisors that have supervisors as children. The processes of an application then form a tree.Read the page · Glossary for each test. start_supervised/2 puts 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 in that tree, so ExUnit stops them when the test ends:

elixir
{:ok, bucket} = start_supervised(KV.Bucket)
{:ok, _} = start_supervised({KV.Bucket, name: config.test})

Observer ​

:observer.start() opens a GUI. Inside a project, first call Mix.ensure_application!(:observer). The Applications tab draws the supervision tree of each running application. New buckets appear when you create them. To make sure that your supervisor reacts, right-click a process and send a kill signal. For this reason, put processes in supervision trees even if they are temporary. You can then reach them and look inside them.

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.