Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

dynamicsupervisor
elixir
elixirsystems
genserver

What is DynamicSupervisor in designing Elixir systems?

Tehreem Arshad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

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
Defining a child spec and naming the process

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
start_link

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
Defining the :via tuple

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] },
Defining a registry

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.

RELATED TAGS

dynamicsupervisor
elixir
elixirsystems
genserver

CONTRIBUTOR

Tehreem Arshad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring