Whats the difference between DynamicSupervisor vs :simple_one_for_one?

Reading the docs for GenStage, DynamicSupervisor (https://hexdocs.pm/gen_stage/Experimental.DynamicSupervisor.html) seems to be pretty much the same thing as a normal supervisor with a strategy of :simple_one_for_one, plus some hooks for GenStage/Flow. Is that a fair assessment, or am I missing something and is it a more general purpose construct?


Yes, I believe you are right. If I remeber correctly, it was created because:simple_one_for_one is not only not really simple, it also works very different from the other types of supervisors, which prompted the Elixir core team to look for another way to do what a simple one for one supervisor would do, but in a clearer way.


I’m confused about when to use dynamic supervisors and when to use :simple_one_for_one.

From my testing, it seems that you cannot use :via tuple to register your children if you use a dynamic supervisor as opposed to a normal supervisor. This makes sense as it seems that your supervisor will keep track of your children, however doesn’t this create two different use cases?

If I needed a chatroom lobby that supervises multiple chatrooms each with an unique name, I should be using :simple_one_for_one. In the case where I just need to spawn a lot of un-named processes, I would use dynamic supervisors

Is my conclusion correct? Did I miss anything?

I think You missed the fact that the worker started by a dynamic supervisor can also register itself via tuple. I did replace all simple_one_for_one with worker started dynamically with a dynamic supervisor.

And each worker is identified by a unique id (I use uuid), in the registry. This is sample code I use in the worker.

  def start_link(args) do
    name = args.uuid
    GenServer.start_link(__MODULE__, args, name: via_tuple(name))

  defp via_tuple(name), do: {:via, Registry, {RegRequests, name}}

Why do You think You can’t?

1 Like