Top Banner
1 hour dive into Erlang/OTP @jvalduvieco @jordillonch
193

1 hour dive into Erlang/OTP

Nov 27, 2014

Download

Technology

Jordi Llonch

Introduction to Erlang and OTP
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 hour dive into Erlang/OTP

1 hour dive into Erlang/OTP

@jvalduvieco @jordillonch

Page 2: 1 hour dive into Erlang/OTP

Problem domain

Page 3: 1 hour dive into Erlang/OTP

Lots of users

Page 4: 1 hour dive into Erlang/OTP

Lots of users

Page 5: 1 hour dive into Erlang/OTP

24x7x365

Page 6: 1 hour dive into Erlang/OTP

24x7x365

Page 7: 1 hour dive into Erlang/OTP

Lots of critical concurrent transactions

Page 8: 1 hour dive into Erlang/OTP

Lots of critical concurrent transactions

Page 9: 1 hour dive into Erlang/OTP

Hardware or softwarebreaks

Page 10: 1 hour dive into Erlang/OTP

Hardware or softwarebreaks

Page 11: 1 hour dive into Erlang/OTP

Lots of code changes

Page 12: 1 hour dive into Erlang/OTP

Lots of code changes

Page 13: 1 hour dive into Erlang/OTP

UnscalableUnmaintainable } code

Page 14: 1 hour dive into Erlang/OTP

UnscalableUnmaintainable } code

Page 15: 1 hour dive into Erlang/OTP

Maintenance/Debugin

production system

Page 16: 1 hour dive into Erlang/OTP

Maintenance/Debugin

production system

Page 17: 1 hour dive into Erlang/OTP

Does it sound to you?

Page 18: 1 hour dive into Erlang/OTP

The Erlang solution

Page 19: 1 hour dive into Erlang/OTP

Simplicity...

Page 20: 1 hour dive into Erlang/OTP

Minimize defensive programming

Page 21: 1 hour dive into Erlang/OTP

Typeless variables

Page 22: 1 hour dive into Erlang/OTP

Develop by contract

Page 23: 1 hour dive into Erlang/OTP

t-shirt function size

If����������� ������������������  a����������� ������������������  function����������� ������������������  does����������� ������������������  not����������� ������������������  fit����������� ������������������  on����������� ������������������  your����������� ������������������  t-shirt����������� ������������������  it����������� ������������������  is����������� ������������������  too����������� ������������������  long!

Page 24: 1 hour dive into Erlang/OTP

Single responsibility principle

Page 25: 1 hour dive into Erlang/OTP

No shared state between entities

Page 26: 1 hour dive into Erlang/OTP

High Concurrency

Page 27: 1 hour dive into Erlang/OTP

High ConcurrencyHigh concurrency

Page 28: 1 hour dive into Erlang/OTP

Light threads

Page 29: 1 hour dive into Erlang/OTP

Light threadshundreds of thousands of threads in one

machine with a good cpu scheduler

Page 30: 1 hour dive into Erlang/OTP

Light threadshundreds of thousands of threads in one

machine with a good cpu scheduler

Lt

Lt

Lt

Page 31: 1 hour dive into Erlang/OTP

Lt

Message passing

Lt

Lt

Page 32: 1 hour dive into Erlang/OTP

Lt

Message passingA shared nothing architecture that

communicates through message passing

Lt

Lt

Page 33: 1 hour dive into Erlang/OTP

Lt

Message passingA shared nothing architecture that

communicates through message passing

Lt

Lt

Page 34: 1 hour dive into Erlang/OTP

Lt

Process Mailbox

Lt

Lt

Page 35: 1 hour dive into Erlang/OTP

Lt

Process MailboxEvery process has a mailbox with incoming

messages, process take on convenience

Lt

Lt

Page 36: 1 hour dive into Erlang/OTP

Lt

No shared state

Lt

LtS

S

S

Page 37: 1 hour dive into Erlang/OTP

Lt

No shared stateEvery process its own internal state stored in

a variable avoiding lock contention

Lt

LtS

S

S

Page 38: 1 hour dive into Erlang/OTP

Soft realtime

Page 39: 1 hour dive into Erlang/OTP

Soft realtime

You have no strict guarantees on latency but language is designed

to have low latency

Page 40: 1 hour dive into Erlang/OTP

High availability

Page 41: 1 hour dive into Erlang/OTP

High availabilityHigh availability

Page 42: 1 hour dive into Erlang/OTP

Pa

Supervised processes

Pb

Page 43: 1 hour dive into Erlang/OTP

Pa

Supervised processesprocesses can be monitored by other

processes, handling its termination

Pb

Page 44: 1 hour dive into Erlang/OTP

Pb

Fail early

Pa

S

Page 45: 1 hour dive into Erlang/OTP

Pb

Fail earlyFail as soon as possible and let someone handle bad data, someone will restart you

Pa

S

Page 46: 1 hour dive into Erlang/OTP

Fail earlyFail as soon as possible and let someone handle bad data, someone will restart you

Pa

Pb2

S

Page 47: 1 hour dive into Erlang/OTP

Hot code update

Pa v1

S

Pb

Pc

Page 48: 1 hour dive into Erlang/OTP

Hot code updateprocesses code and data can be replaced

without loosing service

Pa v1

S

Pb

Pc

Page 49: 1 hour dive into Erlang/OTP

Hot code updateprocesses code and data can be replaced

without loosing service

Pa v1Pa v2

S

Pb

Pc

Page 50: 1 hour dive into Erlang/OTP

P

Distribution

Node

P

PP

P

Node

P

P

P

PP PP

Page 51: 1 hour dive into Erlang/OTP

P

DistributionProcesses run on nodes and can be located

wherever they are

Node

P

PP

P

Node

P

P

P

PP PP

Page 52: 1 hour dive into Erlang/OTP

How does it look like?

Page 53: 1 hour dive into Erlang/OTP
Page 54: 1 hour dive into Erlang/OTP

Show me the code!

Page 55: 1 hour dive into Erlang/OTP

Demo 1

Page 56: 1 hour dive into Erlang/OTP

Hands on

Page 57: 1 hour dive into Erlang/OTP

The shell

Page 58: 1 hour dive into Erlang/OTP

Type on your console:

erl

Page 59: 1 hour dive into Erlang/OTP

You should see:Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V5.10.1 (abort with ^G)1>

Page 60: 1 hour dive into Erlang/OTP

Variables

Page 61: 1 hour dive into Erlang/OTP

Variables start Uppercase

Page 62: 1 hour dive into Erlang/OTP

Variables are immutable

Page 63: 1 hour dive into Erlang/OTP

Can contain any type

Page 64: 1 hour dive into Erlang/OTP

You can do things like...

Page 65: 1 hour dive into Erlang/OTP

1> Foo = 1.12> Foo = 2.** exception error: no match of right hand side value 2

Page 66: 1 hour dive into Erlang/OTP

1> Foo = 1.12> Foo = 2.** exception error: no match of right hand side value 2

Foo����������� ������������������  is����������� ������������������  bounded����������� ������������������  to����������� ������������������  1

Page 67: 1 hour dive into Erlang/OTP

1> Foo = 1.12> Foo = 2.** exception error: no match of right hand side value 2

Foo����������� ������������������  is����������� ������������������  bounded����������� ������������������  to����������� ������������������  1

Foo����������� ������������������  ==����������� ������������������  2����������� ������������������  ?

Page 68: 1 hour dive into Erlang/OTP

1> Foo = 1.12> Bar = 2.23> Foo = Bar.** exception error: no match of right hand side value 2

Page 69: 1 hour dive into Erlang/OTP

This is GREAT!

Page 70: 1 hour dive into Erlang/OTP

Now you have your basic error checking system implemented

Page 71: 1 hour dive into Erlang/OTP

Advanced types

Page 72: 1 hour dive into Erlang/OTP

[List]

Page 73: 1 hour dive into Erlang/OTP

