akka in 100 slides or less
TRANSCRIPT
![Page 2: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/2.jpg)
Akka is Concurrency
![Page 3: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/3.jpg)
Akka is Concurrency
Actors: Living objects with concurrent context
“I am for you, Alrik of Valt.”
![Page 4: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/4.jpg)
Akka is Concurrency
Actors: Living objects with concurrent context
Futures: These aren’t Java’s Futures
val futureData = for { response <-‐ httpGet(…) query = response.body.as[Query] (img, text) <-‐ imageQuery(query) scaledImg <-‐ scaleImg(img) } yield NewPostData(text, scaledImg)
“I am for you, Alrik of Valt.”
![Page 5: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/5.jpg)
Akka is Concurrency
Actors: Living objects with concurrent context
Futures: These aren’t Java’s Futures
val futureData = for { response <-‐ httpGet(…) query = response.body.as[Query] (img, text) <-‐ imageQuery(query) scaledImg <-‐ scaleImg(img) } yield NewPostData(text, scaledImg)
Asynchronous
Asynchronous
Asynchronous
Asynchronous
“I am for you, Alrik of Valt.”
![Page 6: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/6.jpg)
Akka is Concurrency
Actors: Living objects with concurrent context
Futures: These aren’t Java’s Futures
val futureData = for { response <-‐ httpGet(…) query = response.body.as[Query] (img, text) <-‐ imageQuery(query) scaledImg <-‐ scaleImg(img) } yield NewPostData(text, scaledImg)
Asynchronous
Asynchronous
Asynchronous
Asynchronous
Streams: Async Non-Blocking and Back-PressuredSource
Sink
Demand
Supply
Http too…
“I am for you, Alrik of Valt.”
![Page 7: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/7.jpg)
Akka is Concurrency
Actors: Living objects with concurrent context
Futures: These aren’t Java’s Futures
val futureData = for { response <-‐ httpGet(…) query = response.body.as[Query] (img, text) <-‐ imageQuery(query) scaledImg <-‐ scaleImg(img) } yield NewPostData(text, scaledImg)
Asynchronous
Asynchronous
Asynchronous
Asynchronous
Streams: Async Non-Blocking and Back-PressuredSource
Sink
Demand
Supply
Http too…
“I am for you, Alrik of Valt.”
Support for Scala and Java (but use Scala, cuz…)
![Page 8: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/8.jpg)
Actors: Concurrency in Isolation
![Page 9: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/9.jpg)
Actors: Concurrency in Isolation
count: _alert: _score: _
![Page 10: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/10.jpg)
Actors: Concurrency in Isolationcount: 2alert: Yscore: 16
count: 4alert: Nscore: 2
count: 33alert: Gscore: 71
count: 13alert: Gscore: 12
count: 7alert: Xscore: 5
count: 8alert: Nscore: 11
count: 2alert: Iscore: 14
count: 87alert: Gscore: 0
count: 1alert: Bscore: 6
count: 7alert: Wscore: 32
count: 7alert: Oscore: 19
count: 4alert: Gscore: 99
count: _alert: _score: _
✗Construct as many as you’d like
![Page 11: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/11.jpg)
Actors: Concurrency in Isolationcount: 2alert: Yscore: 16
count: 4alert: Nscore: 2
count: 33alert: Gscore: 71
count: 13alert: Gscore: 12
count: 7alert: Xscore: 5
count: 8alert: Nscore: 11
count: 2alert: Iscore: 14
count: 87alert: Gscore: 0
count: 1alert: Bscore: 6
count: 7alert: Wscore: 32
count: 7alert: Oscore: 19
count: 4alert: Gscore: 99
count: _alert: _score: _
✗Construct as many as you’d like
✗Each one encapsulates its own state
![Page 12: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/12.jpg)
Actors: Concurrency in Isolationcount: 2alert: Yscore: 16
count: 4alert: Nscore: 2
count: 33alert: Gscore: 71
count: 13alert: Gscore: 12
count: 7alert: Xscore: 5
count: 8alert: Nscore: 11
count: 2alert: Iscore: 14
count: 87alert: Gscore: 0
count: 1alert: Bscore: 6
count: 7alert: Wscore: 32
count: 7alert: Oscore: 19
count: 4alert: Gscore: 99
Thre
ad
Thre
ad
count: _alert: _score: _
✗Construct as many as you’d like
✗Each one encapsulates its own state
✗They all (can) share the same thread pool
![Page 13: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/13.jpg)
Actors: Concurrency in Isolationcount: 2alert: Yscore: 16
count: 4alert: Nscore: 2
count: 33alert: Gscore: 71
count: 13alert: Gscore: 12
count: 7alert: Xscore: 5
count: 8alert: Nscore: 11
count: 2alert: Iscore: 14
count: 87alert: Gscore: 0
count: 1alert: Bscore: 6
count: 7alert: Wscore: 32
count: 7alert: Oscore: 19
count: 4alert: Gscore: 99
Thre
ad
Thre
ad
count: _alert: _score: _
✗Construct as many as you’d like
✗Each one encapsulates its own state
✗They all (can) share the same thread pool
✗They cannot interfere with each other
![Page 14: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/14.jpg)
Actors: Concurrency in Isolationcount: 2alert: Yscore: 16
count: 4alert: Nscore: 2
count: 33alert: Gscore: 71
count: 8alert: Nscore: 11
count: 2alert: Iscore: 14
count: 87alert: Gscore: 0
count: 1alert: Bscore: 6
count: 7alert: Wscore: 32
count: 7alert: Oscore: 19
count: 4alert: Gscore: 99
Thre
ad
Thre
ad
count: _alert: _score: _
✗Construct as many as you’d like
✗Each one encapsulates its own state
✗They all (can) share the same thread pool
✗They cannot interfere with each other
✗Their life-cycles are entirely under your control
![Page 15: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/15.jpg)
It’s all about the messages!
![Page 16: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/16.jpg)
It’s all about the messages!
Actors have no public methods⦿ A
![Page 17: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/17.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
A B
![Page 18: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/18.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
You cannot communicate with Actors Synchronously⦿
A B
![Page 19: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/19.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
You cannot communicate with Actors Synchronously⦿
The only way to talk to them is with Messages⦿
A Bhey
![Page 20: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/20.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
You cannot communicate with Actors Synchronously⦿
The only way to talk to them is with Messages⦿
The only way to access their data is with Messages⦿
A B83
![Page 21: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/21.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
You cannot communicate with Actors Synchronously⦿
The only way to talk to them is with Messages⦿
The only way to access their data is with Messages⦿
Messages can (and should) carry conversational state⦿
A B
A
![Page 22: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/22.jpg)
It’s all about the messages!
Actors have no public methods⦿
Actors have no publicly accessible data⦿
You cannot communicate with Actors Synchronously⦿
The only way to talk to them is with Messages⦿
The only way to access their data is with Messages⦿
Messages can (and should) carry conversational state⦿
A B
A
Done(a,b,c)
![Page 23: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/23.jpg)
Actors are Fault Tolerant
Parent
Child Child Child
Mailbox
Mailbox
Mailbox Mailbox
![Page 24: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/24.jpg)
Actors are Fault Tolerant
Actors supervise their childrenParent
Child Child Child
Mailbox
Mailbox
Mailbox Mailbox
![Page 25: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/25.jpg)
Actors are Fault Tolerant
Actors supervise their children
Failed Actors are restarted (or stopped, or resumed, or escalated) by their supervisors
Parent
Child Child Child
Mailbox
Mailbox
Mailbox Mailbox
![Page 26: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/26.jpg)
Actors are Fault Tolerant
Actors supervise their children
Failed Actors are restarted (or stopped, or resumed, or escalated) by their supervisors
Restarted Actors are given a fresh state
Parent
Child Child Child
Mailbox
Mailbox
Mailbox Mailbox
![Page 27: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/27.jpg)
Actors are Fault Tolerant
Actors supervise their children
Failed Actors are restarted (or stopped, or resumed, or escalated) by their supervisors
Restarted Actors are given a fresh state
The message they were processing is lost
Parent
Child Child Child
Mailbox
Mailbox
Mailbox Mailbox
![Page 28: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/28.jpg)
Actors are Fault Tolerant
Actors supervise their children
Failed Actors are restarted (or stopped, or resumed, or escalated) by their supervisors
Restarted Actors are given a fresh state
The message they were processing is lost
Parent
Child Child
Mailbox
Mailbox
Mailbox Mailbox
Child
![Page 29: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/29.jpg)
Death is ActionableParent
Child ChildChild
Deathwatch
Mailbox
![Page 30: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/30.jpg)
Death is Actionable❉ Restarting is invisible to outsiders…
❉ …But Actor Death is visible
❉ Deathwatch lets Actors react to death, such as to recreate a child
Parent
Child ChildChild
Deathwatch
Mailbox
![Page 31: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/31.jpg)
Death is Actionable❉ Restarting is invisible to outsiders…
❉ …But Actor Death is visible
❉ Deathwatch lets Actors react to death, such as to recreate a child
Parent
ChildChildChild
Deathwatch
Mailbox
![Page 32: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/32.jpg)
Death is Actionable❉ Restarting is invisible to outsiders…
❉ …But Actor Death is visible
❉ Deathwatch lets Actors react to death, such as to recreate a child
Parent
ChildChildChild
Deathwatch
Mailbox
❉… Or a transaction is complete, or it’s time to shut down, or a current stage of processing is finished, or…
![Page 33: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/33.jpg)
Death is Actionable❉ Restarting is invisible to outsiders…
❉ …But Actor Death is visible
❉ Deathwatch lets Actors react to death, such as to recreate a child
Parent
ChildChildChild
Deathwatch
Mailbox
❉ Remember that this is death, so the mailbox contents are lost
❉… Or a transaction is complete, or it’s time to shut down, or a current stage of processing is finished, or…
![Page 34: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/34.jpg)
So, why would I use Actors?
![Page 35: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/35.jpg)
So, why would I use Actors?✗Actors help you manage concurrency Reasoning
![Page 36: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/36.jpg)
So, why would I use Actors?✗Actors help you manage concurrency
✗Actors let you implement services within your system
Reasoning
Service decoupling
![Page 37: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/37.jpg)
So, why would I use Actors?✗Actors help you manage concurrency
✗Actors let you implement services within your system
✗Actors let you design an entirely asynchronous system
Reasoning
Service decoupling
Capacity and Throughput
![Page 38: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/38.jpg)
So, why would I use Actors?✗Actors help you manage concurrency
✗Actors let you implement services within your system
✗Actors let you design an entirely asynchronous system
✗Actors let you define the resiliency of your applications
Reasoning
Service decoupling
Capacity and Throughput
Fault Tolerance
![Page 39: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/39.jpg)
So, why would I use Actors?✗Actors help you manage concurrency
✗Actors let you implement services within your system
✗Actors let you design an entirely asynchronous system
✗Actors let you define the resiliency of your applications
✗Eventual consistency, and message delivery failure are realities that Actors help you deal with throughout your code
Reasoning
Service decoupling
Capacity and Throughput
Fault Tolerance
Massive Scale
Self Healing
![Page 40: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/40.jpg)
So, why would I use Actors?✗Actors help you manage concurrency
✗Actors let you implement services within your system
✗Actors let you design an entirely asynchronous system
✗Actors let you define the resiliency of your applications
✗Eventual consistency, and message delivery failure are realities that Actors help you deal with throughout your code
✗Most importantly, Actors help you think about the problem differently and express your solutions more creatively
Reasoning
Service decoupling
Capacity and Throughput
Fault Tolerance
Massive Scale
Self Healing
Sanity, Clarity, and Reasonability!!
![Page 41: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/41.jpg)
“Functional” Futures
![Page 42: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/42.jpg)
“Functional” Futures◎ A Future is just a value, but not yet…
![Page 43: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/43.jpg)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
![Page 44: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/44.jpg)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for {
![Page 45: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/45.jpg)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for { a <- futureA()
futureA()
![Page 46: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/46.jpg)
futureB(a)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for { a <- futureA() b <- futureB(a)
futureA()
![Page 47: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/47.jpg)
futureC(a,b) futureB(a)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for { a <- futureA() b <- futureB(a) c <- futureC(a,b)
futureA()
![Page 48: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/48.jpg)
futureC(a,b) futureB(a)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for { a <- futureA() b <- futureB(a) c <- futureC(a,b)} yield c
futureA()One
compo
sed
Future
![Page 49: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/49.jpg)
futureC(a,b) futureB(a)
“Functional” Futures◎ A Future is just a value, but not yet…
◎java.util.concurrent.Future is a bad Future. Akka gave us Futures that compose!
◎Composable Futures let us abstract over Future values, rather than wait for them
for { a <- futureA() b <- futureB(a) c <- futureC(a,b)} yield c
futureA()
◎Futures compose as Monads compose, which makes them “standard” functional abstractions, and that’s a powerful thing
One c
ompose
d
Future
![Page 50: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/50.jpg)
Abolish Callback Hell
![Page 51: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/51.jpg)
Abolish Callback Hellpublic void restHandler(RESTRequest req) { idservice.validate(req.userInfo, new Callback<ValidateResult>() { public void run(ValidateResult result) { if (result.isValid) { db.getProfile(req.userId, new Callback<UserProfile>() { public void run(UserProfile profile) { picServer.get(profile.pic1, new Callback<Pic>() { public void run(Pic p1) { picServer.get(profile.pic2, new Callback<Pic>() { public void run(Pic p2) { picServer.get(profile.pic3, new Callback<Pic>() { public void run(Pic p3) { picServer.get(profile.pic4, new Callback<Pic>() { public void run(Pic p4) {
picServer.get(profile.pic5, new Callback<Pic>() { public void run(Pic p5) {
twitterServer.getRecentActivity(req.userInfo, new Callback<TwitterActivity>() { public void run(TwitterActivity activity) {
req.sendResponse(pic1, pic2, pic3, pic4, pic5, activity)}
}}
}}
}}
}
![Page 52: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/52.jpg)
Abolish Callback Hellpublic void restHandler(RESTRequest req) { idservice.validate(req.userInfo, new Callback<ValidateResult>() { public void run(ValidateResult result) { if (result.isValid) { db.getProfile(req.userId, new Callback<UserProfile>() { public void run(UserProfile profile) { picServer.get(profile.pic1, new Callback<Pic>() { public void run(Pic p1) { picServer.get(profile.pic2, new Callback<Pic>() { public void run(Pic p2) { picServer.get(profile.pic3, new Callback<Pic>() { public void run(Pic p3) { picServer.get(profile.pic4, new Callback<Pic>() { public void run(Pic p4) {
picServer.get(profile.pic5, new Callback<Pic>() { public void run(Pic p5) {
twitterServer.getRecentActivity(req.userInfo, new Callback<TwitterActivity>() { public void run(TwitterActivity activity) {
req.sendResponse(pic1, pic2, pic3, pic4, pic5, activity)}
}}
}}
}}
}
We didn’t handle errors
We didn’t handle timeouts
It’s not necessarily Threadsafe
It’s incredibly hard to read
![Page 53: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/53.jpg)
Compose your Futuresimplicit 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: Throwable => FailedResponse(e) } }
![Page 54: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/54.jpg)
Compose your Futuresimplicit 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: Throwable => FailedResponse(e) } }
Errors have been handled
Timeouts have been handled
It’s (probably) Threadsafe
I didn’t have to shrink the font
![Page 55: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/55.jpg)
So why would I use Futures?
![Page 56: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/56.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
![Page 57: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/57.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
Programming synchronously isn’t a reasonable response⦿Thread starvation
timeoutscapacity is
suesthread thrashing
![Page 58: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/58.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
Programming synchronously isn’t a reasonable response⦿
Scala Futures embody asynchronous values⦿
Thread starvation
timeoutscapacity is
suesthread thrashing
Everything is a value
![Page 59: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/59.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
Programming synchronously isn’t a reasonable response⦿
Scala Futures embody asynchronous values⦿
They let us express composed asynchronous computation⦿
Thread starvation
timeoutscapacity is
suesthread thrashing
Everything is a value
Less Side Effects
![Page 60: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/60.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
Programming synchronously isn’t a reasonable response⦿
Scala Futures embody asynchronous values⦿
They let us express composed asynchronous computation⦿
Futures let us propagate errors and recover from them⦿
Thread starvation
timeoutscapacity is
suesthread thrashing
Everything is a value
Less Side Effects
Graceful Failure
Resiliency
![Page 61: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/61.jpg)
So why would I use Futures?
Asynchronous programming is still hard⦿
Programming synchronously isn’t a reasonable response⦿
Scala Futures embody asynchronous values⦿
They let us express composed asynchronous computation⦿
Futures let us propagate errors and recover from them⦿
Asynchronous programming has always been possible but Futures now make it much more viable⦿
Thread starvation
timeoutscapacity is
suesthread thrashing
Everything is a value
Less Side Effects
Graceful Failure
Resiliency
![Page 62: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/62.jpg)
Operating on Data Streams
![Page 63: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/63.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms
![Page 64: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/64.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms❉ Polling sucks. We live in a real time world.
![Page 65: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/65.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms❉ Polling sucks. We live in a real time world.
❉ Eventing can be hard, since it doesn’t compose
![Page 66: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/66.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms❉ Polling sucks. We live in a real time world.
❉ Eventing can be hard, since it doesn’t compose
❉ Throttling is a real problem we never address
![Page 67: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/67.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms❉ Polling sucks. We live in a real time world.
❉ Eventing can be hard, since it doesn’t compose
❉ Throttling is a real problem we never address
❉ Akka Streams help us solve all of these problems
![Page 68: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/68.jpg)
Operating on Data Streams❉ In the real world, we communicate with things
NetworksDisks
Databases Services Slower Algorithms❉ Polling sucks. We live in a real time world.
❉ Eventing can be hard, since it doesn’t compose
❉ Throttling is a real problem we never address
❉ Akka Streams help us solve all of these problems
Source
Transformation Flow
Transformation Flow
Sink
Demand
Supply
![Page 69: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/69.jpg)
BackPressure is keySource
Sink
![Page 70: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/70.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
Source
Sink
![Page 71: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/71.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
◎ Producers can’t be artificially slow just to be nice
Source
Sink
![Page 72: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/72.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
◎ Producers can’t be artificially slow just to be nice
◎ Consumers signal demand, Producers signal supply
Source
Sink
![Page 73: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/73.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
◎ Producers can’t be artificially slow just to be nice
◎ Consumers signal demand, Producers signal supply
◎ When a consumer is slow, it signals no demand
Source
Sink
![Page 74: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/74.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
◎ Producers can’t be artificially slow just to be nice
◎ Consumers signal demand, Producers signal supply
◎ When a consumer is slow, it signals no demand
◎ When a producer is slow, it doesn’t supply
Source
Sink
![Page 75: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/75.jpg)
BackPressure is key◎ Fast producers can kill; we only have so much RAM
◎ Producers can’t be artificially slow just to be nice
◎ Consumers signal demand, Producers signal supply
◎ When a consumer is slow, it signals no demand
◎ When a producer is slow, it doesn’t supply
Source
Sink Supply
![Page 76: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/76.jpg)
Composable Binary Protocols
![Page 77: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/77.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
![Page 78: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/78.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
ApplicationObject Object
Source Sink
NetworkByteString ByteString
Source SinkBidiFlow
ByteStringObject
![Page 79: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/79.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
ApplicationObject Object
Source Sink
NetworkByteString ByteString
Source SinkBidiFlow
ByteStringObject
✗Entirely reusable components that fit in any BiDirectional Flow
![Page 80: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/80.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
ApplicationObject Object
Source Sink
NetworkByteString ByteString
Source SinkBidiFlow
ByteStringObject
✗Entirely reusable components that fit in any BiDirectional Flow
✗Type safe (or as type safe as byte string marshaling gets)
![Page 81: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/81.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
ApplicationObject Object
Source Sink
NetworkByteString ByteString
Source SinkBidiFlow
ByteStringObject
✗Entirely reusable components that fit in any BiDirectional Flow
✗Type safe (or as type safe as byte string marshaling gets)
✗Entirely back pressured from end to end
![Page 82: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/82.jpg)
Composable Binary Protocols
FramerByteString ByteString
BidiFlow
SerializerObject ByteString
BidiFlow
ChunkerByteString ByteString
BidiFlow
ApplicationObject Object
Source Sink
NetworkByteString ByteString
Source SinkBidiFlow
ByteStringObject
✗Entirely reusable components that fit in any BiDirectional Flow
✗Type safe (or as type safe as byte string marshaling gets)
✗Entirely back pressured from end to end
✗ 100% event driven and reactive Realtime Speeds
No Blocking Threads
Natural Throttling
graceful performance degradationNot a Crash
![Page 83: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/83.jpg)
Why Would I use Streams?
![Page 84: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/84.jpg)
Why Would I use Streams?
Streaming is (probably) the crown jewel of Akka’s offering (but it’s a tough call)
![Page 85: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/85.jpg)
Why Would I use Streams?
Streaming is (probably) the crown jewel of Akka’s offering
The need to respond to backpressure is real and we never do it !(but it’s a tough call)
![Page 86: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/86.jpg)
Why Would I use Streams?
Streaming is (probably) the crown jewel of Akka’s offering
The need to respond to backpressure is real and we never do it
The need to treat our threads with respect is important
!!
(but it’s a tough call)
![Page 87: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/87.jpg)
Why Would I use Streams?
Streaming is (probably) the crown jewel of Akka’s offering
The need to respond to backpressure is real and we never do it
The need to treat our threads with respect is important
The need to be reactive and fast is vital to the user experience
!!!
(but it’s a tough call)
![Page 88: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/88.jpg)
Why Would I use Streams?
Streaming is (probably) the crown jewel of Akka’s offering
The need to respond to backpressure is real and we never do it
The need to treat our threads with respect is important
The need to be reactive and fast is vital to the user experience
Streams give us all of this and let us use our hardware to its fullest
!!!
! ! !
(but it’s a tough call)
![Page 89: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/89.jpg)
Akka from 20k feetAkka from 20k feet
![Page 90: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/90.jpg)
Akka from 20k feetAkka from 20k feet
Actors!Actors!Live Objects
Supervised
AsynchronousMessage Passing
ServicesState Machines
Network Ready
![Page 91: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/91.jpg)
Akka from 20k feetAkka from 20k feet
Actors!Actors!
Futures!Futures!
Live Objects
Supervised
AsynchronousMessage Passing
ServicesState Machines
Network Ready
Composable Values
ReactiveMonadic
Immutable
Threadsafe
Work Well with Actors
![Page 92: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/92.jpg)
Akka from 20k feetAkka from 20k feet
Actors!Actors!
Futures!Futures!
Streams!Streams!
Live Objects
Supervised
AsynchronousMessage Passing
ServicesState Machines
Network Ready
Composable Values
ReactiveMonadic
Immutable
Threadsafe
Work Well with Actors
BackPressured
Composable
Reusable
Event DrivenFlexible
Lots of Activity and Support
Work with Futures and Actors
![Page 93: Akka in 100 slides or less](https://reader034.vdocuments.mx/reader034/viewer/2022051404/5871afec1a28abda6a8b6613/html5/thumbnails/93.jpg)
Go Get AKKA!Go Get AKKA!Website: http://akka.io
Scala Reference: http://doc.akka.io/docs/akka/current/scala.html
Java Reference: http://doc.akka.io/docs/akka/current/java.html
Streams Reference: http://doc.akka.io/docs/akka-stream-and-http-experimental/current/scala.html
Derek Wyatt Twitter: @derekwyatt
Email: [email protected] 2015