Async Microservices with Twitter's Finagle

Vladimir Kostyukov
Vladimir KostyukovSoftware Engineer at Intel
Async Microservices 
with Finagle 
Vladimir Kostyukov 
http://vkostyukov.ru
Asynchronous 
Protocol-Agnostic 
Full-Stack RPC 
2
Adopters 
3
Built atop of Netty 
(#1 Non-Blocking I/O for JVM) 
4
Finagle - 
Netty in a functional setting 
5
Building Blocks: 
Services + Futures + Filters 
6
trait Service[-Req, +Rep] extends (Req => Future[Rep]) 
7
Service-Oriented 
rather then 
Stream-Oriented 
8
// TODO: Don't forget to remove it in production. 
object GetDeveloperPromotionDate extends Service[Developer, Date] { 
def apply(req: Developer) = Future.never 
} 
val date: Future[Date] = GetDeveloperPromotionDate(Developer.Myself) 
9
1 val respond = new Service[HttpRequest, HttpResponse] { 
2 def apply(req: HttpRequest) = { 
3 val rep = new DefaultHttpResponse(HttpVersion.HTTP_1_1, 
4 HttpResponseStatus.OK) 
5 rep.setContentString(req.getUri()) 
6 Future.value(rep) 
7 } 
8 } 
9 
10 Await.ready(Http.serve(":8080", respond) 
10
Servers and Clients 
talk to each other via services 
11
1 val to = Http.newService("google.com") 
2 
3 val proxy = new Service[HttpRequest, HttpResponse] { 
4 def apply(req: HttpRequest) = to(req) 
5 } 
6 
7 Await.ready(Http.serve(":8080", proxy) 
12
Are there high-order services? 
13
14 
Client 
Service 
Server 
Filter Filter Filter Service
// ReqIn -> Service(ReqOut -> RepIn) -> RepOut 
trait Filter[-ReqIn, +RepOut, +ReqOut, -RepIn] 
extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut]) 
15
// A filter that does nothing. 
def identity[Req, Rep] = new Filter[Req, Rep, Req, Rep] { 
def apply(req: Req, service: Service[Req, Rep]) = service(req) 
} 
val auth = identity[HttpRequest, HttpResponse] 
16
Filters are type-safe! 
17
A Type-Safe Input Filter 
1 object TurnAIntoB extends Filter[A, Rep, B, Rep] { 
2 def apply(req: A, service: Service[B, Rep]): Future[Rep] = 
3 service(req.toB) 
4 } 
5 
6 val serviceOfB: Service[B, Rep] = ... 
7 val serviceOfA: Service[A, Rep] = TurnAIntoB andThen serviceOfA 
18
A Type-Safe Output Filter 
1 object TurnAIntoB extends Filter[Req, B, Req, A] { 
2 def apply(req: Req, service: Service[Req, A]): Future[B] = 
3 service(req) map { a => a.toB } 
4 } 
5 
6 val serviceOfA: Service[Req, A] = ... 
7 val serviceOfB: Service[Req, B] = TurnAIntoB andThen serviceOfA 
19
Case Study: TimeOut Filter 
1 def timeout[Req, Rep](timeout: Duration)(implicit timer: Timer) = 
2 new SimpleFilter[Req, Rep] { 
3 def apply(req: Req, service: Service[Req, Rep]) = 
4 service(req).within(timer, timeout) 
5 } 
6 
7 val handleExceptions = new SimpleFilter[Req, Rep] { 
8 def apply(req: Req, service: Service[Req, Rep]) = 
9 service(req) handle { 
10 case t: TimeOutException => Future.value(defaultRep) 
11 } 
12 } 
13 
14 val respond = new Service[Req, Rep] { ... } 
15 // We guarantee 3 seconds response time. 
16 val backend = handleExceptions andThen 
17 timeout(3.seconds) andThen 
18 respond 
20
Case Study: OAuth2Filter 
1 class OAuth2Filter[U](dataHandler: DataHandler[U]) 
2 extends Filter[HttpRequest, HttpResponse, OAuth2Request[U], HttpResponse] 
3 with OAuth2 with OAuthErrorHandler { 
4 
5 def handleError(e: OAuthError) = e.toHttpResponse 
6 def apply(req: HttpRequest, service: Service[OAuth2Request[U], HttpResponse]) = 
7 authorize(req, dataHandler) flatMap { authInfo => 
8 service(OAuth2Request(authInfo, req)) 
9 } handle { 
10 case e: OAuthError => handleError(e) 
11 } 
12 } 
13 
14 val authorize = new OAuth2Filter(...) 
15 val respond: Service[OAuth2Request[U], HttpResponse] = ??? 
16 val backend: Service[HttpRequest, HttpResponse] = authorize andThen respond 
21
Future is a monad! 
22
Abstraction: Fibonacci Calculator 
1 trait FibonacciCalculator { 
2 val Zero = BigInt(0) 
3 val One = BigInt(1) 
4 val Two = BigInt(2) 
5 
6 def calculate(n: BigInt): Future[BigInt] 
7 } 
23
Sequential Composition: map & flatMap 
1 trait Future[+A] { 
2 def flatMap[B](f: A => Future[B]): Future[B] 
3 def map[B](f: A => B): Future[B] 
4 } 
24
FlatMap Power 
1 val ab: Service[A, B] = ??? 
2 val bc: Service[B, C] = ??? 
3 val cd: Service[C, D] = ??? 
4 
5 val req: A = ??? 
6 val rep: Future[D] = ab(req) flatMap bc flatMap cd 
25
Sequential Composition: map & flatMap 
1 object FlatMapFibonacciCalculator extends FibonacciCalculator { 
2 def calculate(n: BigInt): Future[BigInt] = 
3 if (n == Zero || n == One) Future.value(n) 
4 else calculate(n - One) flatMap { a => 
5 calculate(n - Two) flatMap { b => Future.value(a + b) } 
6 } 
7 } 
8 
9 object MapFibonacciCalculator extends FibonacciCalculator { 
10 def calculate(n: BigInt): Future[BigInt] = 
11 if (n == Zero || n == One) Future.value(n) 
12 else calculate(n - One) map { a => 
13 calculate(n - Two) map { b => a + b } 
14 } 
15 } 
26
Sequential Composition: for-comprehension 
1 object ForFibonacciCalculator extends FibonacciCalculator { 
2 def calculate(n: BigInt): Future[BigInt] = 
3 if (n == Zero || n == One) Future.value(n) 
4 else for { 
5 a <- calculate(n - One) 
6 b <- calculate(n - Two) 
7 } yield a + b 
8 } 
27
Concurrent Composition: collect & select 
1 object Future { 
2 def collect[A](fs: Seq[Future[A]]): Future[Seq[A]] 
3 def select[A](fs: Seq[Future[A]]): Future[A] 
4 } 
28
Concurrent Composition: collect 
1 class FanoutFibonacciCalculator( 
2 left: FibonacciCalculator, 
3 right: FibonacciCalculator) extends FibonacciCalculator { 
4 
5 def calculate(n: BigInt): Future[BigInt] = 
6 if (n == Zero) || n == One) Future.value(n) 
7 else { 
8 val seq = Seq(left.calculate(n - One), right.calculate(n - Two)) 
9 Future.collect(seq) map { _.sum } 
10 } 
11 } 
29
Concurrent Composition: select 
1 class SelectFibonacciCalculator(seq: Seq[FibonacciCalculator]) 
2 extends FibonacciCalculator { 
3 
4 def calculate(n: BigInt): Future[BigInt] = { 
5 val futures: Seq[Future[BigInt]] = seq map { _.calculate(n) } 
6 Future.select(futures) 
7 } 
8 } 
30
Futures should be 
chained asynchronously 
31
Do Not Await 
// Asynchronous operation. 
val f = service(req) 
// Blocking operation. 
Await.result(f) 
32
Involving Side-Effects 
// Asynchronous operation. 
val f = service(req) 
! 
f onSuccess { rep => println("Got the value: " + rep) } 
onFailure { t => t.printStackTrace() } 
33
Finagle is a non-blocking glue for 
services that implement 
different protocols 
34
Finagle is extremely good at 
gluing things that work 
with different speed 
35
References 
§ http://twitter.github.io/finagle/ 
! 
§ http://vkostyukov.ru/posts/finagle-your-fibonacci-calculation/ 
! 
§ https://github.com/finagle/finch 
! 
§ https://github.com/finagle/finagle-oauth2 
36
Stay Finagled! 
! 
And drop your feedbacks to 
@vkostyukov 
37
1 of 37

