Wednesday, June 07, 2006


A few months ago, I discovered Erlang and quickly became fascinated by it. Erlang is a dynamically typed functional programming language that runs on a special virtual machine (called BEAM) and a set of libraries developed by Ericsson for the purpose of building large-scale distributed, faul-tolerant applications with soft real time peformance requirements.

Erlang used to be a proprietary technology developed and owned by Ericsson for building large telephone switches. In 1998, Ericsson released Erlang to the community under an open source license. I first heard about Erlang in the context of ejabberd, when I was looking at different Jabber servers for possible deployment at my company. I initially rejected the idea of deploying a server written in a weird, obscure language called Erlang, but as I dug deeper I discovered Erlang's beauty and the power it gives developers for building scalable, robust distributed applications. (Although we didn't end up deploying ejabberd, a good testament to its quality is that has recently made the switch from jabberd, written in C/C++, to ejabberd.)

Erlang's support for distributed programming is unmatched by any other language I know. A core capability of Erlang is spawning lightweight processes that can send and receive messages to each other. A message can be any Erlang term (e.g. {foo, bar, 34, [4,5,6]}), and Erlang's message sending and pattern matching syntax makes message processing a breeze. I know you may think you can imitate the same concurrency facilities in [your favorite language here] using its threading API, but you're probably mistaken. Erlang processes are much more lightweight than OS threads, and hence Erlang scales much better with large numbers of concurrent processes. In addition, Erlang has capabilites such as hot code swapping and remote code deployment, which, in addition to lightweight processes, most languages are probably many years away from having.

Consider the following example. It shows how to spawn a process, send it messages that for printing to console, and then sending it a message to terminate.

-export([start/0, listen/0]).
listen() ->
{msg, Text} ->
io:format("got message: ~s", [Text]),
stop ->
io:format("goodbye", [])

start() ->
Pid = spawn(example, listen, []),
Pid ! {msg, "hello world"},
Pid ! stop.

I hope this gives you a sense of how easy Erlang makes concurrent programming. Of course, this only scratches the surface. There's much more, including a super high performance web server called Yaws and a distributed transactional database called Mnesia, both written in Erlang.

I'll write more about Erlang in the future. For now, I hope I've been able to pique your curiousity. In case you're not fully convinced that Erlang is very powerful, consider the fact that Erlang powers the telephone system in the UK with 31ms downtime per year -- that's 99.9999999% availability. That's very impressive.

No comments: