ndc london 2014: erlang patterns matching business needs

73
Erlang Patterns Matching Business Needs Torben Hoffmann CTO @ Erlang Solutions [email protected] @LeHoff

Upload: torben-hoffmann

Post on 17-Jul-2015

462 views

Category:

Software


0 download

TRANSCRIPT

Page 1: NDC London 2014: Erlang Patterns Matching Business Needs

Erlang Patterns Matching Business

Needs

Torben HoffmannCTO @ Erlang Solutions

[email protected]@LeHoff

Page 2: NDC London 2014: Erlang Patterns Matching Business Needs
Page 3: NDC London 2014: Erlang Patterns Matching Business Needs

Background

Erlanger since 2006

Happiness

Mission critical gateway for Tetra

Hard work

Major learnings

Page 4: NDC London 2014: Erlang Patterns Matching Business Needs

Why this talk?

Encourage you to embrace failure

Show how The Golden Trinity of Erlang delivers business value

Spread the Erlang love

Page 5: NDC London 2014: Erlang Patterns Matching Business Needs

Dealing with disbelievers…

Source: http://2.bp.blogspot.com/-qNM3LGTtUYM/UIFLJGd_MLI/AAAAAAAAAnU/GCtI5SYfbCs/s320/orc-army.jpg

source: http://www.rottentomatoes.com/m/1014027-mission/source: http://asset3.cbsistatic.com/cnwk.1d/i/tim2/2013/08/12/Larry_Ellison_Oracle_Open_World_2009_610x407.jpg

source: http://images1.wikia.nocookie.net/__cb20110119125642/villains/images/e/ef/Saruman.jpg

Page 6: NDC London 2014: Erlang Patterns Matching Business Needs

What Does Business Need?

Page 7: NDC London 2014: Erlang Patterns Matching Business Needs

wantedproductivity

no down-time

something that always works

Page 8: NDC London 2014: Erlang Patterns Matching Business Needs

wantedmoney

money

money

it’s a rich man’s world!

Source: http://zapp5.staticworld.net/images/article/2011/10/cash_bag_of_money-5225858.jpg

Page 9: NDC London 2014: Erlang Patterns Matching Business Needs

Distributed Systems

source: http://www.krug-soft.com/297.html

How likely is it that this will “just work”?

Page 10: NDC London 2014: Erlang Patterns Matching Business Needs

Failure is unavoidable

Software Errors Cost

U.S. Economy

Annually

(NIST 2002*)* http://www.abeacha.com/NIST_press_release_bugs_cost.htm

$59.5 Billion

Page 11: NDC London 2014: Erlang Patterns Matching Business Needs

The thinking it took to get us into this mess

is not the same thinking that is going to

get us out of it.

Page 12: NDC London 2014: Erlang Patterns Matching Business Needs

Source: http://www.sustainwellbeing.net/lemmings.html

Page 13: NDC London 2014: Erlang Patterns Matching Business Needs

Why Erlang?

Page 14: NDC London 2014: Erlang Patterns Matching Business Needs

Source: http://zapp5.staticworld.net/images/article/2011/10/cash_bag_of_money-5225858.jpg

Page 15: NDC London 2014: Erlang Patterns Matching Business Needs

Source:http://daynamartin.com/unschooling-blog/truth-in-the-fun-zone/

Page 16: NDC London 2014: Erlang Patterns Matching Business Needs

Source: http://www.americanscientist.org/issues/id.3489,y.0,no.,content.true,page.1,css.print/issue.aspx

Polyglot is Good™

Page 17: NDC London 2014: Erlang Patterns Matching Business Needs

Erlang not on the map is the #1 reason to learn it

Page 18: NDC London 2014: Erlang Patterns Matching Business Needs

Any sufficiently complicated concurrent program in another language contains an ad hoc informally-specified bug-ridden slow implementation of half of Erlang.”

Robert Virding First Rule of Programming

Page 19: NDC London 2014: Erlang Patterns Matching Business Needs

Fighting Failures

Page 20: NDC London 2014: Erlang Patterns Matching Business Needs

Defensive Coding

