Akka 101
Actor based concurrency framework
What is akka?
Event Driven
Asynchronous
Distributed
Reactive
Responsive: Respond in timely manner if possible
Resilient: Responsive in case of failure
Why Akka?
Actors
Light weight object (around 300 bytes of memory)
Accept messages and do whatever they are supposed to do with them
Process one message at a time
Each actor has messaging queue
Asynchronous
Serializable and passed to other clients/actors
Why actors, why not threads
Concurrency : When two threads are making progress. Parallelism that can
include time slicing.
Parallelism: When two threads are executing simultaneously.
Shared states are real evil. Can cause race condition, dead locks, blocking
calls.
Actors keep mutable state internal.
Actors communicate through messages. No shared state between outside
Message Passing
Fire and forget
Send and wait
Send and reply using future
Akka Components
ActorSystem: Actor, ActorRef, MessageDispatcher, Mailbox, Message Queue.
ActorRef: Addressable destination of actor
MessageDispatcher:
Engine that runs parts of actor system and schedule all activity. Main responsibility to run
mailbox.
ActorRef -> MessageDispatcher -> MessageQueue of destination mailbox
Mailbox: Hold actors. Takes message from message queue and invokes a
actor.
Actor Hierarchies and path
All actors belong to actor hierarchies in actor system.
Two separate hierarchies:
System Hierarchies : Manage internal tasks
User hierarchies : Defines actor for our application
User level root actor (named Guardian) under which all actors are created.
Actor Path: akka://MySystem@MyServer:MyPort/user/TopActor/ChildActor
akka is the name of the scheme
Supervise
Actor can supervise other actors
Supervisor detects and respond to failures
On actor crash, a notification is send to its supervisor to decide what to do
Helps provide clean separation between error handling and message
processing
Fault handling strategy:
Supervision and Monitoring
Every actor has a supervisor who created the current one.
Responsible for watching its subordinates and handling their problem.
Actor detects failure and suspend itself. Supervisor helps by :
Resume its subordinate
Restart the subordinate
Terminate permanently
Escalate the failure to its supervisor
Routers
Type of actor
Layer between ActorRef and Actor
Route Message to underlying actor
Non cluster example:
Round robin
Consistent hashing
Random etc
Use case
Processing pipeline
Streaming data
Multi-user concurrency (as one actor run at one time)
Systems high uptime requirement (Supervisor make sure actor is live)
Application with shared state
Thanks!
References
https://media.itm.uni-luebeck.de/teaching/ws2012/sem-sse/martin-thurau-
akka.io.pdf
http://akka.io/docs/

Akka framework

  • 1.
    Akka 101 Actor basedconcurrency framework
  • 2.
    What is akka? EventDriven Asynchronous Distributed Reactive Responsive: Respond in timely manner if possible Resilient: Responsive in case of failure
  • 3.
  • 4.
    Actors Light weight object(around 300 bytes of memory) Accept messages and do whatever they are supposed to do with them Process one message at a time Each actor has messaging queue Asynchronous Serializable and passed to other clients/actors
  • 5.
    Why actors, whynot threads Concurrency : When two threads are making progress. Parallelism that can include time slicing. Parallelism: When two threads are executing simultaneously. Shared states are real evil. Can cause race condition, dead locks, blocking calls. Actors keep mutable state internal. Actors communicate through messages. No shared state between outside
  • 6.
    Message Passing Fire andforget Send and wait Send and reply using future
  • 7.
    Akka Components ActorSystem: Actor,ActorRef, MessageDispatcher, Mailbox, Message Queue. ActorRef: Addressable destination of actor MessageDispatcher: Engine that runs parts of actor system and schedule all activity. Main responsibility to run mailbox. ActorRef -> MessageDispatcher -> MessageQueue of destination mailbox Mailbox: Hold actors. Takes message from message queue and invokes a actor.
  • 8.
    Actor Hierarchies andpath All actors belong to actor hierarchies in actor system. Two separate hierarchies: System Hierarchies : Manage internal tasks User hierarchies : Defines actor for our application User level root actor (named Guardian) under which all actors are created. Actor Path: akka://MySystem@MyServer:MyPort/user/TopActor/ChildActor akka is the name of the scheme
  • 9.
    Supervise Actor can superviseother actors Supervisor detects and respond to failures On actor crash, a notification is send to its supervisor to decide what to do Helps provide clean separation between error handling and message processing Fault handling strategy:
  • 10.
    Supervision and Monitoring Everyactor has a supervisor who created the current one. Responsible for watching its subordinates and handling their problem. Actor detects failure and suspend itself. Supervisor helps by : Resume its subordinate Restart the subordinate Terminate permanently Escalate the failure to its supervisor
  • 11.
    Routers Type of actor Layerbetween ActorRef and Actor Route Message to underlying actor Non cluster example: Round robin Consistent hashing Random etc
  • 12.
    Use case Processing pipeline Streamingdata Multi-user concurrency (as one actor run at one time) Systems high uptime requirement (Supervisor make sure actor is live) Application with shared state
  • 13.
  • 14.