# Introduction to Erlang

Article by Gints Dreimanis
Wednesday, December 23rd, 2020

Today, we will look at a rather old and somewhat quirky language that most of you probably don’t have on your radars.

While Erlang is not as popular as some modern programming languages, it quietly runs applications like WhatsApp and WeChat that serve massive amounts of users every day.

## What is Erlang, and where is it used?

Erlang is a functional, general-purpose language oriented towards building scalable, concurrent systems with high availability guarantees.

It was built at the end of the 1980s at Ericsson for handling telephone switches. At the time, telephone switching systems were one of the most complicated systems out there, like the internet is nowadays. For this reason, the language used to program them needed to support high concurrency and zero downtime.

After going through multiple existing language options, three guys at the company – Joe Armstrong, Robert Virding, and Mike Williams – decided to create their own. This led to one of the coolest programming languages and, perhaps, the most awesome marketing video for a language I’ve ever seen.

So, what distinguishes this language from all of the others?

### Process-oriented

The main thing that distinguishes Erlang from other languages is its process-based computing model. It uses isolated, lightweight processes that communicate with each other through messages.

These processes can receive messages and, in response to messages, create new processes, send messages to other processes, or modify their state. In other words, Erlang follows the actor model. If you’ve used Akka on JVM, you’ll feel right at home.

The processes are isolated, fast to create, and take up only a small amount of memory. It is easy to expand your system by creating more of them. Since the processes don’t discern whether the other processes are on the same core or in another place, you can easily scale both horizontally (by adding more machines) and vertically (by adding cores).

### Functional

People usually group Erlang as a functional programming language with other languages like Scala and Haskell. Some of FP characteristics are:

• frequent use of pure functions
• higher-order functions
• pattern matching

More about functional programming you can find in our introduction to FP.

### What is Erlang good for?

Primarily, Erlang is a good choice whenever messaging between multiple agents across the network is involved, since that maps well on the basic structure of the language.

It is excellent for:

• Chat apps. Messaging apps, including some famous examples like WeChat and WhatsApp, use Erlang to handle insane amounts of concurrent users. Erlang has a wonderful messaging platform called ejabberd that can be used to create large-scale chat apps.
• Message queue systems. RabbitMQ, an open-source message broker that implements AMQP and other protocols, is a huge success story for Erlang.
• Blockchains. Aeternity, a blockchain for scalable, secure, and decentralized dapps, uses Erlang for its node implementation.
• Binary manipulation. Historically, Erlang has had to support rapid implementation of binary protocols for telecom purposes. Hence, it has features that make binary manipulation much more comfortable, such as pattern matching on binaries. You can, for example, use Erlang as sed for binaries.
• Other distributed, high-performance services. If you need to process transactions coming from a ton of places in your fintech project or create a bidding/user matching platform, Erlang is not the worst choice either.

You can check out some of the frequent use cases of Erlang in our list of Elixir and Erlang companies.

### This looks complicated. Can I build a web app in Erlang?

Yes. Overall, Erlang is well-suited for creating fast and scalable web apps. If you get there, it is quite rewarding. There are some caveats, though.

At the core of your web app (and any other app that works with HTTP) will be Cowboy, but further than that, you need to know what a web app consists of and pick your tools for each layer separately.

Libraries are well documented, but novice-level introductory material is relatively sparse, and you won’t find tutorials for everything. It’s not JavaScript.

All in all, if you do decide to build web apps, using Elixir, a language built on top of Erlang, might be a better choice.

## Why should you use Erlang in your project?

Erlang has three significant advantages over other programming languages, which mainly stem from the unique way the language is built.

• Concurrency. BEAM, the Erlang virtual machine, uses lightweight threads of execution (called processes). These are isolated, run across all CPUs, and communicate through messages. Because of that and language’s functional nature, it is less hard to write concurrent programs in Erlang.
• Scalability. Erlang is perfectly suited to the distributed nature of modern computing and today’s multicore CPUs. Erlang processes allow us to easily scale systems, both by adding more machines and by adding more cores to existing machines.
• Reliability. Erlang has a motto – “let it crash”. Because of the unique approach to fault-tolerance, lightweight processes can be quickly restarted by the supervisor system, which helps you build self-healing systems. While this may not seem reliable, it deals with most bugs that are not due to severe implementation errors.

