getting cozy with actors - developermarch...what is akka? actors ••simple & high performance...

23

Upload: others

Post on 22-May-2020

7 views

Category:

Documents


0 download

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Thanks

Page 23: Getting Cozy with Actors - DeveloperMarch...What is Akka? Actors ••simple & high performance concurrency ••Feature rich non-blocking HTTP server HTTP & client Cluster / Remoting

www.modsummit.com

www.developersummit.com