thiagomajesk
Difference between Supervisors and DynamicSupervisors what am I missing?
Hi! I’ve been diving deeper into Elixir recently and reading through some of the guides and docs about Processes and Supervisors. After reading https://elixir-lang.org/getting-started/mix-otp/dynamic-supervisor.html and comparing the docs from Supervisor — Elixir v1.13.4 and DynamicSupervisor — Elixir v1.13.4 I’m not sure I fully grasped the advantages of DynamicSupervisors.
I think the confusion is mainly because I’ve seen that both modules have the same function to start a child “dynamically”: Supervisor.start_child/2 and DynamicSupervisor.start_child/2. However, every guide I read, states that DynamicSupervisors’s main purpose is to add children to the supervision tree dynamically. What am I missing here?
There’s an example in this code base that shows a Supervisor adding children dynamically as well, so I’m not sure I understand the advantages of one over the other.
PS.: Not directly related to the topic, but I also notice that Supervisor.child_spec/2 and DynamicSupervisor.child_spec/1 have some different semantics that seems not documented. The latter doesn’t allow us to pass the module and uses the name option to define the spec id.
Most Liked
josevalim
I improved the docs to say:
The
Supervisormodule was designed to handle mostly static children
that are started in the given order when the supervisor starts. A
DynamicSupervisorstarts with no children. Instead, children are
started on demand viastart_child/2and there is no ordering between
children. This allows theDynamicSupervisorto hold millions of
children by using efficient data structures and to execute certain
options, such as shutting down, concurrently.
dimitarvp
Permanent == started on app startup and staying there until the app shuts down.
Thanks to this distinction, DynamicSupervisor allows you to start and stop children processes at any time. You also are not strictly mandated to stop them; they will be stopped when the supervisor itself is stopped (although it’s definitely a good idea to stop children proactively after they’ve done their job IMO).
To me, using DynamicSupervisor is appropriate in two scenarios:
- When you would like to spawn quite a lot of (say, at least 200,000+) processes because I’d wager then a performance (or memory) impact would start becoming noticeable.
- When you need to create processes in response to – or because of the specifics of – external systems whose resources you have to monitor e.g. a web crawling service to which you give a new domain to crawl. It would need to spawn processes to enumerate URLs and then maintain a small pool of workers to do the crawling (and apply rate limits if applicable).
I’d say especially the latter option is pretty valid; there are many real-world examples where you cannot neatly plan a supervision from the start because things do change at runtime. DynamicSupervisor is ideal for this.
derek-zhou
I think the DynamicSupervisor is only a special case of Supervisor that handle only dynamic children. In Erlang there is only supervisor. It is a good habit to separate the static children and the dynamic children in different branches of the supervision tree.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









