Software as a Service is a very popular term very used lately. All the code is in the cloud and should be accessible for every user without problems. Scale up and handle concurrency is a problem very common, mostly in systems with chat or sharing information for cooperative work or playing games together. Erlang was discovered for a lot of companies in several sectors and they felt in love with their features. This is a story about what are those features and how help to those companies or specific sectors to be successful.
2. MILAN 21.11.2015 - MANUEL RUBIO
Who I am?
○ Programmer from I was 12 years old... more than 20 years
programming in: Perl, Python, Ruby, PHP, Java, C/C++, JavaScript,
Pascal, Modula-2, Basic,Erlang/OTP and Elixir.
○ Systems Admin from I was 22 years old... more than 10 years
administering GNU/Linux y BSD systems.
○ Wrote the first Erlang/OTP book in Spanish language.
○ Contact:
○ Blog: http://altenwald.org (at this moment only in Spanish)
○ Twitter: @MRonErlang
3. MILAN 21.11.2015 - MANUEL RUBIO
Who we are?
Altenwald Solutions S.L. since 2013 is a company dedicated to:
● High Availability, Concurrency and Distributed Systems
consulting specially if they are built with Erlang/OTP.
● Erlang/OTP Community Support thru books, documentation and
courses (mainly in Spanish). We are part of IEUG (Industrial Erlang
Users Group).
● Make projects… mainly free software and open source.
10. MILAN 21.11.2015 - MANUEL RUBIO
○ Sessions: cannot be modified at the same time usually.
○ Replicate database: always keeps bottlenecks.
○ Master - Slave: only one server can perform writes.
○ Master - Master: decrease the speed of the database access.
○ Cache: increasing number of requests you’ll needed. New bottleneck and
complex flow if you need to purge data.
○ Accelerators & Internal cache for code: to speed up the load of the code.
○ Queues for asynchronous processes or delayed tasks.
○ Concurrency is hard to do or impossible to achieve in the most of the
cases.
○ Websockets … well, we need something better.
Main issues of these architectures
11. MILAN 21.11.2015 - MANUEL RUBIO
What is Erlang?
○ Born at 1986 as a Prolog extension in the Ericsson labs.
○ Language
○ Functional or not? ... better hybrid.
○ Oriented to the Concurrency... Actor Model
○ Virtual Machine or Platform
○ Scheduling and Managing of Processes (supports more than
1.000.000 procs)
○ Memory Management
○ Command line interpreter (shell)
○ Transparent interface for communication between nodes
○ Features
○ Distributed
○ Fault tolerant
○ Scalable
○ Hot code swapping
12. MILAN 21.11.2015 - MANUEL RUBIO
➔ Yaws (Erlang/OTP web server)
➔ Apache (MPM Worker, 250 threads)
➔ Apache (MPM Prefork, 64 processes)
This shows KBytes/second vs load
Why Erlang fits better?
13. MILAN 21.11.2015 - MANUEL RUBIO
When you are stuck in a traffic jam with a Porsche, all you
do is burn more gas in idle. Scalability is about building
wider roads, not about building faster cars.
- Steve Swartz
Why Erlang fits better?
15. MILAN 21.11.2015 - MANUEL RUBIO
● 67 million unique players / month
● 27 million daily players
● 7.5 million concurrent players
● 1 billion events routed / server /
day & only use 20-30% CPU & RAM
● 11K messages/second
● Few hundred chat servers managed
by only 3 people.
● 99% uptime
Use cases...
16. MILAN 21.11.2015 - MANUEL RUBIO
Who is using Erlang/OTP?
… AND MANY MORE
17. MILAN 21.11.2015 - MANUEL RUBIO
What is made in Erlang/OTP?
ejabberd RabbitMQ CouchBase Riak VerneMQ Chef
18. MILAN 21.11.2015 - MANUEL RUBIO
Erlang/OTP around the World!
job opportunities…
19. MILAN 21.11.2015 - MANUEL RUBIO
Actor Model vs OOP
○ Hertz vs Cores
○ Oriented Object Programming is attributed to Alan Kay (Smalltalk)
○ Actor Model is attributed to Carl Hewitt by a study published in 1977.
22. MILAN 21.11.2015 - MANUEL RUBIO
Factorial Example
A typical example of Factorial
23. MILAN 21.11.2015 - MANUEL RUBIO
Factorial Example
A typical example of Factorial (C)
factorial(int f) {
int i;
for (i=f-1; i>1; i--) {
f = f * i;
}
return f;
}
24. MILAN 21.11.2015 - MANUEL RUBIO
Factorial Example
A typical example of Factorial (C recursivo)
factorial(int f) {
if (f <= 1) {
return 1;
}
return f * factorial(f-1);
}
25. MILAN 21.11.2015 - MANUEL RUBIO
Factorial Example
A typical example of Factorial (Erlang)
factorial(0) -> 1;
factorial(1) -> 1;
factorial(N) -> N * factorial(N-1).
26. MILAN 21.11.2015 - MANUEL RUBIO
Language Features
○ Unique Assignments
> A = 1.
1
> A = 2.
** exception error: no match of right hand side value 2
○ Simple language: case, if, try...catch & receive.
case Value of
12 -> "OK";
_ when is_integer(Value) -> "OK";
_ -> "FAIL";
end.
○ Message passing
Pid = spawn(fun micode/0),
Pid ! "Ciao mondo!",
receive
Any -> io:format("OK")
end.
27. MILAN 21.11.2015 - MANUEL RUBIO
Language Features
○ Long, very long numbers
1> fact:fact(128).
38562048236258042173567706592346364061749310959022359027882840327637340257516554356068
61685885073615340300518330589163475921729322624988577661149552450393577600346447092792
47692495585280000000000000000000000000000000
2> fact:fact(1024).
54185287960588572830769219446838547380015539635380134444828702706832106120733766037331
40984136214586719079188457089807539319941657701873682604541333337219391083675280127649
93769768292516937891165755680659663747947314518404886677672556125188694335251213677274
52196343077013371320579624843312887008843617165469023751839045294473227780840293215872
20618538061628060639254353108221868482392871302616909142113622511446847138885878816292
52104046295315949943900357882410243934315037444113890806181406210863953275235375885018
59845158222959965455854124278913090248694429861092315330757913167574514643630402489082
04429077345618273690305022527969265530729673709907587477931276351047024698896679614621
33026237158973227857814631807156427767644064591085076564783456324457736853810336981776
08049870776704639427260534141677912569773337456803747518667626596166561588468145026333
70425226641418621570468256847733609443267374936766749150989537681129458316266438564790
27816385730291542667725665642276826058264393884514911976419675509290208592713156362983
2909894410527321251872495275013140716764...
28. MILAN 21.11.2015 - MANUEL RUBIO
○ Matching
> "Ciao " ++ Who = "Ciao mondo!".
"Ciao mondo!"
> Who.
"mondo!"
○ Sets
> A = [1,2,3,4,5], B = [2,4,6], A -- B.
[1,3,5]
> (A -- B) ++ (B -- A).
[1,3,5,6]
○ Binaries
{ok, PNG} = file:open("debian-logo.png", [read,binary]),
{ok, <<137,"PNG",13,10,26,10,Length:32,"IHDR">>} = file:read(PNG, 16),
{ok, <<Width:32, Height:32, Depth:8, Color:8>>} = file:read(PNG, 10),
{ok, <<Compression:8, Filter:8, Interlace:8>>} = file:read(PNG, 3),
file:close(PNG).
Language Features
29. MILAN 21.11.2015 - MANUEL RUBIO
Behaviors
○ gen_server: generic servers, actors base.
○ gen_fsm: finite state machines.
○ gen_event: event handlers.
○ supervisor: process supervisors.
○ application: base structure for applications.
30. MILAN 21.11.2015 - MANUEL RUBIO
Behaviors
-module(elevator).
-behaviour(gen_fsm).
-compile([export_all]). % to simplify, but should use -export()
start_link() ->
gen_fsm:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
{ok, ground_floor, []}.
ground_floor(down, State) ->
io:format("Beeep!, incorrect option~n", []),
{next_state, ground_floor, State};
ground_floor(up, State) ->
io:format("Going to first floor~n", []),
{next_state, first_floor, State}.
first_floor(down, State) ->
io:format("Going to the ground floor~n", []),
{next_state, ground_floor, State};
first_floor(up, State) ->
io:format("Going to the second floor~n", []),
{next_state, second_floor, State}.
second_floor(down, State) ->
io:format("Going to the first floor~n", []),
{next_state, first_floor, State};
second_floor(up, State) ->
io:format("Beeep!, incorrect option~n", []),
{next_state, second_floor, State}.
% add functions to do easy the use of the calls.
% these are optional:
up_button() ->
gen_fsm:send_event(?MODULE, up).
down_button() ->
gen_fsm:send_event(?MODULE, down).
up up
down down
33. MILAN 21.11.2015 - MANUEL RUBIO
Leave your feedback on Joind.in!
https://m.joind.in/event/codemotion-milan-2015
facebook.com/altenwald
linkedin.com/company/altenwald github.com/altenwald
@altenwald