Akka framework
-
Upload
miteshsharma -
Category
Technology
-
view
282 -
download
3
Transcript of Akka framework
Akka 101Actor based concurrency framework
What is akka?
Event DrivenAsynchronousDistributedReactive
Responsive: Respond in timely manner if possible
Resilient: Responsive in case of failure
Elastic: Easy to scale
Message driven
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 word and actor.
So we love them.
Message Passing
Fire and forget
Send and wait
Send and reply using future
Akka ComponentsActorSystem: 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.
Message Queue: Keep all messages, deliver it to mailbox.
Actor: Does acton on received message.
Actor Hierarchies and pathAll 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
MySystem is the name of the ActorSystem
MyServer:MyPort is the server and port
user is the name of the guardian
TopActor is the name given to a top-level actor
ChildActor name of child actor
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:
One for one restart strategy (Only child failed, need to restart)
All for one restart strategy (all child restart on any child failure)
Supervision and MonitoringEvery 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
No way to put message back, so if actor signal failure, current message might get lost.
DeathWatch: Notified when actor dies, can be used to get notified about actor stopped.
RoutersType of actor
Layer between ActorRef and Actor
Route Message to underlying actor
Non cluster example:Round robin
Consistent hashing
Random etc
Use caseProcessing 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/