What is OTP in Elixir?

Open Telecom Platform (OTP)

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

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:

  • Create a new process.
  • Send message to another process.
  • Modify its state.
Elixir OTP processes response flow

Processes follow different patterns and to control those patterns, we use behaviors.

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

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:

  • It comes with the standard set of functions to introduce functionality of both error tracking and reporting.
  • It can also become a part of supervision.

Supervisor

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:

  • Start an application.
  • Stop an application.
  • Restart an application
  • Provide fault tolerance.
  • Provide hierarchical supervision.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved