Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Akka - A Brief Intro

1,651 views

Published on

Just a short intro I gave to an internal audience last year.

Published in: Technology
  • Be the first to comment

Akka - A Brief Intro

  1. 1. & the actor model
  2. 2. What is Akka? A toolkit that provides: simple concurrency resiliency elasticity high performance
  3. 3. What is an actor? Receive and send messages Define behavior for next message Create other actors
  4. 4. The Actor model of computation processing - behavior storage - state model I/O - messages
  5. 5. Key things to understand messages processed sequentially multiple actors can be working at the same time actors are not threads an actor can block (but should minimize it) no shared state
  6. 6. A very simple actor class MyActor extends Actor with ActorLogging { def receive = { case msg ⇒ log.info(“Received: {}”, msg) } }
  7. 7. Creating actors in an actor system Everything begins here: val system = ActorSystem("example") val myActor = system.actorOf(Props[MyActor], name = “myActor”) myActor ! “a boring message”
  8. 8. Messages Can be any object Should be immutable Should be serializable Ideally define a protocol for communication
  9. 9. Protocol “a system of rules that explain the correct conduct and procedures to be followed in formal situations” — Merriam-Webster
  10. 10. Message protocols Define the interaction between actors Specify clear boundaries Encode success and failure clearly
  11. 11. Message protocols trait Response case class Success(res: Array[Byte]) extends Response case class Failure(err: String) extends Response
  12. 12. Actor System
  13. 13. Actor System system.actorOf(Props[A], name = “A”)
  14. 14. Actor System context.actorOf(Props[C], name = “C”)
  15. 15. Actor System A is now the supervisor for D
  16. 16. Actor System An exception occurs in D
  17. 17. Actor System A declares a ‘Restart’ for the given exception.
  18. 18. Actor System override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: SomeException ⇒ Restart case _: Exception ⇒ Escalate }
  19. 19. Actor System
  20. 20. Extras ● Supervisors ○ provide exception handling ○ “let it crash” ○ orthogonal to actor behavior ● Location transparency ○ abstracts the idea of “where” an actor lives
  21. 21. Extras ● Mailboxes ● Routing ○ e.g., round-robin, smallest mailbox, etc. ● Dispatching ○ essentially how to allocate thread resources
  22. 22. Thank you!

×