Learning Erlang

I’ve been looking into erlang, on and off, over the last few months and I like it a lot. For building high-availability, scalable, network servers it appears there’s no substitute.

A quick overview of Erlang

Erlang is a concurrent, functional programming language created by Ericsson. Some of the nice features are:

  • It’s designed to be fast, scalable and fault-tolerant.
  • Concurrent programming: It’s easy to create processes and easy to communicate between processes. In fact processes are the main unit of erlang programs.
  • Processes are lightweight so there is low cost to creating them.
  • It supports hot swapping, so you can deploy new code without restarting your system, hence high reliability.
  • It’s dynamically typed and functional so you get advantages associated with each of those.

To get an idea of what erlang code looks like, here’s an example of quicksort in Erlang taken from the erlang wikipedia page (This example shows functional aspects of erlang but not the concurrent aspects):

-module(quicksort).
-export([qsort/1]).

qsort([]) -> [];
qsort([Pivot|Rest]) ->
    qsort([ X || X <- Rest, X < Pivot]) ++ [Pivot] 
                    ++ qsort([ Y || Y <- Rest, Y >= Pivot]).

The first line declares the module name and the second declares that this module exports a function called qsort with one argument. The last 2 lines define the qsort. This is an example of pattern matching and recursion. You can define multiple version of a function and the one that is called is the one where the arguments match. The first one declares that the if the qsort function is called on an empty list, an empty list is returned. The second definition of qsort matches any non-empty list and returns a list consisting of the first element of that list preceded by the qsort of all elements that are less than that element and succeeded by the qsort of all elements that are greater than that element. This function recursively calls itself the list is fully sorted.

A few years ago, I worked on a project doing parallel programming with MPI. It was a painful experience and in the end the parallel version turned out to be slower than the sequential version because of the high cost of communicating between processes. Erlang would have made that project a lot nicer to work on.

I first came across erlang through openpoker: a poker server written in erlang. The author Joel Raymond wrote versions of his poker server in lisp and haskell before finally settling on erlang as the most relaible, scalable solution. Openpoker was originally open source but the author closed it and has now licenced it to an online poker site. The last GPL version of openpoker is available here. The openpoker code is a great way to learn about erlang and it’s how I initally started to learn it.

Scalability

Check out Joels excellent article: Writing Low-Pain Massively Scalable Multiplayer Servers. Vendetta Online have reimplemented some of their backend in erlang.

According to Joe Armstrong (creator of erlang), Ericsson have a product written in erlang that has over 2 million lines of code and 99.9999999% reliability. See his overview of erlang for more details.

Other examples of erlang applications include yaws, an extremely fast erlang-based web server, and ejabberd, a jabber server. Check out this parallel-load comparasion of apache and yaws.

Learning Erlang

Learning erlang is now a lot easier than it was a few months ago. The Pragmatic Programmers have a book in the works: Programming Erlang. I bought the beta pdf and it’s excellent.

The original Erlang book is now out of print. However the first half of it is available for download as a pdf. Erlang is also described in Joe Armstrong’s Phd thesis.

There’s also the documentation available at the erlang website.

Also check out Yariv’s Blog. He has written a lot of articles on erlang and has also created several open-source erlang applications including ErlyWeb, an erlang web framework.

PS

This post came out of a discussion I had on the Ruby Ireland irc channel with Octopod, Olivier and Tonyb when I decided to turn some of the links I posted into a blog post.

Yikes,
u're a brave man!
That code stuff reminds me of some Lisp or even getting back to RPG days (and nothing 2do with gaming!)

I've also seen some good reports on it and a few other brave souls dabbling with it. Be surprised if it ever makes it out of the darkness (remember Modulo-2, Pick, ...)

Lal

Some Erlang bits here (from a RoR's guy) http://pragdave.pragprog.com/pragdave/2007/04/testfirst_word_.html

Lal

You may have seen this already: Andre Pang's presentation "Erlang and Concurrency"

Video

Slides

It'll be interesting to see if ErlyWeb gets traction as a framework..

I'd seen the slides but not the video. Thanks.

I have written poker servers in java and remember the days when a proper java based software server only ran with nice concurrency on sun's own machines ( wonders of wonders ! )

I have personally never heard of erlang before today and would have gone with one of the comments above that say remember modulo-2.

However, does everyone or anyone remember that Intel has already demonstrated an 80+ processor cpu that sat in the palm of your hand ( or was it a finger ? ) just about 18-20 months ago? It was already running then, not just a thought in someone's head but an actual running 80 Processor goliath in a square inch or two.

How many of us have Quads now?

Those are mostly quads with very few truly multi-threaded apps.

It's said that we can encrypt video for youtube and listen to songs, and write documents at the same time. Note that there is no overt multi-threading in those examples, just different apps clinging ( Sticky ) to one processor each while your virus checker runs continually in the background.

So, this erlang is sounding quite promising especially if its already running complex server side processes like a poker server.

Thanks very much for this information.

My doctor says i am allergic to learning new programming languages and I tell all future employers this, right up front, but this one looks like its an allergy beater...

Adios

WOW! As intriguing as your claim that "it appears that there's no substitute"... just looking at that small simple snippet of code is concerning me.

Being that I already know C++, Java, Javascript, Perl, MatLab, VisBasic, QBasic and some Assembly, I'm not sure that I'm prepared to learn another language to replace the combination of languages that are serving me pretty well at the moment.

I think you've got me though and I'm going to have to go and have a quick play and see how big the learning curve is...

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.