pragmatic erlang - introduction
TRANSCRIPT
Click to edit the outline text formatSecond Outline LevelThird Outline LevelFourth Outline LevelFifth Outline LevelSixth Outline LevelSeventh Outline Level
Pragmatic Erlang: Introduction
Disclaimer
Erlanger for about 7 months at IDMOG
Background: 2 years Javascript / 3 years PHP
First presentation
Why Erlang
Reliability ! 99.9999999% uptime
Handles millions of connections on a single server
Low latency
What is Erlang
Distributed / Concurrent = Scalable
Functional + Actor Model = Concurrency Oriented
Pros VS Cons
Actor model
Fault tolerant : Let it crash
Code hot swapping
Distribution / Concurrency built-in
Same code works locally and across nodes
Communication inter nodes is buit-in
Linear scaling
Tools
Simple (No curry, functors nor monads)
Side effect intensive :)
Last call optimization
Binary handling
Production shell
Dynamic typing (opinion)
Compiler is not good enough
Slow
History
Proprietary language built in 1986 by Ericsson for telecom switches
Open sourced in 1998
Distributed became Concurrent many years later
Erlang in the industry
Amazon (database)
AOL (realtime biding)
Bet365 (realtime biding)
Infinity Ward (game servers)
Ericsson (mobile networks)
Facebook (chat)
WhatsApp (chat)
Riot Game (chat)
...
Riak
CouchDB
SimpleDB
Redis
Ejabberd
RabbitMQ
Actor Model
An opinionated way of doing distribution
Everything is an isolated VM process
No shared states
Communication via message passing
Mailboxes
Processes do synchronous work
Quick syntax: Variable
1> Name = "Conan the Barbarian"."Conan the Barbarian"
2> Name."Conan the Barbarian"
3> Name = "Conan the Cimmerian".** exception error: no match of right hand side value "Conan the Cimmerian"
4> Name = "Conan the Barbarian"."Conan the Barbarian"
Quick syntax: Variable
1> Todos = [Erlang talk, Buy some milk].[Erlang talk, Buy some milk]
2> NewTodos = [Run a marathon | Todos].[Run a marathon, Erlang talk, Buy some milk]
3> NewTodos.[Run a marathon, Erlang talk, Buy some milk]
4> Todos.[Erlang talk, Buy some milk]
Quick syntax: Atom
1> atom.atom
2> atom = atom.atom
3> atom = "atom".** exception error: no match of right hand side value "atom"
4> atom = another_atom.** exception error: no match of right hand side value another_atom
Quick syntax: Pattern matching
1> [First, Second, Third] = [1, 2, 3].[1, 2, 3]
2> First.1
3> Second.2
4> Third.3
Quick syntax: Pattern matching
Basket = [apple, banana, orange, apple].
% Trying to eat somethingNewBasket = case Basket of [] -> []; [H | T] -> eat_from_basket(H)end.
Quick syntax: Pattern matching
case fetch_logged_user() of {ok, User} -> greet_user(User); {error, _} -> greet_user(John Doe)end.
{ok, User} = fetch_logged_user(),greet_user(User).
Quick syntax: Functions
% sum/1sum(List) when is_list(List) -> sum(List, 0).
% sum/2 (usually private)sum([], Total) -> Total;
sum([H | T], Total) when is_number(H) -> sum(T, Total + H).
Quick syntax: Functions
fetch_timeseries(SensorId, daily) -> fetch_daily_timeseries(SensorId);
fetch_timeseries(SensorId, weekly) -> fetch_weekly_timeseries(SensorId);fetch_timeseries(SensorId, monthly) -> fetch_monthly_timeseries(SensorId).
Quick syntax: Module
-module(robot).-export([yell/1]).
-spec yell(string()) -> string().yell(Something) when is_string(Something) string:to_upper(Something) ++ !.
1> robot:yell(exterminate).EXTERMINATE!
Quick syntax: Processes
spawn(N, M, F, A).spawn_monitor(N, M, F, A). spawn_link(N, M, F, A).
1> self().
2> Pid = spawn(fun() -> io:format(Hello from ~p, [self()]) end).
Hello from
3> Pid.
Quick syntax: Processes
1> self() ! do_a_backflip.do_a_backflip
2> receive Message -> io:format(Consuming message : ~p, [Message])end.Consuming message : do_a_backflipdo_a_backflip
OTP
Years of good practices absorbed into the language
Behaviors
Applications
supervisor, gen_server, gen_event, gen_fsm
Directory structure
Built-in Tools
Tracer / Debugger
Processes visualizers
Unit test / Code coverage : EUnit
Static analyzer : Dialyzer
Node key value store : ETS / DETS
Distributed key value store : Mnesia
Logger: error_logger
Documentation generator: EDoc
Built-in Tools
Built-in Tools
Built-in Tools
Third party tools
REST server: Webmachine
Dependency management: Rebar
Dates: qdate
Logging: Raven
Example
Lets take a look at a quick server that keeps track of online users. Without OTP
Bonus: hot code reloading
Bonus: supervision
Bonus: multiple nodes
Variants
Elixir
Lisp Flavored Erlang
Questions?