Source:  h*p://www.slideshare.net/JanHenryNystrom/produc;vity-­‐gains-­‐in-­‐erlang

Data  Mobility  component  breakdown

25% Defensive

<20% App

You are loosing money!

Page 21: NDC London 2014: Erlang Patterns Matching Business Needs

Heisenbugs Jim Gray - Why Computers Stop

http://www.hpl.hp.com/techreports/tandem/TR-85.7.pdf

Heisenbug = goes away when retrying

Bohrbug = a real bug

Saw 132 faults - 131 were Heisenbugs

Page 22: NDC London 2014: Erlang Patterns Matching Business Needs

Keys to Fault Tolerance Jim Gray - Why Computers Stop

software modularity through processes and message passing

fault containment through fail-fast software

process-pairs to tolerate transient hardware & software faults

transaction mechanism to give data and message integrity

transaction mechanism combined with process-pairs to ease exception handling and tolerate software faults

http://www.hpl.hp.com/techreports/tandem/TR-85.7.pdf

Page 23: NDC London 2014: Erlang Patterns Matching Business Needs

Separation of Concerns

Not embracing failure means you loose the ability to handle failures gracefully!

Golden Path Failure Handling

BAD!GOOD!!!

Page 24: NDC London 2014: Erlang Patterns Matching Business Needs

The Golden Trinity Of Erlang

Page 25: NDC London 2014: Erlang Patterns Matching Business Needs

Share Nothing

Page 26: NDC London 2014: Erlang Patterns Matching Business Needs

To Share Or Not To Share

Memory Memory Memory

P1 P2 P1 P2

Corrupt Corrupt

Page 27: NDC London 2014: Erlang Patterns Matching Business Needs

Message Passing

P1 P2M

P1 sends M to P2.

Every process has a mailbox

Messages are received:receive {tag, Value} -> Value; N when is_integer(N) -> N + 42

end

Page 28: NDC London 2014: Erlang Patterns Matching Business Needs

Fail Fast

Page 29: NDC London 2014: Erlang Patterns Matching Business Needs

Failures

Anything that can go wrong, will go wrong.

Murphy

Programming errors Disk failures Network failures

source: http://www.krug-soft.com/297.html

Whatever can happen will happen.

De Morgan

Page 30: NDC London 2014: Erlang Patterns Matching Business Needs

Fault In-ToleranceMost programming paradigmes are fault in-tolerant ⇒ must deal with all errors or die

source: http://www.thelmagazine.com/BrooklynAbridged/archives/2013/05/14/should-we-be-worried-about-this-brooklyn-measles-outbreak

Page 31: NDC London 2014: Erlang Patterns Matching Business Needs

Fault Tolerance

Erlang is fault tolerant by design ⇒ failures are embraced and managed

source: http://johnkreng.wordpress.com/tag/jean-claude-van-damme/

Page 32: NDC London 2014: Erlang Patterns Matching Business Needs

.

Let It Failconvert(monday) -> 1;convert(tuesday) -> 2;convert(wednesday) -> 3;convert(thursday) -> 4;convert(friday) -> 5; convert(saturday) -> 6;convert(sunday) -> 7

Erlang encourages offensive programming

;convert(_) -> {error, unknown_day}.

Page 33: NDC London 2014: Erlang Patterns Matching Business Needs

Supervision

Page 34: NDC London 2014: Erlang Patterns Matching Business Needs

Handling Failure

P1 P2

P1 monitors P2.

P1 P2

P1 and P2 are linked.

Page 35: NDC London 2014: Erlang Patterns Matching Business Needs

PROPAGATING EXIT SIGNALS

PidA PidB

{'EXIT', PidA, Reason}

PidC

{'EXIT', PidB, Reason}

Page 36: NDC London 2014: Erlang Patterns Matching Business Needs

TRAPPING AN EXIT SIGNAL

PidA

{'EXIT', PidA, Reason}

PidC

PidB

Page 37: NDC London 2014: Erlang Patterns Matching Business Needs

© 1999-2012 Erlang Solutions Ltd.

Supervision Trees

37

worker worker

