Monday, December 10, 2007

Erlang and "Web 2.0"

At first glance, one might not guess that Erlang, a programming language invented in the 1980s by Ericsson (the Swedish phone equipment company) would be a great tool for Web 2.0 development. One of the most important parts of the included library is the "Open Telecom Platform" and error messages remind me of my Apple IIe:


=ERROR REPORT==== 10-Dec-2007::22:12:48 ===
game terminating for reason: {noproc,{gen_server,
call,
[{global,song_picker},
{pick_song,undefined}]}}
** exited: {noproc,{gen_server,call,
[{global,song_picker},{pick_song,undefined}]}} **


Recently, though, the language has been gaining a lot of buzz online, and for good reason. So, a few weeks ago I picked up the book "Programming Erlang: Software for a Concurrent World." I quickly skimmed it in an evening, and passed it along to Jason who did the same. By the end of the week, we both liked it enough to consider writing some Amie Street backend services in it, and the week after that convinced ourselves it was actually a good idea.

So, why is Erlang a good match for web-based services? Here are a few reasons:


  • Functional programming and dynamic typing - with a dynamically typed language, you can start writing code and have working prototypes within minutes. Good functional code can be much more concise than its procedural equivalents, and less lines of code usually means less bugs and shorter development time.

  • Concurrency-oriented programming - in Erlang, pretty much everything is its own process. If you've got 20 users playing a Flash game, or 100 shopping carts outstanding, just give them each their own process. State machines show up all the time in web programming, and a state machine having its own thread and not having to worry about in-DB storage makes the whole thing a lot easier to think about.

  • Fault tolerance - because everything is its own process, one user running into a bug can't cause the whole system to go down. Processes can monitor each other and recover gracefully when errors do occur. For example, in the 2-player Flash game I'm writing, if a user quits his browser, an AI player will automatically take his place for the remainder of the game, and the code to do this is exceedingly simple.

  • Some great standard libraries - best example: mnesia, a built-in database that supports full ACID transactions, disk/ram storage, replication, hash-based fragmentation, and more.

  • thrift support - the Thrift RPC library from Facebook has Erlang bindings, so you can make calls into an Erlang service from PHP, Perl, Ruby, or whatever your frontend happens to be written in.



Using Erlang, we've implemented a prototype "song labeling" game like the Google "image labeler" in less than 1000 lines of code. It stores tags to an mnesia database, speaks directly to the Flash client using XMLSocket, and is damn near impossible to crash. The first working prototype took about 12-16 man-hours of development time between the client (Flex 3) and the server. I'd love to see someone beat that in any other language.

1 comment:

Anonymous said...

Hi! Please I need some help ...
I was trying to make a XMLsocket server in erlang to do this same work: a Flex frontend that receives messages across the socket.
I have no problems with the flash client, but I don't know how implement the server. Will you send me the project, or the erlang code/example to do this work ?
Thanks in advance.. .
(... and i'm latin american. My english could be no perfect, sorry if is this case)
mail: manquelm@hotmail.com