introduction to erlang/otp

56
I ntroduction to Erlang/OTP @jordillonch

Upload: jordi-llonch

Post on 12-Feb-2017

397 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Introduction to Erlang/OTP

Introduction to Erlang/OTP

@jordillonch

Page 2: Introduction to Erlang/OTP

1 hour dive into Erlang/OTP

@jvalduvieco @jordillonch

BASED ON

Page 3: Introduction to Erlang/OTP

Problem domain

Page 4: Introduction to Erlang/OTP

Lots of users

Page 5: Introduction to Erlang/OTP

24x7x365

Page 6: Introduction to Erlang/OTP

Lots of critical concurrent transactions

Page 7: Introduction to Erlang/OTP

Hardware or software breaks

Page 8: Introduction to Erlang/OTP

Lots of code changes

Page 9: Introduction to Erlang/OTP

Unscalable Unmaintainable } code

Page 10: Introduction to Erlang/OTP

Maintenance/Debug in

production system

Page 11: Introduction to Erlang/OTP

Does it sound to you?

Page 12: Introduction to Erlang/OTP

The Erlang solution

Page 13: Introduction to Erlang/OTP

Simplicity...

Page 14: Introduction to Erlang/OTP

Minimize defensive programming

Page 15: Introduction to Erlang/OTP

Typeless variables

Page 16: Introduction to Erlang/OTP

Develop by contract

Page 17: Introduction to Erlang/OTP

t-shirt function size

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

Page 18: Introduction to Erlang/OTP

Single responsibility principle

Page 19: Introduction to Erlang/OTP

No shared state between entities

Page 20: Introduction to Erlang/OTP

High ConcurrencyHigh concurrency

Page 21: Introduction to Erlang/OTP

Light threadshundreds of thousands of threads in one

machine with a good cpu scheduler

Lt

Lt

Lt

Page 22: Introduction to Erlang/OTP

Lt

Message passingA shared nothing architecture that

communicates through message passing

Lt

Lt

Page 23: Introduction to Erlang/OTP

Lt

Process MailboxEvery process has a mailbox with incoming

messages, process take on convenience

Lt

Lt

Page 24: Introduction to Erlang/OTP

Lt

No shared stateEvery process its own internal state stored in

a variable avoiding lock contention

Lt

LtS

S

S

Page 25: Introduction to Erlang/OTP

Soft realtime

You have no strict guarantees on latency but language is designed

to have low latency

Page 26: Introduction to Erlang/OTP

High availabilityHigh availability

Page 27: Introduction to Erlang/OTP

Pa

Supervised processesprocesses can be monitored by other

processes, handling its termination

Pb

Page 28: Introduction to Erlang/OTP

Pb

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

Pa

PbPb2

S

Page 29: Introduction to Erlang/OTP

Hot code updateprocesses code and data can be replaced

without loosing service

Pa v1Pa v2

S

Pb

Pc

Page 30: Introduction to 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 31: Introduction to Erlang/OTP

How does it look like?

Page 32: Introduction to Erlang/OTP
Page 33: Introduction to 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}.

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

Page 34: Introduction to Erlang/OTP

OTP

Page 35: Introduction to Erlang/OTP

Oh, This is Perfect!

Page 36: Introduction to Erlang/OTP

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

Page 37: Introduction to Erlang/OTP

Improves the design, fault tolerance and

deployment of your app

Page 38: Introduction to Erlang/OTP

Erlang is just a language, OTP makes it great for

the real world

Page 39: Introduction to Erlang/OTP
Page 40: Introduction to Erlang/OTP

Basic architecture patterns

Page 41: Introduction to Erlang/OTP

Supervisor

S

P P P S

P P

!P

Page 42: Introduction to Erlang/OTP

Supervisor spawns processes and handles its death

Page 43: Introduction to Erlang/OTP

gen_server

gs

P

P

P

Page 44: Introduction to Erlang/OTP

Generic single threaded server

Page 45: Introduction to Erlang/OTP

gfgfgf

gen_fsm

gf

P

P

P

{S}

Page 46: Introduction to Erlang/OTP

Generic Finite State Machine

Page 47: Introduction to Erlang/OTP

gen_event

em

P

P

P

PP

P

Page 48: Introduction to Erlang/OTP

Event handler, you can subscribe to events

Page 49: Introduction to Erlang/OTP

More on OTP...

• File hierarchy

• Applications

• Releases

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

Page 50: Introduction to Erlang/OTP

Use cases

Page 51: Introduction to Erlang/OTP

Gaming Industry

Page 52: Introduction to Erlang/OTP

Messaging / IM

Page 53: Introduction to Erlang/OTP

Payments

Page 54: Introduction to Erlang/OTP

Databases

Page 55: Introduction to Erlang/OTP

Thanks!

Page 56: Introduction to 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/