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.
Trending in Questions
Other Trending 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
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 6 of 6 Posts
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.
msimonborg
The way that codebase is using
Supervisor.start_child/2is deprecated
Migrating from Supervisor’s :simple_one_for_one
I think if you have a supervisor with “regular” strategy, e.g.
:one_for_one, and start processes withSupervisor.start_child/2it’s assumed they’ll be permanent.DynamicSupervisorshould be used for dynamic start/termination of arbitrary children.Supervisor.child_spec/2is a unique function which receives any child spec (Module,{Module, arg}, ormap) and a list of overrides, and returns that child spec with the overrides applied.DynamicSupervisor.child_spec/1returns a child spec to start aDynamicSupervisorunder a supervisor, the same wayRegistry.child_spec/1returns a child spec to start aRegistryunder a supervisor.josevalim
I improved the docs to say:
thiagomajesk
This is an important distinction that was not clear to me in the docs or guides, this makes things a lot clearer, thanks!
I’m aware that this strategy is deprecated from reading lots and lots of old posts about it
. However, I was still confused that we could add children dynamically to
Supervisorbecause of the wording on the guides.I saw they were different by looking at the implementation, but have you found this documented somewhere? The documentation of
child_spec/1fromDynamicSupervisoronly points to theSupervisordocs.What do you mean by “permanent” in this case? Besides the point on efficiency that @josevalim made, how would that work? I just want to get the wording right here, perhaps I’m not understanding what “permanent” and “dynamic” truly means in this scenario.
Hey @josevalim, I think what has been tripping me up in the docs and guides is that there’s some nuance in the language that doesn’t make the distinction immediately obvious. Improving on your suggestion, I think this would make it a little bit clear, what do you think?
By the way, is there some way we can improve the reference on the other side as well? When I read this section, that states: To dynamically supervise children, see
DynamicSupervisor, it seems that this is the only way it can be done and not just the most efficient way as you mentioned (it’s my general perception while reading the guides as well).msimonborg
IMO the documentation seems clear about it, but I could see how it would be confusing that they’re both supervisors and
DyanmicSupervisorsays “seeSupervisor”, but FWIW it does not say “seeSupervisor.child_spec/2”. I think it’s pointing to the Supervisor docs in general for more info about child specs. Maybe it could link directly to thechild_spec/1section.Returns a specification to start a dynamic supervisor under a supervisoris very different from what’s written in theSupervisor.child_spec/2doc. The doc forDynamicSupervisor.child_spec/1is much more similar toRegistry.child_spec/1orAgent.child_spec/1.I’ll admit this is maybe a bad or incomplete assumption but gleaned from what I’ve seen written elsewhere on the forum. I think José’s new documentation makes it more clear that
DynamicSupervisoris preferred for efficiency of starting and stopping thousands or millions of processes, because there is no specific ordering of them. My takeaway which may be incorrect is that when you add a child spec to a Supervisor it will always be added to the end of the child list and never removed, and shutdown order is always strictly preserved.dimitarvp
Permanent == started on app startup and staying there until the app shuts down.
Thanks to this distinction,
DynamicSupervisorallows 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
DynamicSupervisoris appropriate in two scenarios: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.
DynamicSupervisoris ideal for this.