Coding Resiliently with Akka

59
Resiliently Coding Scala in Akka with Derek Wyatt Twitter: @derekwyatt Email: [email protected] Tuesday, 4 February, 14

description

This is a 300,000 ft view of coding resilient with Akka and Scala. It doesn't go into a lot of depth, and was really meant to be "presented", but hey... if it helps you out, awesome :)

Transcript of Coding Resiliently with Akka

Page 1: Coding Resiliently with Akka

ResilientlyCoding

Scalain AkkawithDerek Wyatt

Twitter: @derekwyattEmail: [email protected]

Tuesday, 4 February, 14

Page 2: Coding Resiliently with Akka

What’s Resiliency?

Tuesday, 4 February, 14

Page 3: Coding Resiliently with Akka

What’s Resiliency?Tolerance against faultsW

Tuesday, 4 February, 14

Page 4: Coding Resiliently with Akka

What’s Resiliency?Tolerance against faultsW

Grace in the face of insane success W

Tuesday, 4 February, 14

Page 5: Coding Resiliently with Akka

What’s Resiliency?Tolerance against faultsW

Grace in the face of insane success W

Slowing down instead of shutting downW

Tuesday, 4 February, 14

Page 6: Coding Resiliently with Akka

What’s Resiliency?Tolerance against faultsW

Grace in the face of insane success W

Slowing down instead of shutting downW

to the crap that happensResiliency is about reactingbecause life is, basically a problem

Tuesday, 4 February, 14

Page 7: Coding Resiliently with Akka

CCCloud Resiliency

Tuesday, 4 February, 14

Page 8: Coding Resiliently with Akka

CCCloud ResiliencyCQueues

CClustered DatabasesCVirtual Machines

CZookeeperCLoad Balancers

Tuesday, 4 February, 14

Page 9: Coding Resiliently with Akka

CCCloud ResiliencyCQueues

CClustered DatabasesCVirtual Machines

CZookeeperCLoad Balancers

Great tools. I’m going to

assume you use them.

Tuesday, 4 February, 14

Page 10: Coding Resiliently with Akka

How Akka Helps

Tuesday, 4 February, 14

Page 11: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Tuesday, 4 February, 14

Page 12: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Akka is buil using queues5Queues add resilient points of communication

Tuesday, 4 February, 14

Page 13: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Akka is buil using queues5Queues add resilient points of communication

Akka divorces threads from work5Improper use of threads kills resiliency

Tuesday, 4 February, 14

Page 14: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Akka is buil using queues5Queues add resilient points of communication

Akka divorces threads from work5Improper use of threads kills resiliency

Akka’s Actors incorporate faul tolerance5A crash is just standard operating procedure

Tuesday, 4 February, 14

Page 15: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Akka is buil using queues5Queues add resilient points of communication

Akka divorces threads from work5Improper use of threads kills resiliency

Akka’s Actors incorporate faul tolerance5A crash is just standard operating procedure

Akka Clusters5Clustering? Come on... Resilient

Tuesday, 4 February, 14

Page 16: Coding Resiliently with Akka

How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems

Akka is buil using queues5Queues add resilient points of communication

Akka divorces threads from work5Improper use of threads kills resiliency

Akka’s Actors incorporate faul tolerance5A crash is just standard operating procedure

Akka Clusters5Clustering? Come on... Resilient

And there’s more!

Tuesday, 4 February, 14

Page 17: Coding Resiliently with Akka

Why We Don’t Code Resiliently

Tuesday, 4 February, 14

Page 18: Coding Resiliently with Akka

Why We Don’t Code Resilientlya.k.a Callback Hell1) REST request comes in2) Validate user identity3) Get profile from DB4) Grab a few pics5) Get recent Twitter activity6) Return REST response

Tuesday, 4 February, 14

Page 19: Coding Resiliently with Akka

