OTP stands for Open Telecom Platform. It is based on Erlang and contains a huge set of libraries from BEAM that follow system design principles. In the core of OTP, we have processes which make Elixir very efficient. Let’s have a look at the processes.
Processes are a tiny part of OTP and unlike OS processes, they are very lightweight. It takes a few milliseconds to create them. This is why a single machine can run thousands of these processes simultaneously. They are created directly in BEAM and communicate asynchronously via messages.
Every process receives a message. In response to that message it can:
Processes follow different patterns and to control those patterns, we use behaviors.
Behaviors are a way of separating generic and specific parts of a component. The generic part is the abstract code which does not require implementation, while the specific part is the callback code which requires implementation. We’ll discuss the behaviors GenServer and Supervisor.
GenServer is a behavior module which is used to implement the server of a client-server relation. Like other Elixir processes, it is a process to keep states, asynchronously execute code, and so on. The advantages of using GenServer are:
Process creation is easy using behaviors, but how can we control them? This is where Supervisor comes in. OTP provides us Supervisor, which can start or end any process or even a list of processes. It’s a process to supervise other processes. Supervisors can:
Free Resources