Introduction to Erlang/OTP

Post on 12-Feb-2017

392 views 0 download

Transcript of Introduction to Erlang/OTP

Introduction to Erlang/OTP

@jordillonch

1 hour dive into Erlang/OTP

@jvalduvieco @jordillonch

BASED ON

Problem domain

Lots of users

24x7x365

Lots of critical concurrent transactions

Hardware or software breaks

Lots of code changes

Unscalable Unmaintainable } code

Maintenance/Debug in

production system

Does it sound to you?

The Erlang solution

Simplicity...

Minimize defensive programming

Typeless variables

Develop by contract

t-shirt function size

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

Single responsibility principle

No shared state between entities

High ConcurrencyHigh concurrency

Light threadshundreds of thousands of threads in one

machine with a good cpu scheduler

Lt

Lt

Lt

Lt

Message passingA shared nothing architecture that

communicates through message passing

Lt

Lt

Lt

Process MailboxEvery process has a mailbox with incoming

messages, process take on convenience

Lt

Lt

Lt

No shared stateEvery process its own internal state stored in

a variable avoiding lock contention

Lt

LtS

S

S

Soft realtime

You have no strict guarantees on latency but language is designed

to have low latency

High availabilityHigh availability

Pa

Supervised processesprocesses can be monitored by other

processes, handling its termination

Pb

Pb

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

Pa

PbPb2

S

Hot code updateprocesses code and data can be replaced

without loosing service

Pa v1Pa v2

S

Pb

Pc

P

DistributionProcesses run on nodes and can be located

wherever they are

Node

P

PP

P

Node

P

P

P

PP PP

How does it look like?

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}.

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

OTP

Oh, This is Perfect!

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

Improves the design, fault tolerance and

deployment of your app

Erlang is just a language, OTP makes it great for

the real world

Basic architecture patterns

Supervisor

S

P P P S

P P

!P

Supervisor spawns processes and handles its death

gen_server

gs

P

P

P

Generic single threaded server

gfgfgf

gen_fsm

gf

P

P

P

{S}

Generic Finite State Machine

gen_event

em

P

P

P

PP

P

Event handler, you can subscribe to events

More on OTP...

• File hierarchy

• Applications

• Releases

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

Use cases

Gaming Industry

Messaging / IM

Payments

Databases

Thanks!

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/