A Beginner’s guide to Elixir OTP System

Elixir OTP Concepts are one of the core features of this language

When we talk about OTP we are basically talking about a bunch of libraries or collections of libraries and the hierarchy of supervisors and workers.

In this blog I will be covering two main libraries:-

  1. Supervisor
  2. GenServer

So first let’s talk about processes in Elixir.

In elixir, your entire code runs inside processes that are isolated from each other running concurrently and communicating via message.

These processes are very lightweight and you can spawn hundreds and thousands of processes without breaking a sweat.

Here is one example of how to spawn a process

So once the process is done with its job it dies instantaneously.

Example:-

But at times you will assign process a job which due to some reason is not able to work on it and will die, but not gracefully!

Voila! we are finally letting our application crash, just like elixir asks for “Let it crash” but the question is are we really letting the entire application crash?

blog_img1.jpeg

So what happens is, due to some reason if our process reaches a state which it is not supposed to then we are basically turning it off, and with the help of genservers and supervisors, we will respawn that process.

Just like turning OFF and ON a piece of electronic equipment for making it work.

So what are we are going to do about it? We will error handle it and this is where supervisors come.

image.png

This is the supervision tree that cascades from the bottom. So if you have a bad code that breaks at a particular level of this supervision tree then the supervisor keeping eye on that level will restart a new fresh process and after a couple of restarts, the supervisor itself will crash and will give control to the above supervisor.

Here is one example of a supervisor.

So let’s say you want to add or remove a number from your state depending upon the command that you send to the application.

So here that start link is starting a process with the empty state which we will loop over and over and change with our requirement.

Here’s how it works

So here we are achieving the same by sending the commands like {:add, 5} for adding into the state and then {:remove, 6} from the state which was already added previously.

But here we have avoided many checks like deadlock and others. So this is where our GenServer comes into the picture. All this above logic can be achieved using GenServer.

Here is an example:-

So what we are doing here is we are starting a genserver process and since we are passing a __MODULE__ so it knows where to look for the init function to initialize our state. All the looping we are doing previously is done by genserver internally. Handle cast and handle call is basically a way for the genserver process to talk.

So with this, I think I have explained whatever little I know or experienced while working on the elixir.

If you really like the blog show some love and follow or a clap would just do fine.

Happy coding, until next time.

Credits:- The ABCs of OTP — Jesse J. Anderson link.

Did you find this article valuable?

Support Ayoush Chourasia by becoming a sponsor. Any amount is appreciated!