Recommended

Finch + Finagle OAuth2 by
Finch + Finagle OAuth2Finch + Finagle OAuth2
Finch + Finagle OAuth2Vladimir Kostyukov
2.6K views18 slides
Finch.io - Purely Functional REST API with Finagle by
Finch.io - Purely Functional REST API with FinagleFinch.io - Purely Functional REST API with Finagle
Finch.io - Purely Functional REST API with FinagleVladimir Kostyukov
6.8K views17 slides
Rntb20200805 by
Rntb20200805Rntb20200805
Rntb20200805t k
167 views19 slides
Un dsl pour ma base de données by
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de donnéesRomain Lecomte
257 views44 slides
React native-firebase startup-mtup by
React native-firebase startup-mtupReact native-firebase startup-mtup
React native-firebase startup-mtupt k
915 views43 slides
Pratik Bakane C++ by
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++pratikbakane
1.2K views26 slides

More Related Content

What's hot

Pratik Bakane C++ by
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++pratikbakane
5.7K views42 slides
2016 gunma.web games-and-asm.js by
2016 gunma.web games-and-asm.js2016 gunma.web games-and-asm.js
2016 gunma.web games-and-asm.jsNoritada Shimizu
1.3K views58 slides
20151224-games by
20151224-games20151224-games
20151224-gamesNoritada Shimizu
1K views64 slides
Python meetup: coroutines, event loops, and non-blocking I/O by
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OBuzzcapture
2.7K views29 slides
Mca 2nd sem u-4 operator overloading by
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
703 views32 slides
Basic Programs of C++ by
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
1.2K views20 slides

