Introducing Erlang

- What the fuzz is all about

Lars Hesel Christensen
@larshesel on Twitter

This talk

  • Yours truly
  • Why Erlang?
  • Erlang philosophy
  • A quick tour
  • Fault tolerance
  • OTP
  • The community
  • Maybe have a look at maps in R17-rc1
  • Outro

A bit about me

  • From Denmark
  • Lived in Switzerland 2008-2013
  • In Barcelona since October


  • Embedded devices (c, c++) for telecom and industry
  • Desktop applications (.Net) for reinsurance
  • Android apps (Java) for banks and industry
  • Server side (Erlang) for retail

Why Erlang?

Ericsson had (among others) the following requirements:

  • Thousands of concurrent tasks
  • Fault tolerance
  • Continuous operation
  • Soft real time
  • Distribution
  • Very large systems (millions of SLOC)

Erlang pocket philosophy

  • The world is parallel
  • Isolation
  • Recoverability
  • Let it crash (defer error handling)
  • ...

Were the first that came to mind...

Erlang the language

  • Impure functional language
  • Single assignment, immutable
  • Strict evaluation
  • Dynamic (with type annotations & Dialyzer)
  • Actors
  • Message passing


  • No classes or inheritance!

Functions

-module(fibonacci).
-export([fibonacci/1]).

fibonacci(1) -> 1;
fibonacci(2) -> 1;
fibonacci(N) -> fibonacci(N-1) + fibonacci(N-2).
> fibonacci:fibonacci(1).
1
> fibonacci:fibonacci(3).
2
> fibonacci:fibonacci(10).
55

Case

-module(using_case).
-export([fibonacci/1]).

fibonacci(N) ->
    case N of
        1 -> 1;
        2 -> 1;
        _ -> fibonacci(N-1) + fibonacci(N-2)
    end.
> using_case:fibonacci(1).
1
> using_case:fibonacci(3).
2
> using_case:fibonacci(10).
55

Pattern matching

> {_, Second, Third, _, _} = {a, tuple, with, five, entries}.
{a,tuple,with,five,entries}
> Second.
tuple
> Third.
with
  • evaluating a function call
  • case statements
  • receive statements
  • try statements
  • and using = as above

Pattern matching

Consider the function:

pattern_match({_, Second, Third, _, _}) ->
    [Second, Third].
> pattern_match({a, tuple, with, five, entries}).
[tuple,with]

Canonical pattern matching example

-define(IP_VERSION, 4).
-define(IP_MIN_HDR_LEN, 5).

DgramSize = byte_size(Dgram),
case Dgram of
    <<?IP_VERSION:4, HLen:4, SrvcType:8, TotLen:16,
      ID:16, Flgs:3, FragOff:13,
      TTL:8, Proto:8, HdrChkSum:16,
      SrcIP:32,
      DestIP:32, RestDgram/binary>> when HLen >= 5, 4*HLen =< DgramSize ->
        OptsLen = 4*(HLen - ?IP_MIN_HDR_LEN),
        <<Opts:OptsLen/binary,Data/binary>> = RestDgram,
    ...
end.

Messages

spawn_a_fun() ->
    Pid = spawn(fun rec_fun/0),
    Pid ! {packet, "Hello!"},
    ok.

rec_fun() ->
    receive
        {packet, Msg} ->
            io:format("Got msg: ~p~n", [Msg])
    end.
> spawn_a_fun().
Got msg: "Hello!"
ok

Fault tolerance, supervisors

Supervising supervisors

Fault tolerance

Fault tolerance

Fault tolerance

The remaining process cannot meaningfully continue!

Actors & message passing!

Actors are

  • Process
  • State and msgbox

Actors encapsulate

  • Actors can only change their own state
  • Change state of others indirectly, through message passing

The Erlang VM

  • Implements processes independently of OS
  • Supports massive concurrency
  • Fast process creation. I once measured $~7\mu s$/process when creating 1M processes
  • Fast context switching
  • Rock solid

Let it crash philosophy

  • Exception: The run-time system does not know what to do
  • Error: The programmer does not know what to do

Let's look at an error example (from Joe's Ph.D thesis).


Assume spec says we handle two op-codes:

asm(load) -> 1;
asm(store) -> 2.

Let it crash (cont.)

What should happen if we eval asm(jump)?

asm(load) -> 1;
asm(store) -> 2;
asm(X) -> ????????????

We could try:

asm(load) -> 1;
asm(store) -> 2;
asm(X) -> exit({oops,i,did,it,again,in,asm,X})

But this isn't really an improvement!

Let it crash (cont.)

So don't! Just let it crash and let the supervisor take care of it.

asm(load) -> 1;
asm(store) -> 2;

This is much easier to understand. No clutter!

OTP middleware

  • a standard library
  • servers (gen_server)
  • finite state machines (gen_fsm)
  • event handling (gen_event)
  • testing, tracing, monitoring
  • and much, much more...

R17 maps

... Let's all go to the terminal

The community

  • mailing lists (questions, patches, bugs)
  • IRC
  • conferences (EUC, EFs, Code Mesh, ...)
  • meetups like this
  • ...

References:

Erlang humor / outro

larshesel.dk
github.com/larshesel
@larshesel on Twitter