Download - Building Reactive Applications with Akka (in Scala)

Transcript
Page 1: Building Reactive Applications with Akka (in Scala)

Building Reactive Applications with Akka

Jonas Bonér Typesafe

CTO & co-founder @jboner

Page 2: Building Reactive Applications with Akka (in Scala)

This is an era of profound change.

Page 3: Building Reactive Applications with Akka (in Scala)

Reactive  Applications

Implications are massive, change is unavoidable

3

!!!Users are demanding richer and more personalized experiences. !Yet, at the same time, expecting

blazing fast load time.

Users!!!

Mobile and HTML5; Data and compute clouds; scaling on

demand. !Modern application technologies

are fueling the always-on, real-time user expectation.

Applications !!!Businesses are being pushed to

react to these changing user expectations… !...and embrace

modern application requirements. !!!!!!!

Businesses

Page 4: Building Reactive Applications with Akka (in Scala)

As a matter of necessity, businesses are going Reactive.

Page 5: Building Reactive Applications with Akka (in Scala)

Reactive  Applications

Reactive applications share four traits

5

Page 6: Building Reactive Applications with Akka (in Scala)

Reactive applications react to changes in the world around them.

Page 7: Building Reactive Applications with Akka (in Scala)

Event-Driven• Loosely coupled architecture, easier to extend, maintain, evolve

• Asynchronous and non-blocking • Concurrent by design, immutable state • Lower latency and higher throughput

7

“Clearly, the goal is to do these operations concurrently and non-blocking, so that entire blocks of seats or sections are not locked.

We’re able to find and allocate seats under load in less than 20ms without trying very hard to achieve it.”  

Andrew Headrick, Platform Architect, Ticketfly

Page 8: Building Reactive Applications with Akka (in Scala)

Introducing the Actor Model.

Page 9: Building Reactive Applications with Akka (in Scala)

9

The Actor Model

Page 10: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

The Actor Model

Page 11: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

The Actor Model

Page 12: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

The Actor Model

Page 13: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

The Actor Model

Page 14: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

Supports 3 axioms—when an Actor receives a message it can:

The Actor Model

Page 15: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

Supports 3 axioms—when an Actor receives a message it can:

1. Create new Actors

The Actor Model

Page 16: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

Supports 3 axioms—when an Actor receives a message it can:

1. Create new Actors

2. Send messages to Actors it knows

The Actor Model

Page 17: Building Reactive Applications with Akka (in Scala)

9

A computational model that embodies:

✓ Processing

✓ Storage

✓ Communication

Supports 3 axioms—when an Actor receives a message it can:

1. Create new Actors

2. Send messages to Actors it knows

3. Designate how it should handle the next message it receives

The Actor Model

Page 18: Building Reactive Applications with Akka (in Scala)

The essence of an actor0. DEFINE

1. CREATE

2. SEND

3. BECOME

4. SUPERVISE

10

Page 19: Building Reactive Applications with Akka (in Scala)

0. DEFINE

11

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }

Page 20: Building Reactive Applications with Akka (in Scala)

0. DEFINE

11

Define the message(s) the Actor should be able to respond to

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }

Page 21: Building Reactive Applications with Akka (in Scala)

0. DEFINE

11

Define the message(s) the Actor should be able to respond to

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }

Define the Actor class

Page 22: Building Reactive Applications with Akka (in Scala)

0. DEFINE

11

Define the message(s) the Actor should be able to respond to

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }

Define the Actor class

Define the Actor’s behavior

Page 23: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

1. CREATE

Page 24: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

1. CREATE

Create an Actor system

Page 25: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

1. CREATE

Create an Actor systemActor configuration

Page 26: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

Give it a name

1. CREATE

Create an Actor systemActor configuration

Page 27: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

Give it a name

1. CREATE

Create the Actor

Create an Actor systemActor configuration

Page 28: Building Reactive Applications with Akka (in Scala)

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter")

Give it a name

1. CREATE

Create the ActorYou get an ActorRef back

Create an Actor systemActor configuration

Page 29: Building Reactive Applications with Akka (in Scala)

Guardian System Actor

Actors can form hierarchies

Page 30: Building Reactive Applications with Akka (in Scala)

Guardian System Actor

system.actorOf(Props[Foo], “Foo”)

Actors can form hierarchies

Page 31: Building Reactive Applications with Akka (in Scala)

Foo

Guardian System Actor

system.actorOf(Props[Foo], “Foo”)

Actors can form hierarchies

Page 32: Building Reactive Applications with Akka (in Scala)

Foo

Guardian System Actor

context.actorOf(Props[A], “A”)

Actors can form hierarchies