## Let it crash

In this section, I’ll try to bring insight into how an Erlang app is structured and how the “let it crash” philosophy works out in real life.

In all actuality, letting it crash is not about crashing for the user or the system. That is something Erlang tries very hard to avoid. Rather, it is about containing failure when it inescapably happens, since in life, things do sometimes fail. Shit happens. Let’s see how Erlang cleans it up.

Basically, an Erlang app is a tree of processes.

At the bottom leaves of the tree, we have worker processes – the ones doing most of the work. Up from them, we have supervisors, which launch the workers and check up on them.

Supervisors themselves can be supervised; we can easily add a Grand Supervisor on top of the tree here.

In case a process crashes, it sends a message to its supervisor. Depending on the supervision strategy set, either just the process is restarted or all of the processes underneath its supervisor are.

If restarting the connected workers doesn’t solve the problem a given amount of times in a period, the supervisor will terminate all its children and then itself. At that point, the responsibility to try to handle the problem is pushed upwards to the next supervision layer.

Only if the top-level supervisor fails does it not get restarted and the application crashes.

## Erlang vs. Elixir

Erlang isn’t the only language that operates on BEAM; there are multiple others. The main one is Elixir.

### What is Elixir?

Elixir was created by José Valim in the early 2010s. He took Erlang and made a thin layer on top of it that had a more modern syntax that resembled Ruby.

The resulting language was an improvement over both Erlang and Ruby. It experienced a decent popularity surge in 2015-2016, when Phoenix, its main web framework, was released.

You can read more about Elixir and Phoenix in our introduction to Elixir.

### Advantages of Elixir over Erlang

Elixir doesn’t actually add a lot of new features to Erlang. Everything you can do in Elixir, you can do in Erlang as well, and it is possible to call both languages from each other. Most of Elixir’s advantages stem from the fact that it has a more modern, Ruby-like syntax, which has led to it being more popular than Erlang.

Here are Elixir’s advantages over Erlang:

• Modern syntax. The syntax of Elixir is much easier to understand if you’ve already programmed in virtually any other popular programming language. It removes some amount of boilerplate code and can lead to higher developer productivity.
• Higher popularity. Elixir has been the more popular of the two for quite some time, so content regarding Elixir is more up-to-date, and there is more of it out there.
• Frameworks. If you’re into web development, Phoenix is one of the best frameworks out there, and it is definitely the most convenient one if you want to do web development and functional programming. Talking about frameworks, Elixir also has Nerves – an awesome framework for embedded software. If this is the route you want to take, Elixir is a better choice.

## Is Erlang worth learning?

So, why should you learn this language? There are three reasons:

• You’re eyeing a position in the specific fields that Erlang is used in. E.g. you adore chat apps and you would like to work at WhatsApp. That’s reasonable.
• You want to write really small, portable programs with as little dependencies as possible. Erlang actually enables you to do a whole lot out of the box.
• You’re a genuinely curious human being and want to discover new ways of programming without an immediate benefit to bottom line. In that case, I welcome you to the ranks of BEAM.

If the last is true, I would actually point your way towards Elixir. While both languages are great to use, Elixir is the one that seems to be more popular lately. It will give you more job opportunities and will be easier to learn.

Afterward, you can learn Erlang and what makes it tick. Knowing how Erlang functions underneath Elixir will help you write better Elixir code and make you more likely to get hired as an Elixir developer.

Anyway, I don’t think you will regret any part of journeying BEAM, even though it is not all sunshine and rainbows (BEAM languages can get quite weird sometimes). If you are wondering where to start, I’d guide you either to Learn You Some Erlang for Great Good! or our beginner’s guide on learning Elixir.