Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Akka 2.0: Scaling Up & Out With Actors

marakana
March 21, 2012
3.7k

Akka 2.0: Scaling Up & Out With Actors

Viktor Klang, tech lead of Akka at Typesafe, discusses scaling software with actors at the 2012 North East Scala Symposium. Traditional approaches to concurrency and distribution force the issue of choosing between productivity and scalability. The cause of that has been the wrong tools and the wrong layer of abstraction and Akka is here to change that.

Akka is a unified runtime and programming model for scaling both UP (utilizing multi-core processors) and OUT (utilizing the grid/cloud). With Akka 2.0 this will be taken to a whole new level, "Distributed by Design".

marakana

March 21, 2012
Tweet

Transcript

  1. Scaling up & out - with Actors √ Akka Tech

    Lead Email: [email protected] Twitter: @viktorklang Saturday, March 10, 12
  2. 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
  3. 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
  4. 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
  5. CAS is mother, CAS is father. – No locks in

    hot path Saturday, March 10, 12
  6. 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
  7. val counter = system.actorOf(Props[Counter]) Create Actors counter is an ActorRef

    Creates a top-level actor Scala API Saturday, March 10, 12
  8. system stop actorRef Stop actors ...also stops all actors in

    the hierarchy below Saturday, March 10, 12
  9. 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
  10. import akka.patterns.ask val future = ask(actor, message, 5 seconds) Send:

    ? returns a Future[Any] Scala API Saturday, March 10, 12
  11. class SomeActor extends Actor { def receive = { case

    User(name) => // reply to sender sender ! (“Hi ” + name) } } Reply Scala API Saturday, March 10, 12
  12. become context become { // new body case NewMessage =>

    ... } Scala API Saturday, March 10, 12
  13. Routers ‣ RoundRobin ‣ Random ‣ SmallestMailbox ‣ Broadcast ‣

    ScatterGatherFirstCompleted … Custom Saturday, March 10, 12
  14. 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
  15. // 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
  16. A B Bar Foo C B E A D C

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

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

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

    /Foo /Foo/A Name resolution Guardian System Actor Saturday, March 10, 12
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. Actor name is virtual and decoupled from how it is

    deployed Deployment Saturday, March 10, 12
  28. Deployment The same system can be configured as distributed without

    code change (even change at runtime) Saturday, March 10, 12
  29. Deployment Write as local but deploy as distributed in the

    cloud without code change Saturday, March 10, 12
  30. akka { actor { deployment { /my-service { router =

    "round-robin" nr-of-instances = 3 target { nodes = ["wallace:2552", "gromit:2552"] } } } } } Deployment configuration Saturday, March 10, 12