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.
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.
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.