[1,2,3,4,5,6]

Page 74: 1 hour dive into Erlang/OTP

You can do things like...

Page 75: 1 hour dive into Erlang/OTP

Iterate sequentially

Page 76: 1 hour dive into Erlang/OTP

1> MyList = [1,2,3,4].[1,2,3,4]2> [Head|Tail] = MyList.[1,2,3,4]3> Head.14> Tail.[2,3,4]

Page 77: 1 hour dive into Erlang/OTP

1> MyList = [1,2,3,4].[1,2,3,4]2> [Head|Tail] = MyList.[1,2,3,4]3> Head.14> Tail.[2,3,4]

Page 78: 1 hour dive into Erlang/OTP

1> MyList = [1,2,3,4].[1,2,3,4]2> [Head|Tail] = MyList.[1,2,3,4]3> Head.14> Tail.[2,3,4]

Page 79: 1 hour dive into Erlang/OTP

Do something to all or some items on a list

(list comprehensions)

Page 80: 1 hour dive into Erlang/OTP

1> MyList = [1, 2, 3, 4].[1,2,3,4]2> [X + 2 || X <- MyList, X > 2].[5,6]

Page 81: 1 hour dive into Erlang/OTP

[Strings]

Page 82: 1 hour dive into Erlang/OTP

A list

Page 83: 1 hour dive into Erlang/OTP

You can do things like...

Page 84: 1 hour dive into Erlang/OTP

1> MyString = "Erlang is not Ruby"."Erlang is not Ruby"

Page 85: 1 hour dive into Erlang/OTP

1> MyString = "Erlang is not Ruby"."Erlang is not Ruby"2> [Head2|Tail2] = MyString."Erlang is not Ruby"3> Head2.694> Tail2."rlang is not Ruby"

ASC����������� ������������������  =����������� ������������������  “E”

Page 86: 1 hour dive into Erlang/OTP

{Tuples}

Page 87: 1 hour dive into Erlang/OTP

{1,2,3}

Page 88: 1 hour dive into Erlang/OTP

Basic data container

Page 89: 1 hour dive into Erlang/OTP

random access

Page 90: 1 hour dive into Erlang/OTP

matcheable

Page 91: 1 hour dive into Erlang/OTP

You can do things like...

Page 92: 1 hour dive into Erlang/OTP