What's hot(20)

Pratik Bakane C++ by pratikbakane
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
pratikbakane5.7K views
Python meetup: coroutines, event loops, and non-blocking I/O by Buzzcapture
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
Buzzcapture2.7K views
Mca 2nd sem u-4 operator overloading by Rai University
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
Rai University703 views
Basic Programs of C++ by Bharat Kalia
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
Bharat Kalia1.2K views
Cocoaheads Meetup / Alex Zimin / Swift magic by Badoo Development
Cocoaheads Meetup / Alex Zimin / Swift magicCocoaheads Meetup / Alex Zimin / Swift magic
Cocoaheads Meetup / Alex Zimin / Swift magic
Badoo Development6.7K views
Using Grafana with InfluxDB 2.0 and Flux Lang by Jacob Lisi by InfluxData
Using Grafana with InfluxDB 2.0 and Flux Lang by Jacob LisiUsing Grafana with InfluxDB 2.0 and Flux Lang by Jacob Lisi
Using Grafana with InfluxDB 2.0 and Flux Lang by Jacob Lisi
InfluxData3.2K views
Cocoaheads Meetup / Kateryna Trofimenko / Feature development by Badoo Development
Cocoaheads Meetup / Kateryna Trofimenko / Feature developmentCocoaheads Meetup / Kateryna Trofimenko / Feature development
Cocoaheads Meetup / Kateryna Trofimenko / Feature development
Badoo Development6.6K views
Compositional I/O Stream in Scala by C4Media
Compositional I/O Stream in ScalaCompositional I/O Stream in Scala
Compositional I/O Stream in Scala
C4Media1.8K views
Wprowadzenie do technologi Big Data i Apache Hadoop by Sages
Wprowadzenie do technologi Big Data i Apache HadoopWprowadzenie do technologi Big Data i Apache Hadoop
Wprowadzenie do technologi Big Data i Apache Hadoop
Sages1.7K views
The Ring programming language version 1.5.2 book - Part 7 of 181 by Mahmoud Samir Fayed
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.4 book - Part 40 of 185 by Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 40 of 185The Ring programming language version 1.5.4 book - Part 40 of 185
The Ring programming language version 1.5.4 book - Part 40 of 185

Viewers also liked

