Getting Cozy with Actors
Pain-free Concurrency in Java
Balaji Srinivasaraghavan
Goal
• Relational Database = JPA/Hibernate• REST = JAX-RS
• Method calls = built into the language
•Concurrency = Executor Service ?? L
©2017 PayPal Inc. Confidential and proprietary.
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.
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.
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.
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;
}}
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.
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.
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:
Dispatcher
©2017 PayPal Inc. Confidential and proprietary.
©2017 PayPal Inc. Confidential and proprietary.
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.
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.
Still harder than it needs to be…
Threads Executors Actors Streams
Level of Abstraction
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.
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
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
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.
PayPal Open Source
©2017 PayPal Inc. Confidential and proprietary.
Akka/Spray for Large-Scale Production Deployments
• Unicomplex micro-container
• TestKit
• Enriched HttpClient
• Patterns
• Pipeline
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
Q & A
Thanks