Erlang

erlang

Sat Sep 27 15:04:00 -0700 2008

Erlang. All the cool kids are doing it: you can tell because the number of projects starting with the letter “e” is on the upswing. (Good, because “r” is definitely a tapped out namespace.) I’ve metioned it offhandedly in a few posts, but now I’m finally getting around to giving it a post of its own.

Erlang’s time is now. Yes, it’s true that we’re early adopters looking for a new toy. But Erlang is gaining traction for another, more pragmatic reason: it’s an excellent tool for building high-performance, reliable, horizontally scalable server daemons.

Why is this problem so pressing now? Haven’t we always needed high-performance server software? No, not like we do now. The rate at which we’re offloading processing to the cloud is greater than Moore’s law. And viral marketing makes it possible for a startup to grow at rates not possible in the recent past. Getting big fast means the need for performant and scalable server processes is a pain more people are feeling.

What tools does Erlang offer that are so suitable to this task?

  • Statelessness - Variables don’t vary. Where’s the state, you might ask? (Answer: hidden in the stack.) Functions without side effects are easier to maintain and debug.
  • Tail recursion - No variables means no loops. Tail recursion fills this role nicely, especially for the standard daemon pattern of looping infinitely doing its business.
  • Lightweight processes - If you were disappointed by Ruby’s Thread.new, you’ll enjoy spawning processes in Erlang. They are cheap and easy, and Erlang developers are encouraged to use them as much as possible. There’s no shared memory (see above: no state), so the usual headaches associated with threads don’t exist here.
  • Messaging - Erlang has a high-speed messaging protocol that makes it possible for processes on the same machine or different machines to talk to each other effortlessly.
  • Hotpatch code into running processes. Amazingly, this isn’t a special feature built into the VM, but rather a pleasant side-effect of the very nature of the language (stateless, first-class functions, dynamic loading of modules).
  • It’s fast. Not as fast as C, but probably less than 3x slower. Since most other high-level languages are an order of magnitude slower than C, this is a big, big difference.

(I’ll probably cover some of these in more detail in future posts.)

Why not other functional languages, like Haskell or OCaml? In one word: maturity. Erlang is not an academic language, hard to compile or find good documentation for. It’s available in both MacPorts and Ubuntu’s package repos, and building from source is easy too. There are many solid and mature libraries for it covering almost anything you might want to do in the server realm. And as for docs, the Prags have a book out on it, what more do you need? (erl -man is pretty good, too.)

There aren’t too many Erlang-focused blogs yet, here’s the ones I’ve found:

Erlang is not just a new programming language - it’s a whole new way to think about server processes. I’ve been having a blast exploring it.