Date post: | 15-Jul-2015 |
Category: |
Technology |
Upload: | aaron-spiegel |
View: | 99 times |
Download: | 4 times |
Let it Crash
Based on
Prolog
Academic
Bad for Text
RabbitMQOpscode 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
TolerantFunctional
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
ProcessesVery lightweight thread implemented in C/Assembly. Can
typically run 200X more than other typical LWPs.
Message PassingProcesses 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
DistributionProcesses can be run on multiple nodes, and are spread
out explicitly
ApplicationsA 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{}}.
handle_call(_Request, _From, 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!