First impression of Erlang
January 12, 2008 – 17:37During the christmas holidays i had the opportunity to sit down and investigate the functional programming language Erlang, and i was not disappointed at all. I had heard some rumors about this particular language from various sources, particularly when i was at the JAOO conference in September 2007.
Now, there are a number of fundamental differences between a functional programming language and an imperative language like Java or C# that i like, all of which i will not delve into in this post (more on that later). But what really struck me with Erlang is the primitives for concurrent programming and building fault tolerant systems. Concurrent programming - and distributed ditto - has been build into the language from the very beginning due to the source of the language which makes it much more complete.
A Bit of History
Erlang was developed at the Ericsson Computer Science Laboratory around 1990 for use in their telephone switching applications. Their developers investigated several programming languages but found none that could fulfill the requirements needed:
- Concurrency - several thousand events needed to be processed simultaneously.
- Robustness - an error in one part of the software should not interfere with the rest of the system.
- Distributed - the software must be distributed on several computers due to
-
- Performance
- Reliability
- The inherent distributed nature of some systems
So the researchers developed their own language.
Concurrency
The Erlang language provides the programmers with a few simple primitives for handling multiple processes. In Erlang terminology a “process” is not what we normally associate with a process on a computer, but rather a lightweight thread. These processes are not controlled by the operating system, but by the runtime.
Erlang processes are indeed lightweight: each process takes up 300 bytes of memory plus the state and code run by the process. This makes it feasible to make a system with hundreds, even thousands, of processes, since the runtime can switch between them very fast.
Furthermore, since Erlang is a functional programming language with no side effects (not entirely true, but true enough for now), there is no shared state between processes. This makes the software robust since race conditions, deadlocks and inconsistent memory does not exist; processes communicate by sending each other messages - what is known as “Message Passing Concurrency”.
Fault Tolerance and Robustness
Erlang also provides simple primitives to make a system extremely fault tolerant. You can instruct a process to “monitor” another, so that if one goes down - due to some unforseen failure or programming error - it can be restarted immediately. This way you can easily write software that simply will not crash (unless the runtime itself crashes, but that is highly unlikely).
Distributed computing
The same primitives used to spawn local processes can be used on a distributed network of computers. Erlang has a simple mechanism for connecting different hosts into a reliable cluster.
Hot-swapping
This is yet another feature i love about Erlang. The language has been build so that it is really easy to update code on different nodes/hosts without having to take the entire system down for maintenance.
Conclusion
With all that i have just described, why is Erlang not the prefered language of developers around the world? After all, with all the good stuff we get with Erlang, as well as the fact that it has been used in real-world, critical applications should make it the number one choice.
Well, first of all it is a functional programming language. Not many programmers know how to program anything other than imperative languages. That means a lot of education, and that costs. Furthermore it hasn”t reached the critical “sexy”-limit like Ruby (a weird language that has poor performance).
This language has a lot of potential. The only reason i do not use it to write some of my current projects is performance; Erlang is a bit slow when it comes to raw computing, as can be seen at The Computer Language Benchmark Game.
But the things i have learned from Erlang is being put to good use. Currently i am writing a framework for distributed computing in C# where i incorporate a lot of the good stuff. Erlang - and what can be learned from it - is definitely what is needed now that multicore processors are becoming more and more prevalent.