worker workerworkerThe OTP library is built on this principle

supervisor

supervisor

Page 38: NDC London 2014: Erlang Patterns Matching Business Needs

© 1999-2012 Erlang Solutions Ltd.

The OTP Supervisor

38

worker workerworker

supervisor

Specifies a default restart strategy

one_for_one one_for_all rest_for_one simple_one_for_one

Child spec for how a child is restarted

permanent | transient | temporary

Page 39: NDC London 2014: Erlang Patterns Matching Business Needs

Supervision Patterns

Page 40: NDC London 2014: Erlang Patterns Matching Business Needs

Supervisors

Page 41: NDC London 2014: Erlang Patterns Matching Business Needs

Simple Manager/Worker Pattern

Page 42: NDC London 2014: Erlang Patterns Matching Business Needs

Intentional Programming

a style of programming where the reader of a program can easily see what the programmer intended by their code. [1]

[1] http://www.erlang.org/download/armstrong_thesis_2003.pdf

Page 43: NDC London 2014: Erlang Patterns Matching Business Needs

Intentional Dictionary

data retrieval - dict:fetch(Key, Dict) = Val | EXIT

the programmer knows a specific key should be in the dictionary and it is an error if it is not.

search - dict:find(Key, Dict) = {ok, Val} | error.

it is unknown if the key is there or not and both cases must be dealt with.

test - dict:is_key(Key, Dict) = Boolean

Page 44: NDC London 2014: Erlang Patterns Matching Business Needs

Saving Money

Page 45: NDC London 2014: Erlang Patterns Matching Business Needs

Benefits of let-it-fail

code that solves the problem

Erlang @ 3xSource:  h*p://www.slideshare.net/JanHenryNystrom/produc;vity-­‐gains-­‐in-­‐erlang

Data  Mobility  component  breakdown

Page 46: NDC London 2014: Erlang Patterns Matching Business Needs

Productivity with Erlang @ Motorola

Goal: Development of a mission-critical telecom gateway for TETRA

To be developed from scratch using Erlang and some drivers in C

The gateway translates between proprietary protocols and the ISI standard

Developed using a two – six man team over four year

Page 47: NDC London 2014: Erlang Patterns Matching Business Needs

Productivity with Erlang @ Motorola

Internal storage

input

input

input

inputoutput

output

output

Function point analysis

Language agnostic measurement of problem size

Page 48: NDC London 2014: Erlang Patterns Matching Business Needs

Show me the money!

Conservative estimation of the number of… inputs, outputs and internal storage

Includes design, box test, system test, project management efforts

Function Point Analysis of the size of the problem

Page 49: NDC London 2014: Erlang Patterns Matching Business Needs

Dealing with deadlocks

7 years of coding Erlang

Time spent on deadlock issues….

1 hour (due to lack of experience with OTP)

Page 50: NDC London 2014: Erlang Patterns Matching Business Needs

Realities of software development

Source: http://www.thejournal.ie/readme/lunch-atop-skyscraper-photo-men-irish-shanaglish-518110-Jul2012/

ProductOwner????

Page 51: NDC London 2014: Erlang Patterns Matching Business Needs

Business benefits of supervisors

Only one process dies

isolation gives continuous service

Everything is logged

you know what is wrong

Corner cases can be fixed at leisure

Product owner in charge!

Not the software!

Software architecture that supports

iterative development

Page 52: NDC London 2014: Erlang Patterns Matching Business Needs

Where To Use Erlang

Page 53: NDC London 2014: Erlang Patterns Matching Business Needs

The Sweet SpotGUI

Drivers

Middleware Coordination Control

Page 54: NDC London 2014: Erlang Patterns Matching Business Needs

Erlang History

Page 55: NDC London 2014: Erlang Patterns Matching Business Needs

One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so

complicated that there are no obvious deficiencies.

- C.A.R. Hoare

There are two ways of constructing a software design:

Page 56: NDC London 2014: Erlang Patterns Matching Business Needs

Erlang’s Original Requirements

Large scale concurrency

Soft real-time

Distributed systems

Hardware interaction

Very large software systems

Complex functionality

