Coding Resiliently with Akka
-
Upload
derek-wyatt -
Category
Technology
-
view
1.840 -
download
3
description
Transcript of Coding Resiliently with Akka
ResilientlyCoding
Scalain AkkawithDerek Wyatt
Twitter: @derekwyattEmail: [email protected]
Tuesday, 4 February, 14
What’s Resiliency?
Tuesday, 4 February, 14
What’s Resiliency?Tolerance against faultsW
Tuesday, 4 February, 14
What’s Resiliency?Tolerance against faultsW
Grace in the face of insane success W
Tuesday, 4 February, 14
What’s Resiliency?Tolerance against faultsW
Grace in the face of insane success W
Slowing down instead of shutting downW
Tuesday, 4 February, 14
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
CCCloud Resiliency
Tuesday, 4 February, 14
CCCloud ResiliencyCQueues
CClustered DatabasesCVirtual Machines
CZookeeperCLoad Balancers
Tuesday, 4 February, 14
CCCloud ResiliencyCQueues
CClustered DatabasesCVirtual Machines
CZookeeperCLoad Balancers
Great tools. I’m going to
assume you use them.
Tuesday, 4 February, 14
How Akka Helps
Tuesday, 4 February, 14
How Akka HelpsAkka is a toolki for asynchrony5Resilient systems are asynchronous systems
Tuesday, 4 February, 14
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
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
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
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
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
Why We Don’t Code Resiliently
Tuesday, 4 February, 14
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
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
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
Callback Hell
Tuesday, 4 February, 14
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
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
Functional Asynchrony
Tuesday, 4 February, 14
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
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
The Circuit Breaker
Tuesday, 4 February, 14
The Circuit BreakerFor the times when you gotta fail the whale ⦿
Tuesday, 4 February, 14
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
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
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
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
Scaling Up
Tuesday, 4 February, 14
Scaling Up⦿ It’s all about dispatchers and untyped endpoints
Tuesday, 4 February, 14
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
Scaling Out
Tuesday, 4 February, 14
Scaling Out☉Routers are another way for messages to reach Actors
Tuesday, 4 February, 14
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
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
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
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
Clustering
Tuesday, 4 February, 14
Clustering
⦿Akka can spawn and communicate with Actors on remote nodes
Tuesday, 4 February, 14
Clustering
⦿Akka can spawn and communicate with Actors on remote nodes Boring!
Tuesday, 4 February, 14
Clustering
⦿Akka can spawn and communicate with Actors on remote nodes Boring! (OK, that’s really cool, but...)
Tuesday, 4 February, 14
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
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
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
Persistence - Event Sourcing
Tuesday, 4 February, 14
Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages
Tuesday, 4 February, 14
Persistence - Event Sourcing⨂ Akka provides the ability to persist incoming messages⨂ This helps for fault tolerance... DUH
Tuesday, 4 February, 14
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
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
Being Resilient
Tuesday, 4 February, 14
Being Resilient☉Akka provides the foundation for resilien programming and design
Queues, Dispatchers, Async, Messages, realistic guarantees⨳
Tuesday, 4 February, 14
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
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
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
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