message driven architecture enables elasticity, resilience and availability

67
Message / Event Driven Arch Tim Harper, Software Craftsman

Upload: tim-harper

Post on 19-Aug-2015

54 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Message Driven Architecture enables Elasticity, Resilience and Availability

Message / Event Driven Arch

Tim Harper, Software Craftsman

Page 2: Message Driven Architecture enables Elasticity, Resilience and Availability

Me

Page 3: Message Driven Architecture enables Elasticity, Resilience and Availability

Books

Page 4: Message Driven Architecture enables Elasticity, Resilience and Availability

Books

+ anything out of this

guy’s mouth

Page 5: Message Driven Architecture enables Elasticity, Resilience and Availability

Messaging / Event Oriented Arch

Tim Harper, Software Craftsman

Page 6: Message Driven Architecture enables Elasticity, Resilience and Availability

Why use privates in code?/** * INTERNAL API */ private[akka] object StreamLayout {

// compile-time constant final val Debug = false

final def validate(m: Module, level: Int = 0, doPrint: Boolean = false, idMap: mutable.Map[AnyRef, Int] = mutable.Map.empty): Unit = { val ids = Iterator from 1 def id(obj: AnyRef) = idMap get obj match { case Some(x) => x

Page 7: Message Driven Architecture enables Elasticity, Resilience and Availability

Why use privates in code?/** * INTERNAL API */ private[akka] object StreamLayout {

// compile-time constant final val Debug = false

final def validate(m: Module, level: Int = 0, doPrint: Boolean = false, idMap: mutable.Map[AnyRef, Int] = mutable.Map.empty): Unit = { val ids = Iterator from 1 def id(obj: AnyRef) = idMap get obj match { case Some(x) => x

Page 8: Message Driven Architecture enables Elasticity, Resilience and Availability

Why use privates in code?/** * INTERNAL API */ private[akka] object StreamLayout {

// compile-time constant final val Debug = false

final def validate(m: Module, level: Int = 0, doPrint: Boolean = false, idMap: mutable.Map[AnyRef, Int] = mutable.Map.empty): Unit = { val ids = Iterator from 1 def id(obj: AnyRef) = idMap get obj match { case Some(x) => x

Page 9: Message Driven Architecture enables Elasticity, Resilience and Availability

Why use privates in code?

Page 10: Message Driven Architecture enables Elasticity, Resilience and Availability

• Good API design

Why use privates in code?

Page 11: Message Driven Architecture enables Elasticity, Resilience and Availability

• Good API design

• It’s easier to add functionality than remove it / change it

Why use privates in code?

Page 12: Message Driven Architecture enables Elasticity, Resilience and Availability

• Good API design

• It’s easier to add functionality than remove it / change it

• Less guarantees = less coupling

Why use privates in code?

Page 13: Message Driven Architecture enables Elasticity, Resilience and Availability

• Good API design

• It’s easier to add functionality than remove it / change it

• Less guarantees = less coupling

Don’t “pay the cost of a rich API without any of the benefits”

Why use privates in code?

Page 14: Message Driven Architecture enables Elasticity, Resilience and Availability

“Private” return value?trait Publisher[T] { def subscribe(sub: Subscriber[T]): Unit } trait Subscription { def requestMore(n: Int): Unit def cancel(): Unit } trait Subscriber[T] { def onSubscribe(s: Subscription): Unit def onNext(elem: T): Unit def onError(thr: Throwable): Unit def onComplete(): Unit }

Page 15: Message Driven Architecture enables Elasticity, Resilience and Availability

“Private” return value?trait Publisher[T] { def subscribe(sub: Subscriber[T]): Unit } trait Subscription { def requestMore(n: Int): Unit def cancel(): Unit } trait Subscriber[T] { def onSubscribe(s: Subscription): Unit def onNext(elem: T): Unit def onError(thr: Throwable): Unit def onComplete(): Unit }

Page 16: Message Driven Architecture enables Elasticity, Resilience and Availability

“Private” return value?trait Publisher[T] { def subscribe(sub: Subscriber[T]): Unit } trait Subscription { def requestMore(n: Int): Unit def cancel(): Unit } trait Subscriber[T] { def onSubscribe(s: Subscription): Unit def onNext(elem: T): Unit def onError(thr: Throwable): Unit def onComplete(): Unit }

Page 17: Message Driven Architecture enables Elasticity, Resilience and Availability

Private result?

Page 18: Message Driven Architecture enables Elasticity, Resilience and Availability

Private result?• Schedule now or then

Page 19: Message Driven Architecture enables Elasticity, Resilience and Availability

Private result?• Schedule now or then

• Run it here or there

Page 20: Message Driven Architecture enables Elasticity, Resilience and Availability

Private result?• Schedule now or then

• Run it here or there

• Recovery, prioritization, etc.

Page 21: Message Driven Architecture enables Elasticity, Resilience and Availability

Private result?• Schedule now or then

• Run it here or there

• Recovery, prioritization, etc.

Don’t “pay the cost of a synchronous API without any of the benefits”

Page 22: Message Driven Architecture enables Elasticity, Resilience and Availability

Reactive Manifesto

Page 23: Message Driven Architecture enables Elasticity, Resilience and Availability

Reactive Manifesto

Page 24: Message Driven Architecture enables Elasticity, Resilience and Availability

Reactive Manifesto

It all starts here.

Page 25: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

• Flip the arrows

Page 26: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Page 27: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Page 28: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Schedule Ingestion Task

Page 29: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Notify SalesForce

Schedule Ingestion Task

Page 30: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Record in ElasticSearch

Notify SalesForce

Schedule Ingestion Task

Page 31: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Record in ElasticSearch

Notify SalesForce

Schedule Ingestion Task

Page 32: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Record in ElasticSearch

Notify SalesForce

Schedule Ingestion Task

Such knowledge

Page 33: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Record in ElasticSearch

Notify SalesForce

Schedule Ingestion Task

Such knowledge

Very couple

Page 34: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Send Email to User

Record in ElasticSearch

Notify SalesForce

Schedule Ingestion Task

Such knowledge

Very couple

Wow

Page 35: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Record in ElasticSearch

Send Email to User

Schedule Ingestion Task

Notify SalesForce

Page 36: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Concert added to database

Record in ElasticSearch

Send Email to User

Schedule Ingestion Task

Notify SalesForce

Page 37: Message Driven Architecture enables Elasticity, Resilience and Availability

Event OrientedConcert added

to database

Record in ElasticSearch

Send Email to User

Schedule Ingestion Task

Notify SalesForce

Announce

Listen for announce

Listen for announce

Listen for announce

Listen for announce

Page 38: Message Driven Architecture enables Elasticity, Resilience and Availability

Event Oriented

Notify SalesForce

Listen for promotion

event

Listen for new concert event

Listen for concert update

event

Page 39: Message Driven Architecture enables Elasticity, Resilience and Availability

Functional Reactive Programming

Event X happened@ time Y

Page 40: Message Driven Architecture enables Elasticity, Resilience and Availability

Functional Reactive Programming

Event X happened@ time Y

AS TRUE TODAY AS

WHEN IT WAS WRITTEN

Page 41: Message Driven Architecture enables Elasticity, Resilience and Availability

Functional Reactive Programming

A @ 1:00

B @ 1:01

C @ 1:05

D @ 1:06

Page 42: Message Driven Architecture enables Elasticity, Resilience and Availability

Functional Reactive Programming

A @ 1:00

B @ 1:01

C @ 1:05

D @ 1:06

F @ 1:00

G @ 1:01

H @ 1:05

I @ 1:06

Page 43: Message Driven Architecture enables Elasticity, Resilience and Availability

FRPA @ 1:00

B @ 1:01

C @ 1:05

D @ 1:06

REDUCE (cache layer)

Page 44: Message Driven Architecture enables Elasticity, Resilience and Availability

FRPA @ 1:00

B @ 1:01

C @ 1:05

D @ 1:06

REDUCE (cache layer)

It’s beginning to look

a lot like CQRS

Page 45: Message Driven Architecture enables Elasticity, Resilience and Availability

FRP / CQRS

• “Travel through time”

• Retain all data (UPDATE = DELETE PRIOR STATE)

• Full audit log

• Separation of read / write concerns; et. al

Page 46: Message Driven Architecture enables Elasticity, Resilience and Availability

Messaging Patterns

• Queue

• Topic

• Exchange

Page 47: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Direct

Orange 1

Publish

“oranges”

“oranges” queue

Orange 3

Orange 2

Page 48: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Direct

Publish

“oranges”

“oranges” queue

Orange 3

Orange 2

Orange 1

Page 49: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Broadcast / Fanout

Orange 1

Publish

“fruit” exchange

“fruit”

Orange 3

Orange 2

“products”

Orange 3

Orange 2

Page 50: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Broadcast / Fanout

Publish

“fruit” exchange

“fruit”

Orange 3

Orange 2

“products”

Orange 3

Orange 2

Orange 1

Orange 1

Page 51: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Broadcast / Fanout

Publish

“fruit” exchange

“fruit”

Orange 3

Orange 2

“products”

Orange 3

Orange 2

Orange 1

Orange 1

AKA PubSub

Page 52: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Topic / PubSub

Orange 1

Publish

“fruit” exchange topic: fruit.orange

“all-fruit” fruit.*

Apple 6

Apple 4

“apples” fruit.apples

Apple 6

Apple 4

Page 53: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Topic / PubSub

Publish

“fruit” exchange topic: fruit.orange

“all-fruit” fruit.*

Apple 6

Apple 4

“apples” fruit.apples

Apple 6

Apple 4

Orange 1

Page 54: Message Driven Architecture enables Elasticity, Resilience and Availability

Orange 1

Messaging Patterns: Topic / PubSub

Publish

“fruit” exchange topic: fruit.orange

“all-fruit” fruit.*

Apple 6

Apple 4

“apples” fruit.apples

Apple 6

Apple 4

Orange 1

“Yo dawgs,

this thing happened”

Page 55: Message Driven Architecture enables Elasticity, Resilience and Availability

Message Broker Benefits

• Messages are routes and enqueued while app not booting

• Centralized point of coordination

• Replication, persistence, etc.

• If you don’t use one then you will build one.

Page 56: Message Driven Architecture enables Elasticity, Resilience and Availability

Delivery guarantees

• At least once

• At most once

Page 57: Message Driven Architecture enables Elasticity, Resilience and Availability

Delivery guarantees

Network Process

acksend receive

Broker

Page 58: Message Driven Architecture enables Elasticity, Resilience and Availability

Delivery guarantees

Page 59: Message Driven Architecture enables Elasticity, Resilience and Availability

Idempotence

• fn(fn(x)) = fn(x)

• Great idea in general

• (NOT impotency)

Page 60: Message Driven Architecture enables Elasticity, Resilience and Availability

Idempotence

• The operation to perform is a function of the current state and next command.

Page 61: Message Driven Architecture enables Elasticity, Resilience and Availability

Cons(in which it is acknowledged that everything has a cost)

Page 62: Message Driven Architecture enables Elasticity, Resilience and Availability

Cons

• Complexity moved, not entirely solved.

• (although, functional discipline aids in reducing state complexity)

Page 63: Message Driven Architecture enables Elasticity, Resilience and Availability

• Difficulty grokking message flow (DOCS!!!)

Cons

Page 64: Message Driven Architecture enables Elasticity, Resilience and Availability

• Tracing errors can be difficult.

Cons

Page 65: Message Driven Architecture enables Elasticity, Resilience and Availability

• Rate-limiting with high-availability can be complex.

ConsDDOS YOUR DATABASE

WITH THIS ONEWEIRD TRICK

Page 66: Message Driven Architecture enables Elasticity, Resilience and Availability
Page 67: Message Driven Architecture enables Elasticity, Resilience and Availability

Thanks! ( ⋂‿⋂’)

@timcharperhttp://tim.theenchanter.com/