1> Mytuple = {1,2,3,4}.{1,2,3,4}2> A = 1.13> B = 2.24> {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5> C.36> D.4

Page 93: 1 hour dive into Erlang/OTP

1> Mytuple = {1,2,3,4}.{1,2,3,4}2> A = 1.13> B = 2.24> {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5> C.36> D.4

A����������� ������������������  and����������� ������������������  B����������� ������������������  arebounded����������� ������������������  variables

Page 94: 1 hour dive into Erlang/OTP

1> Mytuple = {1,2,3,4}.{1,2,3,4}2> A = 1.13> B = 2.24> {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5> C.36> D.4

A����������� ������������������  and����������� ������������������  B����������� ������������������  arebounded����������� ������������������  variables

pattern����������� ������������������  matchingA==1?B==2?

Page 95: 1 hour dive into Erlang/OTP

1> Mytuple = {1,2,3,4}.{1,2,3,4}2> A = 1.13> B = 2.24> {A,B,C,D} = {1,2,3,4}.{1,2,3,4}5> C.36> D.4

A����������� ������������������  and����������� ������������������  B����������� ������������������  arebounded����������� ������������������  variables

pattern����������� ������������������  matchingA==1?B==2?

C����������� ������������������  and����������� ������������������  D����������� ������������������  are����������� ������������������  unbounded����������� ������������������  variables

Page 96: 1 hour dive into Erlang/OTP

functions

Page 97: 1 hour dive into Erlang/OTP

functions are types

Page 98: 1 hour dive into Erlang/OTP

single exit point

Page 99: 1 hour dive into Erlang/OTP

You can do things like...

Page 100: 1 hour dive into Erlang/OTP

foo_bar_func(Foo, Bar) -> Result = Foo + Bar, Result.

Page 101: 1 hour dive into Erlang/OTP

foo_bar_func(Foo, Bar) -> Result = Foo + Bar, Result.

This����������� ������������������  is����������� ������������������  a����������� ������������������  arity����������� ������������������  2����������� ������������������  function����������� ������������������  (2����������� ������������������  parameters)

Page 102: 1 hour dive into Erlang/OTP

foo_bar_func(Foo, Bar) -> Result = Foo + Bar, Result.

This����������� ������������������  is����������� ������������������  a����������� ������������������  arity����������� ������������������  2����������� ������������������  function����������� ������������������  (2����������� ������������������  parameters)

means����������� ������������������  that����������� ������������������  more����������� ������������������  code����������� ������������������  is����������� ������������������  to����������� ������������������  come

Page 103: 1 hour dive into Erlang/OTP

foo_bar_func(Foo, Bar) -> Result = Foo + Bar, Result.

This����������� ������������������  is����������� ������������������  a����������� ������������������  arity����������� ������������������  2����������� ������������������  function����������� ������������������  (2����������� ������������������  parameters)

means����������� ������������������  that����������� ������������������  more����������� ������������������  code����������� ������������������  is����������� ������������������  to����������� ������������������  come

means����������� ������������������  the����������� ������������������  function����������� ������������������  code����������� ������������������  has����������� ������������������  ended

Page 104: 1 hour dive into Erlang/OTP

foo_bar_func(Foo, Bar) -> Result = Foo + Bar, Result.

Single����������� ������������������  exit����������� ������������������  pointReturns����������� ������������������  Result

This����������� ������������������  is����������� ������������������  a����������� ������������������  arity����������� ������������������  2����������� ������������������  function����������� ������������������  (2����������� ������������������  parameters)

means����������� ������������������  that����������� ������������������  more����������� ������������������  code����������� ������������������  is����������� ������������������  to����������� ������������������  come

means����������� ������������������  the����������� ������������������  function����������� ������������������  code����������� ������������������  has����������� ������������������  ended

Page 105: 1 hour dive into Erlang/OTP

1> FooBar = fun(Foo, Bar) ->1> Foo + Bar1> end.#Fun<erl_eval.12.82930912>2> Result = FooBar(1, 2).3

Page 106: 1 hour dive into Erlang/OTP

atoms

Page 107: 1 hour dive into Erlang/OTP

A constant with name

Page 108: 1 hour dive into Erlang/OTP

lowercase

Page 109: 1 hour dive into Erlang/OTP

You can do things like...

Page 110: 1 hour dive into Erlang/OTP

1> ok.ok2> ko.ko3> ok =:= ko.false4> ok =:= ok.true

Page 111: 1 hour dive into Erlang/OTP

1> MyFuncOK = fun() -> {ok, 33} end.#Fun<erl_eval.20.82930912>2> MyFuncFail = fun() -> {error, bad_arg} end.#Fun<erl_eval.20.82930912>

3> {ok, Result} = MyFuncOK().{ok,33}4> {ok, Result2} = MyFuncFail().** exception error: no match of right hand side value {error,bad_arg}

Page 112: 1 hour dive into Erlang/OTP

1> MyFuncOK = fun() -> {ok, 33} end.#Fun<erl_eval.20.82930912>2> MyFuncFail = fun() -> {error, bad_arg} end.#Fun<erl_eval.20.82930912>

3> {ok, Result} = MyFuncOK().{ok,33}4> {ok, Result2} = MyFuncFail().** exception error: no match of right hand side value {error,bad_arg}

atoms

Page 113: 1 hour dive into Erlang/OTP

1> MyFuncOK = fun() -> {ok, 33} end.#Fun<erl_eval.20.82930912>2> MyFuncFail = fun() -> {error, bad_arg} end.#Fun<erl_eval.20.82930912>

3> {ok, Result} = MyFuncOK().{ok,33}4> {ok, Result2} = MyFuncFail().** exception error: no match of right hand side value {error,bad_arg}

atoms

errorchecking}

Page 114: 1 hour dive into Erlang/OTP

1> MyFuncOK = fun() -> {ok, 33} end.#Fun<erl_eval.20.82930912>2> MyFuncFail = fun() -> {error, bad_arg} end.#Fun<erl_eval.20.82930912>

3> {ok, Result} = MyFuncOK().{ok,33}4> {ok, Result2} = MyFuncFail().** exception error: no match of right hand side value {error,bad_arg}

atoms

errorchecking}

do����������� ������������������  they����������� ������������������  match?

Page 115: 1 hour dive into Erlang/OTP

modules

Page 116: 1 hour dive into Erlang/OTP

basic name spacing

Page 117: 1 hour dive into Erlang/OTP

public/private functions

Page 118: 1 hour dive into Erlang/OTP

You can do things like...

Page 119: 1 hour dive into Erlang/OTP

-module(cool_func).

-export([foo_bar_func/2]).

foo_bar_func (Foo, Bar) -> Result = Foo + Bar, {ok, Result}.

On your ${EDITOR} create cool_func.erl

Page 120: 1 hour dive into Erlang/OTP

On erlang shell type:

1> c("cool_module").{ok,cool_module}2> {ok, Result3} = cool_module:sum(33, 44).{ok,77}3> Result3.77

Page 121: 1 hour dive into Erlang/OTP

Too easy?

Let’s add some pattern matching on parameters!

Page 122: 1 hour dive into Erlang/OTP

area({square, Side}) -> {ok,Side * Side};area({circle, Radius}) -> % almost :-) {ok, 3 * Radius * Radius};area({triangle, A, B, C}) -> S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) -> {error, invalid_object}.

