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 Coletiv and started coding in Elixir 😀.
With Java it’s possible to use processes and parallelize 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, citing Bruce Williams, 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.
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.
When it comes to the erlang/elixir process structure we decided to have a GenServer (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.
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.
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.
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.
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 Coletiv on Medium, Twitter, and LinkedIn 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.