A Look at Erlang/OTP Programming Language
This article was published in Better Programming.
Erlang/OTP (Open Telecom Platform) is an open-source programming language designed in the 1980s by Ericsson that is still used today in various applications including telecommunications, web applications, financial services, gaming, and IoT (Internet of Things). According to Cisco, 90% of all internet traffic goes through Erlang/OTP controlled nodes.
At the time of development, Ericsson was looking to build the next generation of telecom systems which had to be scalable, fault-tolerant, predictable, and maintainable. Their initial goal was never to create a new programming language but engineers soon realized there was no option available that fit their needs.
In addition to a programming language, the Erlang/OTP ecosystem includes a virtual machine and a set of middleware libraries that address many of the problems with scaling and resilience. Despite its age, Erlang/OTP is an essential part of the infrastructure for companies like Nintendo, Goldman Sachs, and WhatsApp.
At the heart of the Erlang/OTP concurrency model are lightweight processes. Processes are isolated from each other, meaning they cannot share memory nor interfere with each other’s state. They are detached from the underlying OS and can only communicate through message passing which can consist of integers, atoms, tuples, lists, or even other processes. Process isolation is controlled by supervisors. If a process fails, it sends an exit signal to its supervisor who then decides whether to terminate the process or restart it.
If the supervisor determines that restarting the process won’t fix the problem, it terminates itself and all dependent processes before notifying the top-level supervisor who decides if it should restart or not. This chain of command travels from the smallest level, sequential code, all the way up to the machine level.
To put this into practice, image a telecom system with thousands of concurrent phone calls. If one call has a bug, the corresponding process will be terminated by its supervisor, thus disconnecting the call. When the affected party redials, a new process is created. This interaction only affects the faulty process and not the thousands of other calls going on in the network.
Processes can be linked together, so if one process fails, the linked processes are notified and can take the appropriate action. Because of processes, Erlang/OTP systems can reach an uptime of 99.999% which translates to only five minutes of downtime per year.
Processes are extremely lightweight and can quickly be created and destroyed, meaning applications can support a large number of concurrent processes without straining system resources. Erlang/OTP’s built-in support for distribution makes it easy to distribute processes across multiple nodes and machines. Since processes do not share memory and communicate through message passing, it does not matter if they are on the same machine or not. This increases the potential scalability, even on systems with thousands or millions of concurrent users. WhatsApp is a prime example of the technology in action; when the company hit 900 million users, it only had around 50 engineers all thanks to Erlang/OTP.
Similar to Java, Ruby, and Node.js, Erlang/OTP uses a virtual machine when executing code. Programs are compiled from source code into low-level instructions called bytecode that is then executed by the virtual machine. The most common virtual machine for Erlang/OTP is BEAM. With other virtual machines, an increase in requests is often accompanied by a degradation of throughput, but not with BEAM since it was built to scale. If a system can handle one million requests per second, it will take a second per request if one million are being served simultaneously. If the number of requests increases to two million, latency will increase to two seconds, but throughput will remain the same. Since BEAM can scale to millions of processes, the only thing programmers have to worry about is developing code to handle a single process.
Erlang/OTP is a powerful and flexible platform designed for building highly scalable, concurrent, distributed, and fault-tolerant applications. Using lightweight processes, message passing, fault-tolerance mechanisms, distribution support, and an extensive standard library, Erlang/OTP is the ideal platform for systems that need to handle a large number of concurrent users or requests with minimal downtime. At over three decades old, Erlang/OTP is sure to be around much longer as demand increases for real-time and low-latency applications.