Page 123: 1 hour dive into Erlang/OTP

area({square, Side}) -> {ok,Side * Side};area({circle, Radius}) -> % almost :-) {ok, 3 * Radius * Radius};area({triangle, A, B, C}) -> S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) -> {error, invalid_object}.

means����������� ������������������  that����������� ������������������  there����������� ������������������  is����������� ������������������  another����������� ������������������  matching����������� ������������������  

posibility

Page 124: 1 hour dive into Erlang/OTP

area({square, Side}) -> {ok,Side * Side};area({circle, Radius}) -> % almost :-) {ok, 3 * Radius * Radius};area({triangle, A, B, C}) -> S = (A + B + C)/2, {ok, math:sqrt(S*(S-A)*(S-B)*(S-C))};area(Other) -> {error, invalid_object}.

means����������� ������������������  that����������� ������������������  there����������� ������������������  is����������� ������������������  another����������� ������������������  matching����������� ������������������  

posibility

mymodule:area({square, 10}).mymodule:area({circle, 10}).mymodule:area({triangle, 2, 2, 3.5}).

Page 125: 1 hour dive into Erlang/OTP

Want more?

Let’s add some recursion!

Page 126: 1 hour dive into Erlang/OTP

Want more?

Let’s add some recursion!

Erlang does not use loops

Page 127: 1 hour dive into Erlang/OTP

factorial(0) -> 1;factorial(N) -> N * factorial(N-1).

Page 128: 1 hour dive into Erlang/OTP

OTP

Page 129: 1 hour dive into Erlang/OTP

Oh, This is Perfect!

Page 130: 1 hour dive into Erlang/OTP

OTP is a set of rock solid architecture patterns, practices and tools

Page 131: 1 hour dive into Erlang/OTP

Improves the design, fault tolerance and

deployment of your app

Page 132: 1 hour dive into Erlang/OTP

Erlang is just a language, OTP makes it great for

the real world

Page 133: 1 hour dive into Erlang/OTP
Page 134: 1 hour dive into Erlang/OTP

Basic architecture patterns

Page 135: 1 hour dive into Erlang/OTP

Supervisor

S

P P P S

P P

Page 136: 1 hour dive into Erlang/OTP

Supervisor

S

