Slide 1

Slide 1 text

Scaling up & out - with Actors √ Akka Tech Lead Email: [email protected] Twitter: @viktorklang Saturday, March 10, 12

Slide 2

Slide 2 text

2.0 Saturday, March 10, 12

Slide 3

Slide 3 text

Akka (Áhkká): The name comes from the goddess in the Sami mythology that represented all the wisdom and beauty in the world. It is also the name of a beautiful mountain in Laponia in the north part of Sweden 2.0 Saturday, March 10, 12

Slide 4

Slide 4 text

View from the summit 2.0 Saturday, March 10, 12

Slide 5

Slide 5 text

The problem It is way too hard to build ‣ correct highly concurrent systems ‣ truly scalable systems ‣ self-healing, fault-tolerant systems ...using “state-of-the-art” tools Saturday, March 10, 12

Slide 6

Slide 6 text

Concurrency Scalability Fault-tolerance …made Simpler Vision Saturday, March 10, 12

Slide 7

Slide 7 text

Manage system overload Saturday, March 10, 12

Slide 8

Slide 8 text

Scaling: up & out Saturday, March 10, 12

Slide 9

Slide 9 text

Replicate and distribute for fault-tolerance Saturday, March 10, 12

Slide 10

Slide 10 text

Transparent load balancing Saturday, March 10, 12

Slide 11

Slide 11 text

Markets Use cases FINANCE & BANKING BETTING & GAMING TELECOM & TV E-COMMERCE SIMULATION SOCIAL • Event-driven messaging systems • Stock trend Analysis & Simulation • Streaming media network gateways • Massive multiplayer online gaming • High throughput and transactional betting • 3D simulation engines • Social media community sites Applicability Saturday, March 10, 12

Slide 12

Slide 12 text

Akka 2.0 Saturday, March 10, 12

Slide 13

Slide 13 text

Design for distribution – In-VM is an optimization opportunity Saturday, March 10, 12

Slide 14

Slide 14 text

Async at heart – Even for control Saturday, March 10, 12

Slide 15

Slide 15 text

CAS is mother, CAS is father. – No locks in hot path Saturday, March 10, 12

Slide 16

Slide 16 text

Fault-tolerance as default – Enforced parental supervision Saturday, March 10, 12

Slide 17

Slide 17 text

Always room for improvement – Simplify, Refactor, Replace Saturday, March 10, 12

Slide 18

Slide 18 text

A simple version bump? Saturday, March 10, 12

Slide 19

Slide 19 text

700 tickets closed Saturday, March 10, 12

Slide 20

Slide 20 text

1020 files changed Saturday, March 10, 12

Slide 21

Slide 21 text

98683 lines added Saturday, March 10, 12

Slide 22

Slide 22 text

57415 lines removed Saturday, March 10, 12

Slide 23

Slide 23 text

Scale up Saturday, March 10, 12

Slide 24

Slide 24 text

Akka Actors one tool in the toolbox Saturday, March 10, 12

Slide 25

Slide 25 text

Behavior State Actor Saturday, March 10, 12

Slide 26

Slide 26 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 27

Slide 27 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 28

Slide 28 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 29

Slide 29 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 30

Slide 30 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 31

Slide 31 text

Behavior State Actor Saturday, March 10, 12

Slide 32

Slide 32 text

Event- driven Behavior State Actor Saturday, March 10, 12

Slide 33

Slide 33 text

import akka.actor.ActorSystem import com.typesafe.config.ConfigFactory val system = ActorSystem( “my-app-name”, ConfigFactory.load(“application”) ) Create Application Scala API Saturday, March 10, 12

Slide 34

Slide 34 text

case object Tick class Counter extends Actor { var counter = 0 def receive = { case Tick => counter += 1 println(counter) } } Actors Scala API Saturday, March 10, 12

Slide 35

Slide 35 text

val counter = system.actorOf(Props[Counter]) Create Actors counter is an ActorRef Creates a top-level actor Scala API Saturday, March 10, 12

Slide 36

Slide 36 text

system stop actorRef Stop actors ...also stops all actors in the hierarchy below Saturday, March 10, 12

Slide 37

Slide 37 text

counter ! Tick Send: ! Scala API fire-forget Saturday, March 10, 12

