When we talk about OTP we are basically talking about bunch of libraries or collections of libraries and the hierarchy of supervisors and workers.
In this blog I will be covering two main libraries:-
- Supervisor
- GenServer
So first let’s talk about processes in elixir.
In elixir your entire code runs inside processes which are isolated from each other running concurrently and communicating via message.
These processes are very lightweight and you can spawn 100’s and 1000’s of processes without breaking a sweat.
Here is one example of how to spawn a process
iex(1)> spawn(fn -> :rand.uniform(10) end)
#PID<0.170.0 So once the process is done with its job it dies instantaneously.
Example:-
iex(1)> spawn(fn -> :rand.uniform(10) end)
#PID<0.170.0
iex(8)> pid = spawn(fn -> :rand.uniform(10) end)
#PID<0.172.0>
iex(9)> Process.alive?(pid)
false 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 ask for “Let it crash” but the question is are we really letting the entire application crash ?

So what happens is due to some reason if our process reaches a state which it is not suppose to we are basically turning it off and with the help of genserver’s and supervisors we will respawn that process.
Just like turning OFF and ON an 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 comes.

This is the supervision tree which cascades from bottom. So if you have a bad code which breaks at a particular level of this supervision tree then supervisor keeping eye on that level will restart a new fresh process and after couple of restarts the supervisor itself will crash and will give control to the above supervisor.
Here is one example of supervisor.
defmodule OtpConceptsProcesses do
def start_link() do
spawn_link(__MODULE__, :loop, [%{}])
end
def loop(state) do
receive do
{:add, value} ->
new_state = Map.put(state, :add, value)
loop(new_state)
{:remove, value} ->
new_state = Map.put(state, :remove, value)
loop(new_state)
loop(state)
end
end
endSo 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 empty state which we will loop over and over and change it with our requirement.
Here’s how it works
iex(1)> alias OtpConceptsProcesses
OtpConceptsProcesses
iex(2)> OtpConceptsProcesses.start_link
#PID<0.143.0>
iex(3)> pid = OtpConceptsProcesses.start_link
#PID<0.145.0>
iex(4)> send(pid, {:add, 5})
{:add, 5}
iex(5)> send(pid, {:remove, 6})
{:remove, 6}
iex(6)> send(pid, {})
{}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 picture. All this above logic can be achieved using GenServer.
Here is the example:-
defmodule OtpConceptsProcesses do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, :ok, [])
end
def init(:ok) do
{:ok, %{}}
end
def add(pid, value) do
GenServer.cast(pid, {:add, value})
end
def remove(pid, value) do
GenServer.cast(pid, {:remove, value})
end
def value(pid) do
GenServer.call(pid, :final)
end
def handle_cast({:add, value}, state) do
new_state = Map.put(state, :add, value)
{:noreply, new_state}
end
def handle_cast({:remove, value}, state) do
new_state = Map.put(state, :remove, value)
{:noreply, new_state}
end
def handle_call(:final, _from, state) do
{:reply, state, state}
end
endSo what we are doing here is we are starting a genserver process and since we are passing a MODULE so it nows where to look for init function to initialise our state. All the looping we are doing previously is done by genserver internally. Handle cast and handle call is basically a way for genserver process to talk.
So with this I think I have explained whatever little I know or experienced while working on 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.