Getting started with Erlang

tl;dr;
Here’s a quick introduction to Erlang programming language as well as some basic examples to show you what it looks like and why you should try it !

Erlang

I recently had to learn Erlang for a project with no prior knowledge of the language. In this blog post I’ll describe some of my discoveries, the resources I used to dive in and two simple examples that illustrate the power (and beauty) of Erlang.

I won’t dig deep into the description of the language itself. Some books, listed below, do it much better than me. I will, however, jump directly to the heart of the language: Erlang is a dynamically typed functional language running in a virtual machine. It was developed by Ericson and then open sourced in 1998.

Some of its awesome features are:

  • concurrency and distributed programming: this allows you (thanks to its lightweight processes) to spin a lot of concurrent processes, as shown in an example down below.
  • message passing: It uses asynchronous message passing to communicate between different processes.
  • fault-tolerant: So you don’t impact all your software if one of your process is performing an illegal instruction.
  • OTP: OTP (Open Telecom Platform) is an open source framework that adds a bunch of very useful libraries to the base language. It brings abstraction libraries and design patterns (called behaviors) that you will be very eager to use since they are really helpful when building complete software (most people that are coding in Erlang are actually coding in Erlang/OTP).

Here‘s a nice introduction to Erlang.

So why would you use Erlang ? Well there’s many use cases one can think of, here‘s a list of nice use cases where Erlang is suitable (and some where it is not). But as stated on that page, Erlang is mainly suitable for

“Distributed, reliable, soft real-time concurrent systems”.

Where and how to learn Erlang

It’s not always easy to learn a new programming language. Especially when you are used to imperative languages (procedural, object-oriented, …). A first question one might ask is “is Erlang difficult”. I would say not really, not more than any other languages, but it is different. Yes, it is different if you’re not used to thinking in functional. But it’s really fun and very elegant !

A good starting point (which is also valid for other languages) is http://learnxinyminutes.com/docs/erlang/. It gives you a quick tour of the language itself (how to declare stuff, how to use variables, what is an atom, how to use recursion, how to export/import modules, etc). Then one might move to something more complete in order to understand the distinctive features of the language.

I’d recommend three books that really helped me dive into Erlang:

  1. “Introducing Erlang: Getting Started in Functional Programming” by Simon St. Laurent published by O’Reilly Media (http://shop.oreilly.com/product/0636920025818.do).
  2. “Learn You Some Erlang for Great Good!: A Beginner’s Guide” by Fred Hebert published by No Starch Press (http://learnyousomeerlang.com/).
  3. “Designing for Scalability with Erlang/OTP: Implement Robust, Available, Fault-Tolerant Systems” by Francesco Cesarini and Steve Vinoski published by O’Reilly Media (http://shop.oreilly.com/product/0636920024149.do).

(1) will give you the basics (syntax, different types (or none of them), function declaration and so on). (2) will give you nice examples of concrete uses of the language as well as a deep-dive into OTP and finally (3) will help the most interested of you to use Erlang for developing advanced programs using its main features (scalability, concurrency, fault-tolerent).

But as always, nothing is better than practice. So here are two examples using Erlang.

Network packet parsing using Erlang

One very elegant feature of Erlang is its pattern matching (as other functional languages). It is very nice when one get down to, for example, parsing packets.

This is an example of a function which takes as input an Ethernet frame (in binary) and prints its content:

parse_ethernet(<<
  Src:48,
  Dst:48,
  2048:16,
  Rest/binary>>) ->
  io:fwrite("~p -> ~p (~p)~n", [integer_to_list(Src), integer_to_list(Dst), Rest]),
  parse_ip(Rest);
parse_ethernet(_) ->
  io:fwrite("FAILED TO DECODE Ethernet~n").

 

Isn’t it nice? Erlang will transparently try to match the packet with the first function declaration which states:

  • get me a binary packet with:
    • the first 48 bits are matched to Src
    • the next 48 bits are matched to Dst
    • the next 16 bits must have a value of 2048 (as we expect IPv4)
    • the rest are binaries and matched to Rest

If it succeed, we can easily print the value of those fields. If it doesn’t, it will fall down to the second declaration which will simply output an error message.

Spinning processes with Erlang

The next example shows you how you can spin a lot of Erlang processes. An Erlang process is very small (309 words as stated here).

You can easily spawn a process in Erlang using the function spawn.

spawn(Module, Function, Args) -> pid()

 

Then you would make the created process wait for a message (using Erlang’s message passing) before quitting:

procc(Id) ->
  io:fwrite("[~p] Process started~n", [Id]),
  receive
    {stop} ->
      io:fwrite("[~p] Process stopped~n", [Id])
  end.

 

I’ve been able to spawn 100K processes in less than 30 seconds and 200K processes in about 2 minutes on a i5!

All the code above is available on github here and here. These were small projects I did to learn the language so they might have some design flaws and/or errors but it will give you an idea of the language.

That’s it, hope this little overview made you want to try Erlang!