Akka framework

Akka 101 Actor based concurrency framework

Transcript of Akka framework

Page 1: Akka framework

Akka 101Actor based concurrency framework

Page 2: Akka 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

Page 3: Akka framework

Why Akka?

Page 4: Akka framework


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


Serializable and passed to other clients/actors

Page 5: Akka framework

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.

Page 6: Akka framework

Message Passing

Fire and forget

Send and wait

Send and reply using future

Page 7: Akka framework

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.

Page 8: Akka framework

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

Page 9: Akka framework


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)

Page 10: Akka framework

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.

Page 11: Akka framework

RoutersType of actor

Layer between ActorRef and Actor

Route Message to underlying actor

Non cluster example:Round robin

Consistent hashing

Random etc

Page 12: Akka framework

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

Page 13: Akka framework


Page 14: Akka framework