Slide 38

Slide 38 text

counter tell Tick counter.tell(Tick, sender) ...or use tell fire-forget Scala API Saturday, March 10, 12

Slide 39

Slide 39 text

import akka.patterns.ask implicit val timeout = Timeout(5 seconds) val f = actor ? message Send: ? returns a Future[Any] Scala API Saturday, March 10, 12

Slide 40

Slide 40 text

import akka.patterns.ask val future = ask(actor, message, 5 seconds) Send: ? returns a Future[Any] Scala API Saturday, March 10, 12

Slide 41

Slide 41 text

class SomeActor extends Actor { def receive = { case User(name) => // reply to sender sender ! (“Hi ” + name) } } Reply Scala API Saturday, March 10, 12

Slide 42

Slide 42 text

become context become { // new body case NewMessage => ... } Scala API Saturday, March 10, 12

Slide 43

Slide 43 text

unbecome context.unbecome() Saturday, March 10, 12

Slide 44

Slide 44 text

Routers ‣ RoundRobin ‣ Random ‣ SmallestMailbox ‣ Broadcast ‣ ScatterGatherFirstCompleted … Custom Saturday, March 10, 12

Slide 45

Slide 45 text

Routers val router = system.actorOf( Props[SomeActor].withRouter( RoundRobinRouter(nrOfInstances = 5))) Scala API Saturday, March 10, 12

Slide 46

Slide 46 text

Router + Resizer val resizer = DefaultResizer(lowerBound = 2, upperBound = 15) val router = system.actorOf( Props[ExampleActor1].withRouter( RoundRobinRouter(resizer = Some(resizer)) ) ) Scala API Saturday, March 10, 12

Slide 47

Slide 47 text

Scale up? Saturday, March 10, 12

Slide 48

Slide 48 text

ThreadPoolExecutor Saturday, March 10, 12

Slide 49

Slide 49 text

ThreadPoolExecutor Saturday, March 10, 12

Slide 50

Slide 50 text

new ForkJoinPool Saturday, March 10, 12

Slide 51

Slide 51 text

new ForkJoinPool Saturday, March 10, 12

Slide 52

Slide 52 text

~2.7m actors/GB Saturday, March 10, 12

Slide 53

Slide 53 text

Let it crash fault-tolerance Saturday, March 10, 12

Slide 54

Slide 54 text

Saturday, March 10, 12

Slide 55

Slide 55 text

Which components have critically important state and explicit error handling? Saturday, March 10, 12

Slide 56

Slide 56 text

Saturday, March 10, 12

Slide 57

Slide 57 text

Fault-tolerant onion-layered Error Kernel Saturday, March 10, 12

Slide 58

Slide 58 text

Error Kernel Saturday, March 10, 12

Slide 59

Slide 59 text

Error Kernel Saturday, March 10, 12

Slide 60

Slide 60 text

Error Kernel Saturday, March 10, 12

Slide 61

Slide 61 text

Error Kernel Saturday, March 10, 12

Slide 62

Slide 62 text

Error Kernel Saturday, March 10, 12

Slide 63

Slide 63 text

// from within an actor val child = context.actorOf(Props[MyActor], “my-actor”) Parental automatic supervision Scala API transparent and automatic fault handling by design Saturday, March 10, 12

Slide 64

Slide 64 text

Guardian System Actor Parental automatic supervision Saturday, March 10, 12

Slide 65

Slide 65 text

Guardian System Actor Parental automatic supervision system.actorOf(Props[Foo], “Foo”) Saturday, March 10, 12

Slide 66

Slide 66 text

Foo Guardian System Actor Parental automatic supervision system.actorOf(Props[Foo], “Foo”) Saturday, March 10, 12

Slide 67

Slide 67 text

Foo Guardian System Actor Parental automatic supervision context.actorOf(Props[A], “A”) Saturday, March 10, 12

Slide 68

Slide 68 text

A Foo Guardian System Actor Parental automatic supervision context.actorOf(Props[A], “A”) Saturday, March 10, 12

Slide 69

Slide 69 text

A B Bar Foo C B E A D C Guardian System Actor Parental automatic supervision Saturday, March 10, 12

Slide 70

Slide 70 text

A B Bar Foo C B E A D C Name resolution Guardian System Actor Saturday, March 10, 12

Slide 71

Slide 71 text

A B Bar Foo C B E A D C /Foo Name resolution Guardian System Actor Saturday, March 10, 12

Slide 72

Slide 72 text

A B Bar Foo C B E A D C /Foo /Foo/A Name resolution Guardian System Actor Saturday, March 10, 12

Slide 73

Slide 73 text

A B Bar Foo C B E A D C /Foo /Foo/A /Foo/A/B Name resolution Guardian System Actor Saturday, March 10, 12

Slide 74

Slide 74 text

A B Bar Foo C B E A D C /Foo /Foo/A /Foo/A/B Name resolution /Foo/A/D Guardian System Actor Saturday, March 10, 12

Slide 75

Slide 75 text

ActorPath val actorRef = system.actorFor("/user/pigdog") val parent = context.actorFor("..") val sibling = context.actorFor("../pigdog") val refPath: ActorPath = actorRef.path Saturday, March 10, 12

Slide 76

Slide 76 text

ActorSelection val selection = system.actorSelection("/user/services/*") selection ! "pigdog" Saturday, March 10, 12

Slide 77

Slide 77 text

Supervision class MySupervisor extends Actor { def supervisorStrategy = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate }, maxNrOfRetries = None, withinTimeRange = None) def receive = { case NewUser(name) => ... = context.actorOf[User](name) } } Scala API Saturday, March 10, 12

Slide 78

Slide 78 text

Supervision class MySupervisor extends Actor { def supervisorStrategy = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate }, maxNrOfRetries = None, withinTimeRange = None) def receive = { case NewUser(name) => ... = context.actorOf[User](name) } } AllForOneStrategy Scala API Saturday, March 10, 12

