What is DynamicSupervisor in designing Elixir systems?

Supervisor

Supervisor is a process that supervises the child process. It’s used to create a hierarchical process structure called a supervision tree.

DynamicSupervisor

DynamicSupervisor is the branch of Supervisor that starts children dynamically. With this branch, there are no children to begin with. The process starts when the application starts or restarts.

Prerequisites of DynamicSupervisor

  • childspec must be defined. It describes how to start and restart the process.
  • Naming and the procedure of accessing should be defined beforehand.
  • start_link should be provided for GenServer.
  • The supervisor should be registered.

GenServer is like other processes of Elixir, it modules the behavior. It can be synchronous (immediate running) and asynchronous (long-time running).

Let’s understand the above mentioned terms in a bit more detail.

childspec

childspec is the component that defines how the lifecycle should work for a particular type of process. The process can be start, restart, and so on.

Name the process

  • id: This is the unique id for the different processes so that Supervisor can differentiate.
  • start: This is a tuple with complete information of module, name of the function, and the argument list required for start_link function.
  • restart: This is a key that defines the policy for the restart function.
def child_spec({course, email}) do
{
id: {__MODULE__, {course.title, email}},
start: { __MODULE__, :start_link, [{course, email}]},
restart: :temporary
}
end

start_link

If start_link is not provided, the code will be started by DynamicSupervisor. It uses :via to allow some functions, such as the module name, initial tuple, and naming of the process.

def start_link({course, email}) do
GenServer.start_link(
__MODULE__,
{course, email},
name: via({course.title, email}))
end

Registry

To understand the concept of registry, first, :via needs to be cleared. :via is a tuple that OTP uses to register the process. :via consists of Registry and the name of the process. Various registries are used in Elixir implementation.

def via({_title, _email}=name) do
{
:via,
Registry,
{Mastery.Registry, ClassSession, name}
}
end

Registry consists of :name and :keys.

  • :name: This defines the name of the registry.
  • :keys: This defines whether it is unique or not.
{Registry,
[name: Mastery.Registry.QuizSession, keys: :unique] },

Why are DynamicSupervisors more convenient?

DynamicSupervisor allows us to start and stop the GenServer according to the need. It may delete the childspec after the process terminates. It prevents the process to restart. Moreover, it reduces the risk of inconveniences.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved