After reading an interesting article on Erlang and Java interoperability, I have decided to dedicate my spare time to Erlang.
O’Reilly has just published a wonderful book on Erlang, so I decided to dive into it.
Erlang Programming A Concurrent Approach to Software Development
By Francesco Cesarini, Simon Thompson
ISBN 13: 9780596518189
I have already talked to a lot of Erlang-enthusiast guys on Milan Java User group, to get an idea about this programming language: Erlang has over twenty years of experience with designing and implementing parallel algorithms.
“Erlang Progamming” is a very dense and compact book. It give you a very deep overview on Erlang, and has plenty of examples.
Erlang is a functiona language, and its development started in the mid-1980s at Ericsson’s Computer Science Laboratory, becoming eentually the language for the ATM switcher built by the company. Erlang became open source at the end of 1998, cooked with an already strong and mature core library.
The book has a set of examples at the end of every chapter.
The book is divided in two major parts: the first 11 chapters give a high-level introduction of the language, covering all its built-in feature. The chapter 12-19 introduce some of the major library delivered with Erlang, and chapter 20 deals on how to write efficent code.
First part of the book
Chapter 1 give us the above introduction to Erlang, explaing where Erlang it is used. Erlang is a functiona language, with immutable variables. It is build to support thousand of processes running together. The processes communicate each others via message passing, so the vast maiority of synchronization issues are managed by the language itself.
Chapter 2 introduces basic Erlang types (like integers, atoms, lists and tuples). Patter namtching, a very important language concept, is introduced here, and it is a good choice, because the next chapter is already very dense of concepts. For every language construct, a lot of examples are provided to better explain it.
Chapter 3 deals with sequential Erlang programming, error handling and recursion. Because of its functional nature, recursion is a very important concept and the authors spent ten pages on it. But if you are new to recursion concept, it will be hard to get it at a first look: anyway, recursion is a central concept in Erlang, so there will be recurring example of its usage.
Chapter 4 gives the reader an overview on Concurrent programming: in this chapter you starts to see the true difference between Erlang and imperative languages like, for instance, Java or C++. To keep things easy, it is supposed to have a single host with multi-core CPU. Treu Distributed progamming will be introduced far away, in Chapter 11.
In this chapter there is also a case of study about the way Erlang implemented a Jabber server. The important thing here is to remember the “mental switch” from Object oriented programming. In Object oriented programming threads are assigned to objects. In Erlang processes are configured in a different way: you create a process for every logical piece of work you must do.
The key idea is to have “a process for each concurrent activity”. Because each process has a mailbox, it is easy to stack the messages received, and to decide when to process them, giving a simplified model to deal with. The strong idea here is to leave the Erlang VM to deal with concurrency, maximixing the number of messages swapped between processes, and resloving much of the starvation problems. For instance, suppose to have two processes, A and B. Process A sends request to process B. Because of a peak of load, the process A starts to send a lot of messages to process B.ErlangVM will give less chanche to run to process A, because the mailbox of process B is getting bigger. Then ErlangVM will give more CPU time to proces B, giving the system a chance to come back to a balanced state.
Chapter 5 and Chapter 6 deals with process design patterns and with error handling. A lot of this patterns will be more clear when you hit the on OTP behaviours (Chapter 12) but knowing the basics is important. If you have already programmed in a distributed way, this chapter will be easy to undesratand, so I suggest to take them as a “relax” chapters.
Chapter 7 give an overview to records, which mimics a lot Pascal records. Also macros are introduced: they are very similar to the C pre-processor macro. All these mechanics are a spy of the fact Erlang was designed before the 90s, when macro pre processor was the norm. Java and Ruby for instance have wisely avoided these “tricks”.
Chapter 8 introduce us with the Software upgrade mechanism. The examples are good, but the idea is quite unique compared to other programming language. Do not miss this chapter!
Chapter 9 deals with List Comprehension and bit manipulation: very important constructs of Erlang.
Chapter 10 show us ETS and Dets Table: they are memory and disk-based hash tables; we suggest to speed up reading on this chapter, because you will end up not using these structure by hand: Mnesia database is a better and a higer-level interface to this data. Anyway the examples are valuable. In this charapter will be introduced the “Mobile Subscriber Database Example” which will be expand and used as a major example on the other chapters.
Finally Chapters 11 explains to us how distributed programming works; you will understand very well how to depploy programs which spawns on multiple phisical hosts. Is it important to understand how run “disconnected nodes”. Disconnected nodes can decide to which node connect, reducing keep-alive traffic between nodes, and thus having better performance.
This chapter is valuable because give you understandings on how it is difficult to plan a distributed application, keeping an eye also on network limits (like bandwith and response time).
The Open Telecom Platform (OTP)
The second part of the book is equally well written, because you get live examples of the various modules introduced. A the end of this part of the book you will end up keeping (and understanding well) the reference manual you will find on the erlang web site.
Chapter 12 deals with OTP Behaviors, used to simplify implementation of servers.
Chapter 13 describes the e Mnesia distributed DB, reimplementing the back end of the “Mobile Subscriber Database Example” introduced early on Chapter 10.
Chapter 14 introduces a bit of GUI programming via wxErlang, while Chapter 15 introduce raw socket programming. These chapter are interesting.
But I find here the first (and only) “bug” of this book. Raw socket programming is interesting, but only if you must code a protocol on your own.
Most of our application are web based on these days, so I had preferred a different approach: giving the importance of web interfaces nowedays, I have preferred a small introduction to an Erlang web server like Yaws instead.
Chapter 16 gives you plenty of information about interfacing Erlang with other langugas, including Java, C and also the Unix shell (via erl_call).
Chapters from 17 to 19 talks about debugging(17), unit testing(19) and documentation and refactoring tools (18). Anyway the best chapter is the 20, because give you some hints to young Erlang developers.
“Erlang Programming” is a very good book. My few complaints are mostly about the lack of a chapter on web programming, but I understand Erlang is born for different purpose, so the authors focused on giving you a 360 degree overview of the language.
One of the better book I have read, so far.