Many people refer to Erlang as “Erlang/OTP.” OTP stands for Open Telecom Platform, and is more or less a set of libraries which come packaged with Erlang. They consist of Erlang behaviors (or behaviours, technically) for writing servers, finite state machines, event managers. But that is not all, OTP also encompasses the application behavior which allows programmers to package their Erlang code into a single “application.” And the supervisor behavior allows programmers to create a hierarchy of processes, where if one process dies, it will be restarted.
OTP is much too complex of a thing to explain in a single article, and I will not attempt to do so here. Instead, I will be making this a series of articles spanning a couple weeks.
Why should I learn about Erlang/OTP?
The platform which comes with Erlang gives you a rich and very generalized set of libraries and behaviors to easily create highly available, fault tolerant, hot code swapping ready systems. The following is a basic list of what it gives you “for free:”
- Generic server, finite state machine, and event manager behaviors
- Standardized application structure
- Hot swappable code
- Supervisor Trees so your processes always stay up
Therefore, by learning OTP, you will not only gain the knowledge and ability to create powerful Erlang systems, but you’ll be able to readily jump into other open source projects and learn from them, since they follow the same general structure.
If you know the general Erlang syntax, then you’re ready to jump in and begin learning about Erlang/OTP!
Click read more below to read the rest of this introduction
The gen_* behaviors
- gen_server - A behavior which should be used to create a general multiple client to server system.
- gen_fsm - A behavior to create a finite state machine. I used gen_fsm to create a SOCKS5 proxy server, which is a FSM following the states: wait_for_init, wait_for_auth, ready, and some intermediate steps for different types of authentication. There are many other uses for finite state machines and this is one of my favorite behaviors.
- gen_event - Used to create event handlers and event dispatchers. The most common use of this is for error logging, which is a dispatcher, and many handlers (terminal, file, etc.) can be attached to it.
The Supervisor Behavior
The supervisor behavior is used to describe its child processes that it will be monitoring. Multiple supervisors are used to create a supervisor hierarchy, so your processes are always up.
To get a great introduction to the supervisor behavior, read this page on it from the Erlang documentation.
The Application Behavior
The application behavior is used to start and stop multiple supervisors and other processes in order to start some code as a single unit. For example, after creating my SOCKS5 server, I created it into an application which I can start by typing application:start(socks5_server).
To read more about the applicaiton behavior, check out the introduction from the Erlang documentation.
What to Look Forward to
This post was meant only as a brief introduction to what OTP is and to answer questions such as why you should learn it and the starting points for where you can learn it. In the coming days I will be posted a series of articles where we’ll create a OTP application.
My next article, which I’ll post tomorrow, will be a comprehensive introduction to gen_server, where we will create a theoretical bank account manager. And by the end of two weeks from now, you will know how to create your own Erlang/OTP-based systems from the ground up.