P P P S

P P

Page 137: 1 hour dive into Erlang/OTP

Supervisor spawns processes and handles its death

Page 138: 1 hour dive into Erlang/OTP

gen_server

gs

P

P

P

Page 139: 1 hour dive into Erlang/OTP

gen_server

gs

P

P

P

Page 140: 1 hour dive into Erlang/OTP

Generic single threaded server

Page 141: 1 hour dive into Erlang/OTP

gen_fsm

gf

P

P

P

{S}

Page 142: 1 hour dive into Erlang/OTP

gf

gen_fsm

P

P

P

{S}

Page 143: 1 hour dive into Erlang/OTP

Generic Finite State Machine

Page 144: 1 hour dive into Erlang/OTP

gen_event

em

P

P

P

PP

P

Page 145: 1 hour dive into Erlang/OTP

gen_event

em

P

P

P

PP

P

Page 146: 1 hour dive into Erlang/OTP

Event handler, you can subscribe to events

Page 147: 1 hour dive into Erlang/OTP

More on OTP...

• File hierarchy

• Applications

• Releases

• Basic abstraction libraries (logging, system...)

Page 148: 1 hour dive into Erlang/OTP

Let’s see a gen_server in action!

Page 149: 1 hour dive into Erlang/OTP

Introducing Simple Key Value Store

Page 150: 1 hour dive into Erlang/OTP

A server that is able to store {Key,

Value} and retrieve them by Key

Page 151: 1 hour dive into Erlang/OTP

Demo II

Page 152: 1 hour dive into Erlang/OTP

ets

S

gsP

skvs diagram

Page 153: 1 hour dive into Erlang/OTP

ets

S

gsP

skvs diagram

Page 154: 1 hour dive into Erlang/OTP

Starting skvs

Go to skvs source directory

Compile./rebar compile

Start the Erlang nodeerl -pa ebin

Start the appapplication:start(skvs).

Page 155: 1 hour dive into Erlang/OTP

Some commands

Set a value

skvs_server:set(“Key”,”Value”).

skvs_server:set(“Key2”,”Value2”).

Get a value

skvs_server:get(“Key2”).

Page 156: 1 hour dive into Erlang/OTP

start_link() -> gen_server:start_link({local, skvs}, skvs_server, [], []).

get(Key) -> gen_server:call(skvs, {get, Key}).

set(Key,Value) -> gen_server:call(skvs, {set, Key, Value}).

set_fire_and_forget(Key,Value) -> gen_server:cast(skvs, {set, Key, Value}).

Code on client: skvs_server.erl

Page 157: 1 hour dive into Erlang/OTP

start_link() -> gen_server:start_link({local, skvs}, skvs_server, [], []).

get(Key) -> gen_server:call(skvs, {get, Key}).

set(Key,Value) -> gen_server:call(skvs, {set, Key, Value}).

set_fire_and_forget(Key,Value) -> gen_server:cast(skvs, {set, Key, Value}).

Code on client: skvs_server.erl

start����������� ������������������  gen_server

Page 158: 1 hour dive into Erlang/OTP

start_link() -> gen_server:start_link({local, skvs}, skvs_server, [], []).

get(Key) -> gen_server:call(skvs, {get, Key}).

set(Key,Value) -> gen_server:call(skvs, {set, Key, Value}).

set_fire_and_forget(Key,Value) -> gen_server:cast(skvs, {set, Key, Value}).

Code on client: skvs_server.erl

start����������� ������������������  gen_server

sync����������� ������������������  call����������� ������������������  to����������� ������������������  server(waits����������� ������������������  for����������� ������������������  reply)

Page 159: 1 hour dive into Erlang/OTP

start_link() -> gen_server:start_link({local, skvs}, skvs_server, [], []).

get(Key) -> gen_server:call(skvs, {get, Key}).

set(Key,Value) -> gen_server:call(skvs, {set, Key, Value}).

set_fire_and_forget(Key,Value) -> gen_server:cast(skvs, {set, Key, Value}).