Slide 79

Slide 79 text

class FaultTolerantService extends Actor { ... override def preRestart( reason: Throwable, message: Option[Any]) = { ... // clean up before restart } override def postRestart(reason: Throwable) = { ... // init after restart } } Manage failure Scala API Saturday, March 10, 12

Slide 80

Slide 80 text

val buddy: ActorRef = ... val watcher = context.actorOf(Props( new Actor { context.watch(buddy) def receive = { case Terminated(`buddy`) ⇒ … } } )) watch/unwatch Scala API Saturday, March 10, 12

Slide 81

Slide 81 text

Scale out Saturday, March 10, 12

Slide 82

Slide 82 text

New Remote Actors Saturday, March 10, 12

Slide 83

Slide 83 text

val actor = system.actorOf(Props[MyActor], “my-service”) Name Bind the actor to a name Scala API Saturday, March 10, 12

Slide 84

Slide 84 text

Actor name is virtual and decoupled from how it is deployed Deployment Saturday, March 10, 12

Slide 85

Slide 85 text

Deployment If no deployment configuration exists then actor is deployed as local Saturday, March 10, 12

Slide 86

Slide 86 text

Deployment The same system can be configured as distributed without code change (even change at runtime) Saturday, March 10, 12

Slide 87

Slide 87 text

Deployment Write as local but deploy as distributed in the cloud without code change Saturday, March 10, 12

Slide 88

Slide 88 text

Deployment Allows runtime to dynamically and adaptively change topology Saturday, March 10, 12

Slide 89

Slide 89 text

akka { actor { deployment { /my-service { router = "round-robin" nr-of-instances = 3 target { nodes = ["wallace:2552", "gromit:2552"] } } } } } Deployment configuration Saturday, March 10, 12

Slide 90

Slide 90 text

Error Kernel Saturday, March 10, 12

Slide 91

Slide 91 text

Node 1 Node 2 Saturday, March 10, 12

Slide 92

Slide 92 text

‣ EventBus API ‣ Extensions ‣ ScalaSTM ‣ TypedActor 2.0 More news Saturday, March 10, 12

Slide 93

Slide 93 text

Akka 2.0+ Saturday, March 10, 12

Slide 94

Slide 94 text

‣ Apache Camel ‣ AMQP ‣ Spring Integration Saturday, March 10, 12

Slide 95

Slide 95 text

Get it and learn more http://akka.io http://typesafe.com Saturday, March 10, 12

Slide 96

Slide 96 text

EOF Saturday, March 10, 12