I am building HTML5 applications at Sencha. It is a great platform, take a look...

 

  Ted Patrick - Developer Relations @ Sencha


   Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



Why Erlang?

DIGG IT!     Published Wednesday, January 16, 2008 at 2:05 PM .

I decided in November to learn Erlang as a new programming language and over the recent holidays I got 3 weeks to play around with it. In my 2008 RIA trends post I listed Erlang as a language trend and thought it would be good to shed some light on the matter.

Erlang - a general-purpose concurrent programming language and runtime system.

The language is very different from any programming language I have worked with and was created by Ericsson to write highly scalable mission critical telecom server products. There are several advantages to Erlang as a language that make it optimal for writing server based applications:

1. Concurrent Processes - Erlang can spawn many processes, like 250,000 processes and these run concurrently in the VM. Each process can receive messages and send messages to other processes. The VM automatically handles executing processes across multiple processors so having more processes generally runs N times faster on N core machines. In a world where cpu cores are multiplying, Erlang has a huge advantage in leveraging additional CPUs without any programming effort.

2. Message Based - Erlang processes communicate by sending messages and the language syntax is optimized to make handling messages easy. The arrity and type of data passed to a function determine which version of the function is called so message handling is done at the interface of the process not in some if/then/else/case statement. The message either matches a function or it is ignored. The way messages and processes are handled is much like the real world, we do not have shared state, rather we send messages to one another by honking, blinking, speaking or waving.

3. No shared state and Variables that do not vary - The strangest truth in Erlang is that variables do not vary. This allows apps to avoid shared state and read/write contention and locks. The rule is that where there are locks, there are keys to loose so Erlang has neither. It seems annoying but in fact it only takes a little while to get used to. In avoiding shared state, processes are much more scalable and issues of thread contention, race conditions, and collisions essentially fall onto the floor.

4. Bit Masking and Pattern Matching - The feature I like most is pattern matching and the bit masking syntax. It is trivial to parse extremely complex binary data and set variables from input data. Here is a little snippet:
<<?Header:4, Chan:4, Note:8, _:8>> = InBytes.
Basically InBytes is an array of bytes and the variables get set to the left. In this case, 4 bytes are read into Header, Chan and 8 bytes into Note. What seems odd is that equals assigns values throught the pattern on the left to become equivalent to the pattern on the right. It looked alien to me at first but once you get it parsing binary data is never the same.

5. Hot Swapping Code - Imagine swapping out server logic within a mission critical server at runtime? Erlang provides a framework called OTP for writing server behavior and swaping the behavior live even on remote Erlang VMs. You have to see it to fully appreciate what is happening and the syntax and patterns for writing the logic are really simple compared to procedural or object oriented languages.

6. RPC and Distributed Computing - Running processes in local and remote Erlange VM's could not be easier. Once the machines are physically allocated by IP you can spawn processes on remote VMs and call remote processes as if they were local. It makes writing a cluster of servers really simple and once the different VMs are trusted on a network exchanging messages is nearly identical to local messaging between processes.

Learning Erlang has already been one of the more interesting explorations in my software career and it is similar to my first experience with Python back in 1999. If you are interested in exploring large scale distributed computing, Erlang is the language and VM in use at both Google and Amazon in a large scale. I think we will see much more from this programming language in 2008 as the services market heats up. I will also be releasing some services written in Erlang for Flex later this year.

Cheers,

Ted :)


Where to find me:

Ted on Twitter - @__ted__
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe

Latest

Lists

Links

Jobs

city, state, zip

Archives