Code on client: skvs_server.erl

start����������� ������������������  gen_server

async����������� ������������������  call����������� ������������������  to����������� ������������������  server(fire����������� ������������������  &����������� ������������������  forget)

sync����������� ������������������  call����������� ������������������  to����������� ������������������  server(waits����������� ������������������  for����������� ������������������  reply)

Page 160: 1 hour dive into Erlang/OTP

%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) -> {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) -> {reply, get_value(Key), State}.

%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) -> set_value(Key,Value), {noreply, State}.

Callback code: skvs_server.erl

Page 161: 1 hour dive into Erlang/OTP

%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) -> {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) -> {reply, get_value(Key), State}.

%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) -> set_value(Key,Value), {noreply, State}.

Callback code: skvs_server.erl

return����������� ������������������  value

Page 162: 1 hour dive into Erlang/OTP

%% Client Callbacks on server%% Sync calls, expects responsehandle_call({set, Key, Value}, _From,State) -> {reply, set_value(Key,Value), State};handle_call({get, Key}, _From, State) -> {reply, get_value(Key), State}.

%% Async calls, do not expect responsehandle_cast({set, Key, Value}, State) -> set_value(Key,Value), {noreply, State}.

Callback code: skvs_server.erl

return����������� ������������������  valueSurvives����������� ������������������  between����������� ������������������  

requests

Page 163: 1 hour dive into Erlang/OTP

%% Private functionsset_value(Key, Value) -> % Insert Key, Value into ETS. % Fail early {ok,Result}=case ets:insert(data_store,{Key,Value}) of true -> {ok, {Key,Value}}; {error, Reason} -> {error, Reason} end, Result.

Actual work...: skvs_server.erl

Page 164: 1 hour dive into Erlang/OTP

%% Private functionsset_value(Key, Value) -> % Insert Key, Value into ETS. % Fail early {ok,Result}=case ets:insert(data_store,{Key,Value}) of true -> {ok, {Key,Value}}; {error, Reason} -> {error, Reason} end, Result.

Actual work...: skvs_server.erl

ETS����������� ������������������  is����������� ������������������  a����������� ������������������  Key����������� ������������������  value����������� ������������������  store����������� ������������������  in����������� ������������������  RAM

Page 165: 1 hour dive into Erlang/OTP

Use cases

Page 166: 1 hour dive into Erlang/OTP

Gaming Industry

Page 167: 1 hour dive into Erlang/OTP

Gaming Industry

Page 168: 1 hour dive into Erlang/OTP

Messaging / IM

Page 169: 1 hour dive into Erlang/OTP

Messaging / IM

Page 170: 1 hour dive into Erlang/OTP

Payments

Page 171: 1 hour dive into Erlang/OTP

Payments

Page 172: 1 hour dive into Erlang/OTP

Databases

Page 173: 1 hour dive into Erlang/OTP

Databases

Page 174: 1 hour dive into Erlang/OTP

Others

Page 175: 1 hour dive into Erlang/OTP

Others

Page 176: 1 hour dive into Erlang/OTP

Some day...

Page 177: 1 hour dive into Erlang/OTP

Every highly interactive website? ;)

Page 178: 1 hour dive into Erlang/OTP

Every highly interactive website? ;)

Hi

Hey

HelloPayOuch!

!BufNO!

Would you...

Page 179: 1 hour dive into Erlang/OTP

Internet of things

Page 180: 1 hour dive into Erlang/OTP

And now what?

Page 181: 1 hour dive into Erlang/OTP

Install Erlang/OTP!

https://www.erlang-solutions.com/downloads/download-erlang-otp

Page 182: 1 hour dive into Erlang/OTP

Use a great IDE• IntelliJ + Erlang (plugin)

• Sublime Text + SublimErl

• Emacs

Page 183: 1 hour dive into Erlang/OTP

Read some books

Page 184: 1 hour dive into Erlang/OTP

RTFM

• http://www.erlang.org/doc.html

• http://erldocs.com/

