Let it Crash
Based on
Prolog
Academic
Bad for Text
RabbitMQ
Opscode Chef
Not Node.JS
Web-scale
Riak
Parallelism
Funny
Syntax
Concurrent
Actor
Model
Fault Tolerant
Functional
Hard to Learn
Open Telecom
Platform
Lightweight
Light Weight Processes
Hot Code Loading
Reactor
Model
CouchDB
Whats App
Message
PassingSupervisors
Distributed
Elixir
ERLANG DESIGN
PRINCIPLES
Fault
Tolerant
Functional
Distributed SupervisedImmutable
Transparent Concurrent
Language Primitives
Organized into single namespace of modules
Functions are the primary method of abstraction
Functions have a single arity, and functions of different arities
can share names
Pattern Matching with function headers and guards are primary
conditions
Tail-call optimized — so recurse at will (and please)
Dynamically typed with type annotations and static analysis
Basic types: Atom, Function, Integer, Float, Char, Binary, Tuple,
List, Map, PID, Port, Reference, Union
Basic Syntax
-module(module_name).
-export([public_fun/1, public_fun/2, map/2]).
public_fun(Arg1) ->
io:format("Hello, ~w!~n", [Arg1]).
public_fun(Arg1, Arg2) when is_binary(Arg1) ->
io:format("~w ~w!~n", [Arg1, Arg2]);
public_fun(atom, _Arg2) ->
io:format("atom received, binary here: ~w~n", [<<"This is binary
text">>]).
map(List, Fun) ->
map(List, Fun, []).
map([], _Fun, Acc) ->
lists:reverse(Acc);
map([H|T], Fun, Acc) ->
map(T, Fun, [Fun(H)|Acc]).
TRADITIONAL SHARED
MEMORY CONCURRENCY
Locks protect shared
memory from
corruption
Intrinsically sequential
Failure cases: DIY
Distribution: DIY
Multi-core: Good
luck
ACTOR MODEL
CONCURRENCY
No shared memory
Data is shared by
message-passing
• Copies data
• Need not share
hardware
Break up sequential
code
Actor
Actor
Actor
Concurrency Features
Processes
Very lightweight thread implemented in C/Assembly. Can
typically run 200X more than other typical LWPs.
Message Passing
Processes have (mostly) dedicated heap. Memory is
shared exclusively via messages.
Generic Servers Common structure for resilient processes
Links & Monitors Used to setup supervisor hierarchies between processes
Supervisors Common structure for monitoring processes
Distribution
Processes can be run on multiple nodes, and are spread
out explicitly
Applications
A set of Erlang modules, and common structure for running
processes.
ERLANG CONCURRENCY
Supervisor
Worker
Supervisor
Worker
Worker
Based on Erlang
Processes
Erlang messages are very
light weight
Supervisors can revive
processes as needed
Garbage collection done
by process
SMP schedules processes
across cores
ERLANG CONCURRENCY
Supervisor
Worker
Supervisor
Worker
Worker
LAN attached distribution
don’t require much change
Creates truly fault
tolerance systems
Linear scalability, so long
as there are enough
processes
ERLANG CONCURRENCYSo, what’s an Erlang
Process?
-module(echo_and_quit).
-export([init/0]).
init() ->
receive
{Pid, Msg} -> Pid ! Msg
end.
-module(echo_server).
-export([init/1]).
init(Count) ->
receive
{Pid, Msg} -> Pid ! Msg,
init(Count + 1)
end.
ERLANG CONCURRENCYSo, what’s an Erlang
Process?
-module(echo_and_quit).
-export([init/0]).
init() ->
receive
{Pid, Msg} -> Pid ! Msg
end.
-module(echo_server).
-export([init/1]).
init(Count) ->
receive
{Pid, Msg} -> Pid ! Msg,
init(Count + 1)
end.
supervisor Example
-module(erl_sup).
-behaviour(supervisor).
%% API
-export([start_link/0]).
%% Supervisor callbacks
-export([init/1]).
%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
%% ===================================================================
%% API functions
%% ===================================================================
start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
%% ===================================================================
%% Supervisor callbacks
%% ===================================================================
init([]) ->
{ok, { {one_for_one, 5, 10}, [?CHILD(erl_wrk, worker)]} }.
gen_server Example
-module(erl_server).
-behaviour(gen_server).
%% API
-export([ start_link/0 ]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
-record(state, {}).
%%% API
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%% gen_server callbacks
init([]) ->
{ok, #state{}}.
gen_server Example
%%% gen_server callbacks
init([]) ->
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%% Internal functions
LIVE CODING
TIME
Learn More!
Learn you some Erlang Book (Free)
Erlang Language Site
Elixir Language Site
Mostly Erlang Podcast
Programming Elixir Book
Programming Erlang Book
Seven Languages in Seven Weeks Books
Talk to me!

Erlang

  • 1.
    Let it Crash Basedon Prolog Academic Bad for Text RabbitMQ Opscode Chef Not Node.JS Web-scale Riak Parallelism Funny Syntax Concurrent Actor Model Fault Tolerant Functional Hard to Learn Open Telecom Platform Lightweight Light Weight Processes Hot Code Loading Reactor Model CouchDB Whats App Message PassingSupervisors Distributed Elixir
  • 2.
  • 3.
    Language Primitives Organized intosingle namespace of modules Functions are the primary method of abstraction Functions have a single arity, and functions of different arities can share names Pattern Matching with function headers and guards are primary conditions Tail-call optimized — so recurse at will (and please) Dynamically typed with type annotations and static analysis Basic types: Atom, Function, Integer, Float, Char, Binary, Tuple, List, Map, PID, Port, Reference, Union
  • 4.
    Basic Syntax -module(module_name). -export([public_fun/1, public_fun/2,map/2]). public_fun(Arg1) -> io:format("Hello, ~w!~n", [Arg1]). public_fun(Arg1, Arg2) when is_binary(Arg1) -> io:format("~w ~w!~n", [Arg1, Arg2]); public_fun(atom, _Arg2) -> io:format("atom received, binary here: ~w~n", [<<"This is binary text">>]). map(List, Fun) -> map(List, Fun, []). map([], _Fun, Acc) -> lists:reverse(Acc); map([H|T], Fun, Acc) -> map(T, Fun, [Fun(H)|Acc]).
  • 5.
    TRADITIONAL SHARED MEMORY CONCURRENCY Locksprotect shared memory from corruption Intrinsically sequential Failure cases: DIY Distribution: DIY Multi-core: Good luck
  • 6.
    ACTOR MODEL CONCURRENCY No sharedmemory Data is shared by message-passing • Copies data • Need not share hardware Break up sequential code Actor Actor Actor
  • 7.
    Concurrency Features Processes Very lightweightthread implemented in C/Assembly. Can typically run 200X more than other typical LWPs. Message Passing Processes have (mostly) dedicated heap. Memory is shared exclusively via messages. Generic Servers Common structure for resilient processes Links & Monitors Used to setup supervisor hierarchies between processes Supervisors Common structure for monitoring processes Distribution Processes can be run on multiple nodes, and are spread out explicitly Applications A set of Erlang modules, and common structure for running processes.
  • 8.
    ERLANG CONCURRENCY Supervisor Worker Supervisor Worker Worker Based onErlang Processes Erlang messages are very light weight Supervisors can revive processes as needed Garbage collection done by process SMP schedules processes across cores
  • 9.
    ERLANG CONCURRENCY Supervisor Worker Supervisor Worker Worker LAN attacheddistribution don’t require much change Creates truly fault tolerance systems Linear scalability, so long as there are enough processes
  • 10.
    ERLANG CONCURRENCYSo, what’san Erlang Process? -module(echo_and_quit). -export([init/0]). init() -> receive {Pid, Msg} -> Pid ! Msg end. -module(echo_server). -export([init/1]). init(Count) -> receive {Pid, Msg} -> Pid ! Msg, init(Count + 1) end.
  • 11.
    ERLANG CONCURRENCYSo, what’san Erlang Process? -module(echo_and_quit). -export([init/0]). init() -> receive {Pid, Msg} -> Pid ! Msg end. -module(echo_server). -export([init/1]). init(Count) -> receive {Pid, Msg} -> Pid ! Msg, init(Count + 1) end.
  • 12.
    supervisor Example -module(erl_sup). -behaviour(supervisor). %% API -export([start_link/0]). %%Supervisor callbacks -export([init/1]). %% Helper macro for declaring children of supervisor -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). %% =================================================================== %% API functions %% =================================================================== start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). %% =================================================================== %% Supervisor callbacks %% =================================================================== init([]) -> {ok, { {one_for_one, 5, 10}, [?CHILD(erl_wrk, worker)]} }.
  • 13.
    gen_server Example -module(erl_server). -behaviour(gen_server). %% API -export([start_link/0 ]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -define(SERVER, ?MODULE). -record(state, {}). %%% API start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). %%% gen_server callbacks init([]) -> {ok, #state{}}.
  • 14.
    gen_server Example %%% gen_servercallbacks init([]) -> {ok, #state{}}. handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. %%% Internal functions
  • 15.
  • 16.
    Learn More! Learn yousome Erlang Book (Free) Erlang Language Site Elixir Language Site Mostly Erlang Podcast Programming Elixir Book Programming Erlang Book Seven Languages in Seven Weeks Books Talk to me!