getting cozy with actors - developermarch...what is akka? actors ••simple & high performance...
TRANSCRIPT
![Page 1: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/1.jpg)
![Page 2: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/2.jpg)
Getting Cozy with Actors
Pain-free Concurrency in Java
Balaji Srinivasaraghavan
![Page 3: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/3.jpg)
Goal
• Relational Database = JPA/Hibernate• REST = JAX-RS
• Method calls = built into the language
•Concurrency = Executor Service ?? L
©2017 PayPal Inc. Confidential and proprietary.
![Page 4: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/4.jpg)
Modelling with Java
• Static aspects of a system− Object oriented modelling•Mirrors the real world
• Dynamic aspects of a system− Concurrency & communication− Threads – hardware model
©2017 PayPal Inc. Confidential and proprietary.
![Page 5: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/5.jpg)
Threads
• Good−Default pattern for concurrency−Reasonably well understood, 20 years old
• Bad−Inter-thread communication is a mess−Locks are hard to design & reason about−20 years old−Explicit concurrency
• Hardware model - world as a processor see’s it• Impedance mismatch between object oriented
models
©2017 PayPal Inc. Confidential and proprietary.
![Page 6: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/6.jpg)
Hardware Environment• Age of multi-core processors• Per core performance improvements begin
to flat line in ‘05
• “Free lunch is over”
©2017 PayPal Inc. Confidential and proprietary.
![Page 7: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/7.jpg)
Can we do better?
• Yes, let’s look other models• Functional programming• no shared mutable state• immutable POJOs
• Dataflow programming• model programs as a series of
transformations & connections• implicit concurrency
• Actor model• treats "actors" as the universal
primitive of concurrent computation
©2017 PayPal Inc. Confidential and proprietary.
public class Person {public final String name;
public Person(String name) {this.name = name;
}}
![Page 8: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/8.jpg)
Object
Java Actor
Object MailboxMailbox
MessageMessage
Message
Actors
©2017 PayPal Inc. Confidential and proprietary.
everything is an object everything is an actor
©2017 PayPal Inc. Confidential and proprietary.
![Page 9: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/9.jpg)
Actor
MailboxMailbox
StateMessageMessageMessage
• Actors have • State
• OO: class attributes
• Mailbox
• OO: method params
• Behavior
• State is internal & not shared, accessed through message passing
Behavior
©2017 PayPal Inc. Confidential and proprietary.
Actor Model
©2017 PayPal Inc. Confidential and proprietary.
![Page 10: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/10.jpg)
What is Akka?
••simple & high performance concurrencyActors
••Feature rich non-blocking HTTP server & clientHTTP
••location transparency, resilienceCluster / Remoting
••back-pressured data flowsStreams
©2017 PayPal Inc. Confidential and proprietary.
Java library that provides:
![Page 11: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/11.jpg)
Dispatcher
©2017 PayPal Inc. Confidential and proprietary.
©2017 PayPal Inc. Confidential and proprietary.
![Page 12: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/12.jpg)
public class Greeter extends AbstractActor {private String greeting = ”Hello";
public Greeter() {receive(ReceiveBuilder
.match(WhoToGreet.class, message -> greeting = ”Hello, " + message.who)
.match(Greet.class, message -> sender().tell(new Greeting(greeting), self()))
.build());}
}
Actor Operations - Define
©2017 PayPal Inc. Confidential and proprietary.
©2017 PayPal Inc. Confidential and proprietary.
![Page 13: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/13.jpg)
public class Greeter extends AbstractActor {private String greeting = ”Hello";
public Greeter() {receive(ReceiveBuilder
.match(WhoToGreet.class, message -> greeting = ”Hello, " + message.who)
.match( Greet.class, message -> sender().tell(new Greeting(greeting), self()))
.build());}
}
Actor Operations – Send Messages
©2017 PayPal Inc. Confidential and proprietary.
©2017 PayPal Inc. Confidential and proprietary.
![Page 14: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/14.jpg)
Still harder than it needs to be…
Threads Executors Actors Streams
Level of Abstraction
![Page 15: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/15.jpg)
Streams
• Defines a data flow• Programmer focuses on − transformations− composition
• Framework focuses on − Concurrency− Data movement− Creates actors internally
©2017 PayPal Inc. Confidential and proprietary.
![Page 16: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/16.jpg)
Akka Streams
©2017 PayPal Inc. Confidential and proprietary.
• A high-level DSL for defining stream processing graphs
• Machinery for transforming graphs into actors
• Non blocking all the way
• Backpressure enabled
![Page 17: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/17.jpg)
Stream Example
©2017 PayPal Inc. Confidential and proprietary.
messageQueueSource
.map(message -> parse(message))
.filter(Try::isSuccess) // filter out unparseable
.mapAsync(5, tweet -> detectBot(tweet.get())) //cpu bound
.filter(result -> result.isBot)
.groupedWithin(100, FiniteDuration.create(1, TimeUnit.SECONDS))
.runForeach(Step2BotDetection::writeFraudReport, materializer);
* from https://github.com/ktoso/akka-streams-alpakka-talk-demos-2016
![Page 18: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/18.jpg)
Use Cases
• Actors−maintain state across requests− natural fit for orchestration/façade services− clustering, distributed data designs
• Streams− batch processing of large data sets− structuring complex business logic− streaming data sources e.g. HTTP, Kafka, Oracle− low latency HTTP services
©2017 PayPal Inc. Confidential and proprietary.
![Page 19: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/19.jpg)
PayPal Open Source
©2017 PayPal Inc. Confidential and proprietary.
Akka/Spray for Large-Scale Production Deployments
• Unicomplex micro-container
• TestKit
• Enriched HttpClient
• Patterns
• Pipeline
![Page 20: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/20.jpg)
Attributions & Bibliography
©2017 PayPal Inc. Confidential and proprietary.
• Asynchronous stream processing with AkkaStreams by Johan Andrén JFokus,Stockholm, 2017-02-08
• Akka Actor Introduction – Gene
• Reactive Microservices Architecture ByJonas Bonér
• Akka Docs
• Lightbend Documentation & Case Studies
![Page 21: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/21.jpg)
Q & A
![Page 22: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting](https://reader030.vdocuments.site/reader030/viewer/2022011822/5ec97cc9258bef7b90516b62/html5/thumbnails/22.jpg)
Thanks