Continuous operation for many years

Software maintenance on-the-fly

High quality and reliability

Fault tolerance Bjarne Däcker’s Licentiate Thesis: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.88.1957

Page 57: NDC London 2014: Erlang Patterns Matching Business Needs

General vs Domain Specific

Telecom

Erlang

C++/Java

Smaller gap =

money!

Page 58: NDC London 2014: Erlang Patterns Matching Business Needs

If the glove fits...

drivers coordination GUI

needs/fit

C

Erlang

Telecom

Page 59: NDC London 2014: Erlang Patterns Matching Business Needs

If our basic tool, the language in which we design and code our programs, is also complicated, the language itself becomes part of the problem rather than part of its solution.

C.A.R. Hoare

Page 60: NDC London 2014: Erlang Patterns Matching Business Needs

Good Erlang Domains

Low latency over throughput

Stateful (in contrast to being stateless)

Massively concurrent

Distributed

Fault tolerant

Uses OTP

Non-stop operation

Under load, Erlang programs usually performs as well as programs in other languages, often way better.

@jlouis

Page 61: NDC London 2014: Erlang Patterns Matching Business Needs

Other Erlang Domains Messaging - XMPP et al

ejabberd, MongooseIM

Webservers

Yaws, Chicago Boss, Cowboy

Payment switches & soft switches

Vocalink, OpenFlow/LINC

Distributed Databases

Riak, CouchDB, Scalaris

Queueing systems

RabbitMQ (AMQP)

Page 62: NDC London 2014: Erlang Patterns Matching Business Needs

The glove fits!Low

latency Stateful Massively concurrent Distributed Fault tolerant

Messaging

Webservers

Soft switches

Distributed DBs

Queueing systems

Page 63: NDC London 2014: Erlang Patterns Matching Business Needs

CustomersSome

19,000,000,000 reasons to use Erlang

Page 64: NDC London 2014: Erlang Patterns Matching Business Needs

WhatsApp

Real-time Messaging

Text and Pictures

Group chat

Page 65: NDC London 2014: Erlang Patterns Matching Business Needs

WhatsApp Numbers

10 Erlang engineers

~500M monthly users

19B msg/day in / 40B msg/day out

147M concurrent connections

peak: 324K msg/s in / 712K msg/s out

Page 66: NDC London 2014: Erlang Patterns Matching Business Needs

WhatsApp Hardware

~550 servers

2x2690v2 Ivy Bridge 10-core (40 threads total)

64-512 GB RAM

SSD

>11,000 cores

Page 67: NDC London 2014: Erlang Patterns Matching Business Needs

Bet365 Research team re-did a Java system in

Erlang as a POC

Results:

5x connected users in load test

4x rate of data change

Better utilisation of CPU resources

How to convince the developers to switch?

Page 68: NDC London 2014: Erlang Patterns Matching Business Needs

Bet365 showed that Erlang…

makes programming fun

scales and is reliable

has enough depth to be interesting

solves difficult problems with simple code

Results:

production teams quickly started to appreciate the benefits of Erlang

did not want to go back to Java https://www.erlang-solutions.com/resources/webinars/webinar-recording-erlang-gamblingonline-betting

Page 69: NDC London 2014: Erlang Patterns Matching Business Needs

Learning Erlang

Page 70: NDC London 2014: Erlang Patterns Matching Business Needs

Source: http://www.despair.com/mistakes.html

Page 71: NDC London 2014: Erlang Patterns Matching Business Needs

Learning Erlang

ESL training courses

Learn You Some Erlang

http://learnyousomeerlang.com/

Use the erlang-questions mailing list

Do it hands-on

Give it time to sink in!!!

Page 72: NDC London 2014: Erlang Patterns Matching Business Needs

Elixir

Built on top of the Erlang VM

More Ruby-like syntax

Hygienic macros - easy to do DSLs

Better support for data handling

But… you still have to learn the Erlang programming model

Page 73: NDC London 2014: Erlang Patterns Matching Business Needs

Cruising with Erlang

Understand the failure model

Embrace failure!

Use supervision patterns to deliver business value

Stay in charge!