Page 185: 1 hour dive into Erlang/OTP

Join mailing lists

• Official Erlang mailing list

• http://erlang.org/mailman/listinfo/erlang-questions

• Join Barcelona Erlang user group on google groups

Page 186: 1 hour dive into Erlang/OTP

Find know-how

• Check http://www.erlang-factory.com/ videos

• Search for Erlang conferences on slideshare

• Pray for an http://erlangcamp.com/ near your city

Page 187: 1 hour dive into Erlang/OTP

Read some code

Page 188: 1 hour dive into Erlang/OTP

This is the result of 6 months of fun learning Erlang/OTP

Page 189: 1 hour dive into Erlang/OTP

We are looking for awesome projects

http://es.linkedin.com/in/jllonch

http://es.linkedin.com/in/jvalduvieco

Page 190: 1 hour dive into Erlang/OTP

Thank you!

https://github.com/jvalduvieco/betabeers_201303

Page 191: 1 hour dive into Erlang/OTP

Thank you!

we����������� ������������������  hope����������� ������������������  you����������� ������������������  feel����������� ������������������  like����������� ������������������  him����������� ������������������  :)

(we����������� ������������������  did)

https://github.com/jvalduvieco/betabeers_201303

Page 192: 1 hour dive into Erlang/OTP

Erlang, The Movie

http://www.youtube.com/watch?v=uKfKtXYLG78

Page 193: 1 hour dive into Erlang/OTP

Images sourceshttp://diromero.wordpress.com/2011/10/07/home-automation/http://iffergan.com/http://technode.com/2012/05/14/internet-of-things-not-just-a-concept-for-fund-raising/http://modernherstory.wordpress.com/2011/05/18/open-minds-open-hearts/http://www.i-m.co/eviemouse/problemsolved/http://strategicsalesmarketingosmg.wordpress.com/2012/06/25/who-are-twitter-users/http://www.secureworks.com/it_security_services/advantage/soc/http://labourlist.org/2011/11/my-biggest-worry-about-labour%E2%80%99s-future/broken-chain/http://prothemedesign.com/how-to/why-you-should-keep-wordpress-and-your-plugins-up-to-date/attachment/matrix-code-update/http://thegapbetweentwoworlds.com/category/career-transitionhttp://www.wingweb.co.uk/Images/697/KC-10_Extender_SR-71_Blackbirdhttp://www.tonywoodtraining.com/2010/11/29/motivation-to-exercise-and-eat-right/http://portalmie.com/blog/6/2011/09/kushimoto-diving-park-tokushima-revista-no-7/http://www.bcsagroup.com/http://www.losemyaccent.com/tag/hand/http://articulo.mercadolibre.com.ar/MLA-450970296-cortaplumas-victorinox-huntsman-15-usos-navaja-origen-suizo-_JMhttp://openclipart.org/detail/4112/http://blog-en.coaching-go.com/2012/02/learn-to-say-no-and-know-how-to-say-yes/ok-2/http://en.wikipedia.org/wiki/File:We_Can_Do_It!.jpghttp://triniblog.wordpress.com/tag/laberintos/http://www.catskillsnyrealestate.com/Want-More-Info-Catskill-Real-Estate.phphttp://www.thelovelyplanet.net/lush-green-images-planet-earth/http://www.growthmax.com/how-exactly-does-growthmax-plus-make-you-grow-taller/http://www.ayurvedalive.in/keep-your-eyes-healthy-beautifulhttp://my.mmosite.com/510619/blog/item/lord_of_the_rings_online.htmlhttp://scifistorm.org/2010/03/24/lord-of-the-rings-blu-ray-review/http://www.123rf.com/photo_15612022_3d-cartoon-bug.htmlhttp://www.airliners.net/photo/1134244/L/http://www.muypymes.com/2009/12/04/moleskine-mucho-mas-que-una-agenda-personalhttp://learnyousomeerlang.com/the-hitchhikers-guide-to-concurrencyhttp://planetpooks.com/one-more-reason-to-love-hermiones-handbag/