of its time executing within the highly
tuned runtime system written in C.
For example, when copying bulk data
between network sockets, interpreted
Erlang performs on par with a custom
C program to do the same task. 4
The significant test of the implementation’s efficiency is the practicality of the worker process idiom,
as demonstrated by the multicall2
code shown earlier. Spawning worker
processes would seem to be much less
efficient than sending messages directly. Not only does the parent have to
spawn and destroy a process, but also
the worker needs extra message hops
to return the results. In most programming environments, these overheads
would be prohibitive, but in Erlang,
the concurrency primitives (
including process spawning) are lightweight
enough that the overhead is usually
negligible.
Not only do worker processes have
negligible overhead, but they also increase efficiency in many cases. When
a process exits, all of its memory can be
immediately reclaimed. A short-lived
process might not even need a collection cycle. Per-process heaps also eliminate global collection pauses, achieving soft real-time levels of latency.
For this reason, Erlang programmers
avoid reusable pools of processes and
instead create new processes when
needed and discard them afterward.
Since values in Erlang are immutable, it’s up to the implementation
whether the message is copied when
sent or whether it is sent by reference.
Copying would seem to be the slower
option in all situations, but sending
messages by reference requires coordination of garbage collection between
processes: either a shared heap space
or maintenance of interregion links.
For many applications, the overhead
of copying is small compared with the
benefit from short collection times
and fast reclamation of space from
ephemeral processes. The low penalty
for copying is driven by an important
exception in send-by-copy: raw binary
data is always sent by reference, which
doesn’t complicate garbage collection
since the raw binary data cannot contain pointers to other structures.
The Erlang emulator can create a
new Erlang process in less than a microsecond and run millions of process-
es simultaneously. Each process takes
less than a kilobyte of space. Message
passing and context switching take
hundreds of nanoseconds.
Because of its performance characteristics and language and library support, Erlang is particularly good for:
˲ Irregular concurrency—
applications that need to derive parallelism
from disparate concurrent tasks
˲ Network servers
˲ Distributed systems
˲ Parallel databases
˲ GUIs and other interactive programs
˲ Monitoring, control, and testing
tools
So when is Erlang not an appropriate programming language, for efficiency or other reasons? Erlang tends
not to be good for:
˲ Concurrency more appropriate to
synchronized parallel execution
˲ Floating-point-intensive code
˲Code requiring nonportable instructions
˲Code requiring an aggressive
compiler (Erlang entries in language
benchmark shoot-outs are unimpressive—except for process spawning and
message passing)
˲Projects to implement libraries
that must run under other execution
environments, such as JVM (Java Virtual Machine) or CLR (Common Language Runtime)
˲ Projects that require the use of extensive libraries written in other languages
Erlang can still form part of a larger solution in combination with other
languages, however. At a minimum,
Erlang programs can speak text or binary protocols over standard interprocess communication mechanisms. In
addition, Erlang provides a C library
that other applications can link with
that will allow them to send and receive Erlang messages and be monitored by an Erlang controlling program, appearing to it as just another
(Erlang) process.
conclusion
With the increasing importance of
concurrent programming, Erlang is
seeing growing interest and adoption. Indeed, Erlang is branded as a
“concurrency-oriented” language. The
standard Erlang distribution is under
active development. Many high-quality libraries and applications are freely
available for:
˲ Network services
˲ GUIs for 3D modeling
˲ Batch typesetting
˲ Telecom protocol stacks
˲ Electronic payment systems
˲ HTML and XML generation and
parsing
˲Database implementations and
ODBC (Open Database Connectivity)
bindings
Several companies offer commercial products and services implemented in Erlang for telecom, electronic
payment systems, and social networking chat. Erlang-based Web servers are
notable for their high performance
and scalability. 3
Concurrent programming will never be easy, but with Erlang, developers
have a chance to use a language built
from the ground up for the task and
with incredible resilience engineered
in its runtime system and standard libraries.
The standard Erlang implementation and its documentation, ported
to Unix and Microsoft Windows platforms, is open source and available for
free download from http://erlang.org.
You can find a community forum at
http://trapexit.org, which also mirrors
several mailing lists.
The author would like to thank Romain Lenglet and JJ
Furman for their feedback.
References
1. armstrong, J. making reliable distributed systems in
the presence of software errors. Ph.d. thesis (2003).
swedish institute of computer science; www.erlang.
org/download/armstrong_thesis_2003.pdf.
2. armstrong, J. Programming Erlang: Software for a
Concurrent World. the Pragmatic Bookshelf, raleigh,
nc, 2007.
3. Brown, B. application server performance testing,
includes django, erly Web, rails, and others; http://
berlinbrowndev.blogspot.com/2008/08/application-
server-benchmarks-including.html.
4. lystig fritchie, s., larson, J., christenson, n., Jones,
d., and ohman, l. sendmail meets erlang: experiences
using erlang for email applications. in Proceedings of
the Erlang User’s Conference, 2000; www.erlang.se/
euc/00/ euc00-sendmail.ps.
5. steele, g.l. and raymond, e.s. The New Hacker’s
Dictionary, 3rd edition. mit Press, cambridge, ma,
1996.
Jim Larson ( jimlarson@google.com) is a software
engineer at google, implementing large-scale distributed
storage systems. he has worked with erlang for
commercial products since 1999 and is the architect
of the replication engine of the amazon simpledB Web
service at amazon.com.
a previous version of this article appeared in the
september 2008 issue of ACM Queue.
2009 © acm 0001-0782/09/0300 $5.00