High Performance RPC with Finagle by
High Performance RPC with FinagleHigh Performance RPC with Finagle
High Performance RPC with FinagleSamir Bessalah
7K views42 slides
Exploring Twitter's Finagle technology stack for microservices by
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices💡 Tomasz Kogut
3.2K views63 slides
Finatra v2 by
Finatra v2Finatra v2
Finatra v2Steve Cosenza
4K views47 slides
Ember vs Backbone by
Ember vs BackboneEmber vs Backbone
Ember vs BackboneAbdriy Mosin
1.1K views46 slides
Service Objects Evolution by
Service Objects EvolutionService Objects Evolution
Service Objects EvolutionAbdriy Mosin
768 views40 slides
Rabbitmq basics by
Rabbitmq basicsRabbitmq basics
Rabbitmq basicsAbdriy Mosin
1.3K views14 slides

Viewers also liked(18)

High Performance RPC with Finagle by Samir Bessalah
High Performance RPC with FinagleHigh Performance RPC with Finagle
High Performance RPC with Finagle
Samir Bessalah7K views
Exploring Twitter's Finagle technology stack for microservices by 💡 Tomasz Kogut
Exploring Twitter's Finagle technology stack for microservicesExploring Twitter's Finagle technology stack for microservices
Exploring Twitter's Finagle technology stack for microservices
💡 Tomasz Kogut3.2K views
Ember vs Backbone by Abdriy Mosin
Ember vs BackboneEmber vs Backbone
Ember vs Backbone
Abdriy Mosin1.1K views
Service Objects Evolution by Abdriy Mosin
Service Objects EvolutionService Objects Evolution
Service Objects Evolution
Abdriy Mosin768 views
Distributed & Highly Available server applications in Java and Scala by Max Alexejev
Distributed & Highly Available server applications in Java and ScalaDistributed & Highly Available server applications in Java and Scala
Distributed & Highly Available server applications in Java and Scala
Max Alexejev5.6K views
A sane approach to microservices by Toby Matejovsky
A sane approach to microservicesA sane approach to microservices
A sane approach to microservices
Toby Matejovsky1.8K views
Finagle-Based Microservices at SoundCloud by Phil Calçado
Finagle-Based Microservices at SoundCloudFinagle-Based Microservices at SoundCloud
Finagle-Based Microservices at SoundCloud
Phil Calçado5.5K views
The end of polling : why and how to transform a REST API into a Data Streamin... by Audrey Neveu
The end of polling : why and how to transform a REST API into a Data Streamin...The end of polling : why and how to transform a REST API into a Data Streamin...
The end of polling : why and how to transform a REST API into a Data Streamin...
Audrey Neveu1.1K views
Spring Cloud Netflixを使おう #jsug by Toshiaki Maki
Spring Cloud Netflixを使おう #jsugSpring Cloud Netflixを使おう #jsug
Spring Cloud Netflixを使おう #jsug
Toshiaki Maki14.1K views
ELK at LinkedIn - Kafka, scaling, lessons learned by Tin Le
ELK at LinkedIn - Kafka, scaling, lessons learnedELK at LinkedIn - Kafka, scaling, lessons learned
ELK at LinkedIn - Kafka, scaling, lessons learned
Tin Le22.6K views
Functional Programming Patterns (BuildStuff '14) by Scott Wlaschin
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin175.3K views

Similar to Async Microservices with Twitter's Finagle

Compose Async with RxJS by
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
5.7K views178 slides
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015 by
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015NAVER / MusicPlatform
38.4K views54 slides
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр... by
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...GeeksLab Odessa
683 views33 slides
Finagle Lightning Talk JPR 2014 by
Finagle Lightning Talk JPR 2014Finagle Lightning Talk JPR 2014
Finagle Lightning Talk JPR 2014Chris Phelps
263 views12 slides
Building Scalable Stateless Applications with RxJava by
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
7.6K views38 slides
ClojureScript loves React, DomCode May 26 2015 by
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
411 views52 slides

Similar to Async Microservices with Twitter's Finagle(20)

서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015 by NAVER / MusicPlatform
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
NAVER / MusicPlatform38.4K views
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр... by GeeksLab Odessa
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
GeeksLab Odessa683 views
Finagle Lightning Talk JPR 2014 by Chris Phelps
Finagle Lightning Talk JPR 2014Finagle Lightning Talk JPR 2014
Finagle Lightning Talk JPR 2014
Chris Phelps263 views
Building Scalable Stateless Applications with RxJava by Rick Warren
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
Rick Warren7.6K views
ClojureScript loves React, DomCode May 26 2015 by Michiel Borkent
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
Michiel Borkent411 views
Quill - 一個 Scala 的資料庫存取利器 by vito jeng
Quill - 一個 Scala 的資料庫存取利器Quill - 一個 Scala 的資料庫存取利器
Quill - 一個 Scala 的資料庫存取利器
vito jeng79 views
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease by KAI CHU CHUNG
GDG Devfest 2019 - Build go kit microservices at kubernetes with easeGDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
KAI CHU CHUNG926 views
RxJava applied [JavaDay Kyiv 2016] by Igor Lozynskyi
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi514 views
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai by Databricks
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin HuaiA Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
Databricks10.9K views
Job Queue in Golang by Bo-Yi Wu
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
Bo-Yi Wu22.3K views
Coding in Style by scalaconfjp
Coding in StyleCoding in Style
Coding in Style
scalaconfjp1.4K views
Functional Programming from OO perspective (Sayeret Lambda lecture) by Ittay Dror
Functional Programming from OO perspective (Sayeret Lambda lecture)Functional Programming from OO perspective (Sayeret Lambda lecture)
Functional Programming from OO perspective (Sayeret Lambda lecture)
Ittay Dror772 views
A practical work of matlab by SalanSD
A practical work of matlabA practical work of matlab
A practical work of matlab
SalanSD240 views
Functional Systems @ Twitter by C4Media
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ Twitter
C4Media1.5K views
科特林λ學 by 彥彬 洪
科特林λ學科特林λ學
科特林λ學
彥彬 洪450 views
CodiLime Tech Talk - Katarzyna Ziomek-Zdanowicz: RxJS main concepts and real ... by CodiLime
CodiLime Tech Talk - Katarzyna Ziomek-Zdanowicz: RxJS main concepts and real ...CodiLime Tech Talk - Katarzyna Ziomek-Zdanowicz: RxJS main concepts and real ...
CodiLime Tech Talk - Katarzyna Ziomek-Zdanowicz: RxJS main concepts and real ...
CodiLime30 views
Understanding reactive programming with microsoft reactive extensions by Oleksandr Zhevzhyk
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensions
Oleksandr Zhevzhyk201 views

Recently uploaded

Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
61 views21 slides
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc
160 views29 slides
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueShapeBlue
179 views7 slides
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...ShapeBlue
117 views25 slides
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...ShapeBlue
79 views17 slides
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TShapeBlue
112 views34 slides

Recently uploaded(20)

TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc160 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue179 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue117 views
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ... by ShapeBlue
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
Import Export Virtual Machine for KVM Hypervisor - Ayush Pandey - University ...
ShapeBlue79 views
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue112 views
Business Analyst Series 2023 - Week 4 Session 7 by DianaGray10
Business Analyst Series 2023 -  Week 4 Session 7Business Analyst Series 2023 -  Week 4 Session 7
Business Analyst Series 2023 - Week 4 Session 7
DianaGray10126 views
The Power of Heat Decarbonisation Plans in the Built Environment by IES VE
The Power of Heat Decarbonisation Plans in the Built EnvironmentThe Power of Heat Decarbonisation Plans in the Built Environment
The Power of Heat Decarbonisation Plans in the Built Environment
IES VE69 views
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas... by Bernd Ruecker
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
iSAQB Software Architecture Gathering 2023: How Process Orchestration Increas...
Bernd Ruecker50 views
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by ShapeBlue
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
ShapeBlue144 views
Confidence in CloudStack - Aron Wagner, Nathan Gleason - Americ by ShapeBlue
Confidence in CloudStack - Aron Wagner, Nathan Gleason - AmericConfidence in CloudStack - Aron Wagner, Nathan Gleason - Americ
Confidence in CloudStack - Aron Wagner, Nathan Gleason - Americ
ShapeBlue88 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson156 views
The Role of Patterns in the Era of Large Language Models by Yunyao Li
The Role of Patterns in the Era of Large Language ModelsThe Role of Patterns in the Era of Large Language Models
The Role of Patterns in the Era of Large Language Models
Yunyao Li80 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue140 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue93 views
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P... by ShapeBlue
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
Developments to CloudStack’s SDN ecosystem: Integration with VMWare NSX 4 - P...
ShapeBlue154 views
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha... by ShapeBlue
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
Mitigating Common CloudStack Instance Deployment Failures - Jithin Raju - Sha...
ShapeBlue138 views
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software385 views
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue by ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlueMigrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
Migrating VMware Infra to KVM Using CloudStack - Nicolas Vazquez - ShapeBlue
ShapeBlue176 views

Async Microservices with Twitter's Finagle

  • 1. Async Microservices with Finagle Vladimir Kostyukov http://vkostyukov.ru
  • 4. Built atop of Netty (#1 Non-Blocking I/O for JVM) 4
  • 5. Finagle - Netty in a functional setting 5
  • 6. Building Blocks: Services + Futures + Filters 6
  • 7. trait Service[-Req, +Rep] extends (Req => Future[Rep]) 7
  • 8. Service-Oriented rather then Stream-Oriented 8
  • 9. // TODO: Don't forget to remove it in production. object GetDeveloperPromotionDate extends Service[Developer, Date] { def apply(req: Developer) = Future.never } val date: Future[Date] = GetDeveloperPromotionDate(Developer.Myself) 9
  • 10. 1 val respond = new Service[HttpRequest, HttpResponse] { 2 def apply(req: HttpRequest) = { 3 val rep = new DefaultHttpResponse(HttpVersion.HTTP_1_1, 4 HttpResponseStatus.OK) 5 rep.setContentString(req.getUri()) 6 Future.value(rep) 7 } 8 } 9 10 Await.ready(Http.serve(":8080", respond) 10
  • 11. Servers and Clients talk to each other via services 11
  • 12. 1 val to = Http.newService("google.com") 2 3 val proxy = new Service[HttpRequest, HttpResponse] { 4 def apply(req: HttpRequest) = to(req) 5 } 6 7 Await.ready(Http.serve(":8080", proxy) 12
  • 13. Are there high-order services? 13
  • 14. 14 Client Service Server Filter Filter Filter Service
  • 15. // ReqIn -> Service(ReqOut -> RepIn) -> RepOut trait Filter[-ReqIn, +RepOut, +ReqOut, -RepIn] extends ((ReqIn, Service[ReqOut, RepIn]) => Future[RepOut]) 15
  • 16. // A filter that does nothing. def identity[Req, Rep] = new Filter[Req, Rep, Req, Rep] { def apply(req: Req, service: Service[Req, Rep]) = service(req) } val auth = identity[HttpRequest, HttpResponse] 16
  • 18. A Type-Safe Input Filter 1 object TurnAIntoB extends Filter[A, Rep, B, Rep] { 2 def apply(req: A, service: Service[B, Rep]): Future[Rep] = 3 service(req.toB) 4 } 5 6 val serviceOfB: Service[B, Rep] = ... 7 val serviceOfA: Service[A, Rep] = TurnAIntoB andThen serviceOfA 18
  • 19. A Type-Safe Output Filter 1 object TurnAIntoB extends Filter[Req, B, Req, A] { 2 def apply(req: Req, service: Service[Req, A]): Future[B] = 3 service(req) map { a => a.toB } 4 } 5 6 val serviceOfA: Service[Req, A] = ... 7 val serviceOfB: Service[Req, B] = TurnAIntoB andThen serviceOfA 19
  • 20. Case Study: TimeOut Filter 1 def timeout[Req, Rep](timeout: Duration)(implicit timer: Timer) = 2 new SimpleFilter[Req, Rep] { 3 def apply(req: Req, service: Service[Req, Rep]) = 4 service(req).within(timer, timeout) 5 } 6 7 val handleExceptions = new SimpleFilter[Req, Rep] { 8 def apply(req: Req, service: Service[Req, Rep]) = 9 service(req) handle { 10 case t: TimeOutException => Future.value(defaultRep) 11 } 12 } 13 14 val respond = new Service[Req, Rep] { ... } 15 // We guarantee 3 seconds response time. 16 val backend = handleExceptions andThen 17 timeout(3.seconds) andThen 18 respond 20
  • 21. Case Study: OAuth2Filter 1 class OAuth2Filter[U](dataHandler: DataHandler[U]) 2 extends Filter[HttpRequest, HttpResponse, OAuth2Request[U], HttpResponse] 3 with OAuth2 with OAuthErrorHandler { 4 5 def handleError(e: OAuthError) = e.toHttpResponse 6 def apply(req: HttpRequest, service: Service[OAuth2Request[U], HttpResponse]) = 7 authorize(req, dataHandler) flatMap { authInfo => 8 service(OAuth2Request(authInfo, req)) 9 } handle { 10 case e: OAuthError => handleError(e) 11 } 12 } 13 14 val authorize = new OAuth2Filter(...) 15 val respond: Service[OAuth2Request[U], HttpResponse] = ??? 16 val backend: Service[HttpRequest, HttpResponse] = authorize andThen respond 21
  • 22. Future is a monad! 22
  • 23. Abstraction: Fibonacci Calculator 1 trait FibonacciCalculator { 2 val Zero = BigInt(0) 3 val One = BigInt(1) 4 val Two = BigInt(2) 5 6 def calculate(n: BigInt): Future[BigInt] 7 } 23
  • 24. Sequential Composition: map & flatMap 1 trait Future[+A] { 2 def flatMap[B](f: A => Future[B]): Future[B] 3 def map[B](f: A => B): Future[B] 4 } 24
  • 25. FlatMap Power 1 val ab: Service[A, B] = ??? 2 val bc: Service[B, C] = ??? 3 val cd: Service[C, D] = ??? 4 5 val req: A = ??? 6 val rep: Future[D] = ab(req) flatMap bc flatMap cd 25
  • 26. Sequential Composition: map & flatMap 1 object FlatMapFibonacciCalculator extends FibonacciCalculator { 2 def calculate(n: BigInt): Future[BigInt] = 3 if (n == Zero || n == One) Future.value(n) 4 else calculate(n - One) flatMap { a => 5 calculate(n - Two) flatMap { b => Future.value(a + b) } 6 } 7 } 8 9 object MapFibonacciCalculator extends FibonacciCalculator { 10 def calculate(n: BigInt): Future[BigInt] = 11 if (n == Zero || n == One) Future.value(n) 12 else calculate(n - One) map { a => 13 calculate(n - Two) map { b => a + b } 14 } 15 } 26
  • 27. Sequential Composition: for-comprehension 1 object ForFibonacciCalculator extends FibonacciCalculator { 2 def calculate(n: BigInt): Future[BigInt] = 3 if (n == Zero || n == One) Future.value(n) 4 else for { 5 a <- calculate(n - One) 6 b <- calculate(n - Two) 7 } yield a + b 8 } 27
  • 28. Concurrent Composition: collect & select 1 object Future { 2 def collect[A](fs: Seq[Future[A]]): Future[Seq[A]] 3 def select[A](fs: Seq[Future[A]]): Future[A] 4 } 28
  • 29. Concurrent Composition: collect 1 class FanoutFibonacciCalculator( 2 left: FibonacciCalculator, 3 right: FibonacciCalculator) extends FibonacciCalculator { 4 5 def calculate(n: BigInt): Future[BigInt] = 6 if (n == Zero) || n == One) Future.value(n) 7 else { 8 val seq = Seq(left.calculate(n - One), right.calculate(n - Two)) 9 Future.collect(seq) map { _.sum } 10 } 11 } 29
  • 30. Concurrent Composition: select 1 class SelectFibonacciCalculator(seq: Seq[FibonacciCalculator]) 2 extends FibonacciCalculator { 3 4 def calculate(n: BigInt): Future[BigInt] = { 5 val futures: Seq[Future[BigInt]] = seq map { _.calculate(n) } 6 Future.select(futures) 7 } 8 } 30
  • 31. Futures should be chained asynchronously 31
  • 32. Do Not Await // Asynchronous operation. val f = service(req) // Blocking operation. Await.result(f) 32
  • 33. Involving Side-Effects // Asynchronous operation. val f = service(req) ! f onSuccess { rep => println("Got the value: " + rep) } onFailure { t => t.printStackTrace() } 33
  • 34. Finagle is a non-blocking glue for services that implement different protocols 34
  • 35. Finagle is extremely good at gluing things that work with different speed 35
  • 36. References § http://twitter.github.io/finagle/ ! § http://vkostyukov.ru/posts/finagle-your-fibonacci-calculation/ ! § https://github.com/finagle/finch ! § https://github.com/finagle/finagle-oauth2 36
  • 37. Stay Finagled! ! And drop your feedbacks to @vkostyukov 37