Why We Don’t Code Resilientlya.k.a Callback Hell1) REST request comes in2) Validate user identity3) Get profile from DB4) Grab a few pics5) Get recent Twitter activity6) Return REST response

Do it asynchronously

and don’t block

Tuesday, 4 February, 14

Page 20: Coding Resiliently with Akka

Why We Don’t Code Resilientlya.k.a Callback Hell1) REST request comes in2) Validate user identity3) Get profile from DB4) Grab a few pics5) Get recent Twitter activity6) Return REST response

Do it asynchronously

and don’t block

Welcome to Callback Hell

Tuesday, 4 February, 14

Page 21: Coding Resiliently with Akka

Callback Hell

Tuesday, 4 February, 14

Page 22: Coding Resiliently with Akka

Callback Hellpublic void restHandler(RESTRequest req) { idService.validate(req.userInfo, new Callback(ValidateResult result) { if (result.isValid) { db.getProfile(req.userId, new Callback(UserProfile profile) { picServer.get(profile.pic1, new Callback(Pic p1) { picServer.get(profile.pic2, new Callback(Pic p2) { picServer.get(profile.pic3, new Callback(Pic p3) { picServer.get(profile.pic4, new Callback(Pic p4) { picServer.get(profile.pic5, new Callback(Pic p5) { twitterServer.getRecentActivity(req.userInfo, new Callback(TwitterActivity activity) { req.sendResponse(pic1, pic2, pic3, pic4, pic5, activity) }) }) }) }) }) }) }) })}

Tuesday, 4 February, 14

Page 23: Coding Resiliently with Akka

Callback Hellpublic void restHandler(RESTRequest req) { idService.validate(req.userInfo, new Callback(ValidateResult result) { if (result.isValid) { db.getProfile(req.userId, new Callback(UserProfile profile) { picServer.get(profile.pic1, new Callback(Pic p1) { picServer.get(profile.pic2, new Callback(Pic p2) { picServer.get(profile.pic3, new Callback(Pic p3) { picServer.get(profile.pic4, new Callback(Pic p4) { picServer.get(profile.pic5, new Callback(Pic p5) { twitterServer.getRecentActivity(req.userInfo, new Callback(TwitterActivity activity) { req.sendResponse(pic1, pic2, pic3, pic4, pic5, activity) }) }) }) }) }) }) }) })}

We didn’t handle timeouts

We didn’t handle errors

We actually didn’t make it thread safe

We did throw up a little though...

☉☉☉☉

Tuesday, 4 February, 14

Page 24: Coding Resiliently with Akka

Functional Asynchrony

Tuesday, 4 February, 14

Page 25: Coding Resiliently with Akka

Functional Asynchronyimplicit val timeout = Timeout(30.seconds)def restHandler(req: RESTRequest): Future[RESTResponse] = { val resp = for { validity <- idService.validate(req.userInfo) if validity.isValid profile <- db.getProfile(req.userId) pic1 <- picServer.get(profile.pic1) pic2 <- picServer.get(profile.pic2) pic3 <- picServer.get(profile.pic3) pic4 <- picServer.get(profile.pic4) pic5 <- picServer.get(profile.pic5) activity <- twitterServer.getRecentActivity(req.userInfo) } yield SuccessfulResponse(pic1, pic2, pic3, pic4, pic5, activity) resp.recover { e => FailedResponse(e) }}

Tuesday, 4 February, 14

Page 26: Coding Resiliently with Akka

Functional Asynchronyimplicit val timeout = Timeout(30.seconds)def restHandler(req: RESTRequest): Future[RESTResponse] = { val resp = for { validity <- idService.validate(req.userInfo) if validity.isValid profile <- db.getProfile(req.userId) pic1 <- picServer.get(profile.pic1) pic2 <- picServer.get(profile.pic2) pic3 <- picServer.get(profile.pic3) pic4 <- picServer.get(profile.pic4) pic5 <- picServer.get(profile.pic5) activity <- twitterServer.getRecentActivity(req.userInfo) } yield SuccessfulResponse(pic1, pic2, pic3, pic4, pic5, activity) resp.recover { e => FailedResponse(e) }}

We handle timeouts (!!)

We handle errors (!!)

It’s immutable and thread safe (!!)

We got to keep our lunch down

☉☉☉☉

Tuesday, 4 February, 14

Page 27: Coding Resiliently with Akka

The Circuit Breaker

Tuesday, 4 February, 14

Page 28: Coding Resiliently with Akka

The Circuit BreakerFor the times when you gotta fail the whale ⦿

Tuesday, 4 February, 14

Page 29: Coding Resiliently with Akka

The Circuit BreakerFor the times when you gotta fail the whale ⦿

Closed

Open

HalfOpen

Success

Trip Breaker

Calls Failing Fast

Attempt Reset

Trip Breaker

Reset Breaker

Tuesday, 4 February, 14

Page 30: Coding Resiliently with Akka

The Circuit BreakerFor the times when you gotta fail the whale ⦿

Closed

Open

HalfOpen

Success

Trip Breaker

Calls Failing Fast

Attempt Reset

Trip Breaker

Reset BreakerDB

Tuesday, 4 February, 14

Page 31: Coding Resiliently with Akka

The Circuit BreakerFor the times when you gotta fail the whale ⦿

Closed

Open

HalfOpen

Success

Trip Breaker

Calls Failing Fast

Attempt Reset

Trip Breaker

Reset BreakerDB

Circuit Closed

Circuit Open

Tuesday, 4 February, 14

Page 32: Coding Resiliently with Akka

Load BalancingActors are untyped endpointsYou can easily swap one for anotherActors have dispatchersHow messages are dispatched is configurableYou can only communicate through messagesYou can route them however you like

Messages can carry the state dataActors then become completely stateless and Resilient

Tuesday, 4 February, 14

Page 33: Coding Resiliently with Akka

Scaling Up

Tuesday, 4 February, 14

Page 34: Coding Resiliently with Akka

Scaling Up⦿ It’s all about dispatchers and untyped endpoints

Tuesday, 4 February, 14

Page 35: Coding Resiliently with Akka

Scaling Up⦿ It’s all about dispatchers and untyped endpoints

••

Acto

rRef

Acto

rRef

Acto

rRef

BalancingDispatcher

Actor

Actor

Actor

Client

Normally the message from the client would go to the middle Actor, but the Balancing Dispatcher lets the last one “steal” it because it can handle it faster.

Tuesday, 4 February, 14

Page 36: Coding Resiliently with Akka

Scaling Out

Tuesday, 4 February, 14

Page 37: Coding Resiliently with Akka

Scaling Out☉Routers are another way for messages to reach Actors

Tuesday, 4 February, 14

Page 38: Coding Resiliently with Akka

Scaling Out☉Routers are another way for messages to reach Actors

☉A router igures ou which Actor to send to based on rules

Tuesday, 4 February, 14

Page 39: Coding Resiliently with Akka

Scaling Out☉Routers are another way for messages to reach Actors

☉A router igures ou which Actor to send to based on rules

DBActor

DBActor

Client Router DBActor

DB ClusterMachine 1

DB ClusterMachine 2

DB ClusterMachine 3

Tuesday, 4 February, 14

Page 40: Coding Resiliently with Akka

Scaling Out☉Routers are another way for messages to reach Actors

☉A router igures ou which Actor to send to based on rules

DBActor

DBActor

Client Router DBActor

DB ClusterMachine 1

DB ClusterMachine 2

DB ClusterMachine 3

This router can

route consistently

based on sender

Tuesday, 4 February, 14

Page 41: Coding Resiliently with Akka

Scaling Out☉Routers are another way for messages to reach Actors

☉A router igures ou which Actor to send to based on rules

DBActor

DBActor

Client Router DBActor

DB ClusterMachine 1

DB ClusterMachine 2

DB ClusterMachine 3

This router can

route consistently

based on sender

☉Routers can also resize the number o Actors dynamically

Tuesday, 4 February, 14

Page 42: Coding Resiliently with Akka

Clustering

Tuesday, 4 February, 14

Page 43: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes

Tuesday, 4 February, 14

Page 44: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes Boring!

Tuesday, 4 February, 14

Page 45: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes Boring! (OK, that’s really cool, but...)

Tuesday, 4 February, 14

Page 46: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes Boring! (OK, that’s really cool, but...)

⦿ You can cluster your Actors as well

Tuesday, 4 February, 14

Page 47: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes Boring! (OK, that’s really cool, but...)

⦿ You can cluster your Actors as well

⦿Akka takes care of letting you know when nodes come and go

Tuesday, 4 February, 14

Page 48: Coding Resiliently with Akka

Clustering

⦿Akka can spawn and communicate with Actors on remote nodes Boring! (OK, that’s really cool, but...)

⦿ You can cluster your Actors as well

⦿Akka takes care of letting you know when nodes come and go

⦿Clustering provides a ton of possibilities to design for resiliency

Tuesday, 4 February, 14

Page 49: Coding Resiliently with Akka

Persistence - Event Sourcing

Tuesday, 4 February, 14

Page 50: Coding Resiliently with Akka

Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages

Tuesday, 4 February, 14

Page 51: Coding Resiliently with Akka

Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages⨂ This helps for fault tolerance... DUH

Tuesday, 4 February, 14

Page 52: Coding Resiliently with Akka

Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages⨂ This helps for fault tolerance... DUH⨂ It also provides the ability to move stateful Actors

Tuesday, 4 February, 14

Page 53: Coding Resiliently with Akka

Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages⨂ This helps for fault tolerance... DUH⨂ It also provides the ability to move stateful Actors⨂ Or provide tools for “Crash-Only” software

ActorMessageCrash! ActorEvent 1

Event 2

Event 3

Good to go!

Tuesday, 4 February, 14

Page 54: Coding Resiliently with Akka

Being Resilient

Tuesday, 4 February, 14

Page 55: Coding Resiliently with Akka

Being Resilient☉Akka provides the foundation for resilien programming and design

Queues, Dispatchers, Async, Messages, realistic guarantees⨳

Tuesday, 4 February, 14

Page 56: Coding Resiliently with Akka

Being Resilient☉Akka provides the foundation for resilien programming and design

Queues, Dispatchers, Async, Messages, realistic guarantees⨳

☉Then you ge...

Routers⨳Clustering⨳

Remote Deployment⨳

Persistence⨳

Resiliency Patterns⨳

Message Oriented Flexibility⨳

Tuesday, 4 February, 14

Page 57: Coding Resiliently with Akka

Being Resilient☉Akka provides the foundation for resilien programming and design

Queues, Dispatchers, Async, Messages, realistic guarantees⨳

☉Then you ge...

Routers⨳Clustering⨳

Remote Deployment⨳

Persistence⨳

Resiliency Patterns⨳

Message Oriented Flexibility⨳

☉Existing cloud components are vial for resiliency

Tuesday, 4 February, 14

Page 58: Coding Resiliently with Akka

Being Resilient☉Akka provides the foundation for resilien programming and design

Queues, Dispatchers, Async, Messages, realistic guarantees⨳

☉Then you ge...

Routers⨳Clustering⨳

Remote Deployment⨳

Persistence⨳

Resiliency Patterns⨳

Message Oriented Flexibility⨳

☉Existing cloud components are vial for resiliency

☉Akka picks up where those components leave of

Tuesday, 4 February, 14

Page 59: Coding Resiliently with Akka

Go write some Code

http://akka.iohttp://github.com/akka/akka

http://scala-lang.orghttp://github.com/scala/scala

Get Scala

Get AkkaDerek Wyatt

Twitter: @derekwyattEmail: [email protected]

Tuesday, 4 February, 14