Page 33: Building Reactive Applications with Akka (in Scala)

A

Foo

Guardian System Actor

context.actorOf(Props[A], “A”)

Actors can form hierarchies

Page 34: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

Guardian System Actor

Actors can form hierarchies

Page 35: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

Guardian System Actor

Name resolution—like a file-system

Page 36: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

/Foo

Guardian System Actor

Name resolution—like a file-system

Page 37: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

Guardian System Actor

Name resolution—like a file-system

Page 38: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

/Foo/A/B

Guardian System Actor

Name resolution—like a file-system

Page 39: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

/Foo/A/B

/Foo/A/D

Guardian System Actor

Name resolution—like a file-system

Page 40: Building Reactive Applications with Akka (in Scala)

2. SEND

15

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s”Hello ${who}") } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")

Page 41: Building Reactive Applications with Akka (in Scala)

2. SEND

15

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s”Hello ${who}") } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")

Send the message asynchronously

Page 42: Building Reactive Applications with Akka (in Scala)

Bring it together

16

case class Greeting(who: String) !class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s”Hello ${who}") } } !val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")

Page 43: Building Reactive Applications with Akka (in Scala)

DEMO TIMEA simple game of ping pong

Page 44: Building Reactive Applications with Akka (in Scala)

3. BECOME

18

