From 0 to DynamicSupervisor — Handling Processes in Elixir

Nuno Marinho
Mar 29 · 4 min read

I started my software development career as a Java developer and it had been my language of choice until December of 2016, which was the time when I decided to join and started coding in 😀.

With them but it isn’t common and it’s always a struggle. One of the use cases I used processes for was when I had to process a big amount of data in a reduced timeframe, but as a rule of thumb, I avoided them as much as I could.

With Elixir I found another world that, , doesn’t treat concurrency and process management like afterthoughts or advanced features.

A few months ago Elixir was updated to version 1.6 which introduced the .

DynamicSupervisor in my way

In one of my current projects, users have tasks assigned to them and the backend has to notify the user about the status of their tasks every 5 minutes.
In terms of database structure, I have users and they have tasks assigned to them.

Database structure

When it comes to the erlang/elixir process structure we decided to have a (Task worker) for each active task, whose responsibility is to constantly update the users about their tasks status.
On top of these processes, there is a DynamicSupervisor responsible for supervising and working as an interface to communicate with the child processes.
In terms of process lifetime it goes like this:

  • When the application starts all the Task workers are started, one by one, for each active task.
  • When a user adds a new task, a new Task worker is started.
  • When a user completes a task the respective Task worker is killed.
Elixir process tree

DynamicSupervisor Code

The TaskSupervisor is a DynamicSupervisor and we just need to initialise it (start_link/1 & init/1) and define the functions to start (start_task_worker/1) and finish (terminate_task_worker/2) the workers related to them.

DynamicSupervisor example code

TaskWorker Code

The TaskWorker is a GenServer that verifies every 5 minutes if the task is active and notifies the user that the task is still open. When a task is finished the related Task worker dies.
One important thing is the restart strategy for the GenServer, we are using the :transient strategy so that if the Task worker is abnormally terminated (e.g. crashes for some unexpected reason) it is immediately respawned by the DynamicSupervisor. If the Task worker is normally terminated (e.g. when the task is finished) it is not respawned.

GenServer example code

StarterWorker Code

In order to start all the workers when the application starts we created a StarterWorker whose single responsibility is to start all Task workers. One Task worker per active Task.

StarterWorker example code

Lesson learned

Like me, when you start with Elixir you might be afraid of using processes because of the experience you had with previous languages, instead, give it a try and you will be surprised how quick and easy it is to work with them.

There are a few great links you should check when trying processes:

Thank you for reading!

Thank you so much for reading and if you enjoyed this article make sure to hit that 👏👏 button. It means a lot to us! Also don’t forget to follow on , , and as we keep posting more and more interesting articles on multiple technologies.

In case you don’t know, Coletiv is a software development studio from Porto specialised in Elixir, iOS, and Android app development. But we do all kinds of stuff. We take care of UX/UI design, web development, and even security for you.



Thoughts, dreams and rants about technology and work life from the Coletiv team

Thanks to Tiago Duarte, Andre Silva, Pedro Costa, and António Valente.

Nuno Marinho

Written by



Thoughts, dreams and rants about technology and work life from the Coletiv team