class GreetingActor extends Actor with ActorLogging { def receive = happy ! val happy: Receive = { case Greeting(who) => log.info(s”Hello ${who}") case Angry => context become angry } ! val angry: Receive = { case Greeting(_) => log.info("Go away!") case Happy => context become happy } }

Page 45: Building Reactive Applications with Akka (in Scala)

3. BECOME

18

class GreetingActor extends Actor with ActorLogging { def receive = happy ! val happy: Receive = { case Greeting(who) => log.info(s”Hello ${who}") case Angry => context become angry } ! val angry: Receive = { case Greeting(_) => log.info("Go away!") case Happy => context become happy } }

Redefine the behavior

Page 46: Building Reactive Applications with Akka (in Scala)

Reactive applications are architected to handle failure at all levels.

Page 47: Building Reactive Applications with Akka (in Scala)

Resilient• Failure is embraced as a natural state in the app lifecycle

• Resilience is a first-class construct • Failure is detected, isolated, and managed • Applications self heal

20

“The Typesafe Reactive Platform helps us maintain a very aggressive development and deployment cycle, all in a fail-forward manner.

It’s now the default choice for developing all new services.”  

Peter Hausel, VP Engineering, Gawker Media

Page 48: Building Reactive Applications with Akka (in Scala)

Think Vending Machine

Page 49: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Think Vending Machine

Page 50: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Think Vending Machine

Page 51: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Add more coins

Think Vending Machine

Page 52: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Gets coffee

Add more coins

Think Vending Machine

Page 53: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Think Vending Machine

Page 54: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Think Vending Machine

Page 55: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Think Vending Machine

Out of coffee beans error

Page 56: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Think Vending Machine

Out of coffee beans errorWrong

Page 57: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Think Vending Machine

Page 58: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Inserts coins

Out of coffee beans

error

Think Vending Machine

Page 59: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Service Guy

Inserts coins

Out of coffee beans

error

Think Vending Machine

Page 60: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Service Guy

Inserts coins

Out of coffee beans

error

Adds more beans

Think Vending Machine

Page 61: Building Reactive Applications with Akka (in Scala)

Coffee MachineProgrammer

Service Guy

Inserts coins

Gets coffee

Out of coffee beans

error

Adds more beans

Think Vending Machine

Page 62: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Page 63: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Request

Page 64: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Request

Response

Page 65: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Request

Response

Validation Error

Page 66: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Request

Response

Validation Error

Application Error

Page 67: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Supervisor

Request

Response

Validation Error

Application Error

Page 68: Building Reactive Applications with Akka (in Scala)

The Right Way

ServiceClient

Supervisor

Request

Response

Validation Error

Application Error

Manages Failure

Page 69: Building Reactive Applications with Akka (in Scala)
Page 70: Building Reactive Applications with Akka (in Scala)

• Isolate the failure

• Compartmentalize

• Manage failure locally

• Avoid cascading failures

Use Bulkheads

Page 71: Building Reactive Applications with Akka (in Scala)

• Isolate the failure

• Compartmentalize

• Manage failure locally

• Avoid cascading failures

Use Bulkheads

Page 72: Building Reactive Applications with Akka (in Scala)

Enter Supervision

Page 73: Building Reactive Applications with Akka (in Scala)

Enter Supervision

Page 74: Building Reactive Applications with Akka (in Scala)

A

B

BarFoo

C

BE

A

D

C

Automatic and mandatory supervisionSupervisor hierarchies

Page 75: Building Reactive Applications with Akka (in Scala)

4. SUPERVISE

28

Every single actor has a default supervisor strategy.

Which is usually sufficient. But it can be overridden.

Page 76: Building Reactive Applications with Akka (in Scala)

4. SUPERVISE

28

Every single actor has a default supervisor strategy.

Which is usually sufficient. But it can be overridden.

class Supervisor extends Actor { override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } ! val worker = context.actorOf(Props[Worker], name = "worker") ! def receive = {

Page 77: Building Reactive Applications with Akka (in Scala)

4. SUPERVISE

28

class Supervisor extends Actor { override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } ! val worker = context.actorOf(Props[Worker], name = "worker") ! def receive = { case n: Int => worker forward n } } !

Page 78: Building Reactive Applications with Akka (in Scala)

Cleanup & (Re)initialization

29

class Worker extends Actor { ... override def preRestart( reason: Throwable, message: Option[Any]) { ... // clean up before restart } override def postRestart(reason: Throwable) { ... // init after restart } }

Page 79: Building Reactive Applications with Akka (in Scala)

Monitor through Death Watch

30

class Watcher extends Actor { val child = context.actorOf(Props.empty, "child") context.watch(child) ! def receive = { case Terminated(`child`) => … // handle child termination } }

Page 80: Building Reactive Applications with Akka (in Scala)

Reactive applications scale up and down to meet demand.

Page 81: Building Reactive Applications with Akka (in Scala)

Scalable• Scalability and elasticity to embrace the Cloud

• Leverage all cores via asynchronous programming • Clustered servers support joining and leaving of nodes • More cost-efficient utilization of hardware

32

“Our traffic can increase by as much as 100x for 15 minutes each day. Until a couple of years ago, noon was a stressful time.

Nowadays, it’s usually a non-event.”  

Eric Bowman, VP Architecture, Gilt Groupe

Page 82: Building Reactive Applications with Akka (in Scala)

33

Scale OUTScale UP

Page 83: Building Reactive Applications with Akka (in Scala)

33

Essentially the same thing

Page 84: Building Reactive Applications with Akka (in Scala)

34

1. Minimize Contention 2. Maximize Locality of Reference

We need to

Page 85: Building Reactive Applications with Akka (in Scala)

35

Share NOTHING

Design

Page 86: Building Reactive Applications with Akka (in Scala)

Fully event-driven apps are a necessity

36

Amdahl’s Law will hunt you down

Page 87: Building Reactive Applications with Akka (in Scala)

Define a router

37

val router = context.actorOf( RoundRobinPool(5).props(Props[Worker])), “router”)

Paths can be local or remote actor paths

Page 88: Building Reactive Applications with Akka (in Scala)

…or from config

38

akka.actor.deployment { /service/router { router = round-robin-pool resizer { lower-bound = 12 upper-bound = 15 } } }

Page 89: Building Reactive Applications with Akka (in Scala)

Turn on clustering

39

akka { actor { provider = "akka.cluster.ClusterActorRefProvider" ... }    cluster { seed-nodes = [ “akka.tcp://[email protected]:2551", “akka.tcp://[email protected]:2552" ]   auto-down = off } }

Page 90: Building Reactive Applications with Akka (in Scala)

Use clustered routers

40

akka.actor.deployment  {      /service/master  {          router  =  consistent-­‐hashing-­‐pool          nr-­‐of-­‐instances  =  100  !        cluster  {              enabled  =  on              max-nr-of-instances-per-node = 3              allow-­‐local-­‐routees  =  on              use-­‐role  =  compute          }      }  }

Page 91: Building Reactive Applications with Akka (in Scala)

Use clustered routers

40

akka.actor.deployment  {      /service/master  {          router  =  consistent-­‐hashing-­‐pool          nr-­‐of-­‐instances  =  100  !        cluster  {              enabled  =  on              max-nr-of-instances-per-node = 3              allow-­‐local-­‐routees  =  on              use-­‐role  =  compute          }      }  }

Or perhaps use an AdaptiveLoadBalancingPool

Page 92: Building Reactive Applications with Akka (in Scala)

Use clustered pub-sub

41

Page 93: Building Reactive Applications with Akka (in Scala)

Use clustered pub-sub

41

class Subscriber extends Actor { val mediator = DistributedPubSubExtension(context.system).mediator mediator ! Subscribe(“content”, self) def receive = { … } }

Page 94: Building Reactive Applications with Akka (in Scala)

Use clustered pub-sub

41

class Publisher extends Actor { val mediator = DistributedPubSubExtension(context.system).mediator def receive = { case in: String => mediator ! Publish("content", in.toUpperCase) } }

Page 95: Building Reactive Applications with Akka (in Scala)

• Cluster Membership • Cluster Leader • Clustered Singleton • Cluster Roles • Cluster Sharding

42

Other Akka Cluster features

Page 96: Building Reactive Applications with Akka (in Scala)

• Supports two different models: • Command Sourcing — at least once

• Event Sourcing — at most once

• Great for implementing • durable actors • replication • CQRS etc.

• Messages persisted to Journal and replayed on restart

43

Use Akka Persistence

Page 97: Building Reactive Applications with Akka (in Scala)

Akka  Persistence  Webinar

Command Sourcing Event Sourcingwrite-ahead-log derive events from a command

same behavior during recovery as normal operation external interaction can be problematic

only state-changing behavior during recovery

persisted before validation events cannot fail

allows retroactive changes to the business logic

fixing the business logic will not affect persisted events

naming: represent intent, imperative naming: things that have completed, verbs in past tense

Page 98: Building Reactive Applications with Akka (in Scala)

Akka  Persistence  Webinar

Life beyond Distributed Transactions: an Apostate’s Opinion

Position Paper by Pat Helland

“In general, application developers simply do not implement large scalable applications assuming distributed transactions.”  

Pat Helland

http://www-­‐db.cs.wisc.edu/cidr/cidr2007/papers/cidr07p15.pdf

Page 99: Building Reactive Applications with Akka (in Scala)

Akka  Persistence  Webinar

Consistency boundary

• Aggregate Root is the Transactional Boundary • Strong consistency within an Aggregate • Eventual consistency between Aggregates

• No limit to scalability

Page 100: Building Reactive Applications with Akka (in Scala)

Akka  Persistence  Webinar

Domain Events• Things that have completed, facts

• Immutable

• Verbs in past tense • CustomerRelocated • CargoShipped • InvoiceSent

“State transitions are an important part of our problem space and should be modeled within our domain.”  

Greg Young, 2008

Page 101: Building Reactive Applications with Akka (in Scala)

DEMO TIMEPersist a game of ping pong

Page 102: Building Reactive Applications with Akka (in Scala)

Reactive applications enrich the user experience with low latency response.

Page 103: Building Reactive Applications with Akka (in Scala)

Responsive• Real-time, engaging, rich and collaborative

• Create an open and ongoing dialog with users • More efficient workflow; inspires a feeling of connectedness • Fully Reactive enabling push instead of pull

50

“The move to these technologies is already paying off. Response times are down for processor intensive code–such as image

and PDF generation–by around 75%.”  

Brian Pugh, VP of Engineering, Lucid Software

Page 104: Building Reactive Applications with Akka (in Scala)

Responsive

51

• Keep latency consistent—under:

1. Blue sky scenarios

2. Traffic spikes

3. Failures

• The system should always be responsive

Page 105: Building Reactive Applications with Akka (in Scala)

http://reactivemanifesto.org

Page 106: Building Reactive Applications with Akka (in Scala)

Typesafe Activatorhttp://typesafe.com/platform/getstarted

Page 107: Building Reactive Applications with Akka (in Scala)

54

Typesafe Reactive Platform

Page 108: Building Reactive Applications with Akka (in Scala)

54

Typesafe Reactive Platform

• Asynchronous and immutable programming constructs

• Composable abstractions enabling simpler concurrency and parallelism

Page 109: Building Reactive Applications with Akka (in Scala)

54

Typesafe Reactive Platform

• Actors are asynchronous and communicate via message passing

• Supervision and clustering in support of fault tolerance

• Asynchronous and immutable programming constructs

• Composable abstractions enabling simpler concurrency and parallelism

Page 110: Building Reactive Applications with Akka (in Scala)

54

Typesafe Reactive Platform

• Actors are asynchronous and communicate via message passing

• Supervision and clustering in support of fault tolerance

• Purely asynchronous and non-blocking web frameworks

• No container required, no inherent bottlenecks in session management

• Asynchronous and immutable programming constructs

• Composable abstractions enabling simpler concurrency and parallelism

Page 111: Building Reactive Applications with Akka (in Scala)

Reactive is being adopted acrossa wide range of industries.

Page 112: Building Reactive Applications with Akka (in Scala)

56

Finance Internet/Social Media Mfg/Hardware Government Retail

Page 113: Building Reactive Applications with Akka (in Scala)

Questions?

Page 114: Building Reactive Applications with Akka (in Scala)

©Typesafe 2014 – All Rights Reserved