SlideShare a Scribd company logo
Akka and Actors
Usage patterns at Africa’s Talking
What is akka
“Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient
message-driven applications” - LightBend
The actor model
An actor is a high level concurrency primitive that allows you to model concurrent
computations using entities that interact through message passing.
Why akka
Distributed
High performance
Self healing - let it crash model, fault recovery strategies
Asynchronous
Reactive manifesto - self healing resilient applications
The actor ...
An actor is a container for State, Behavior, a Mailbox, Child Actors and a Supervisor
Strategy.
All of this is encapsulated behind an Actor Reference
The actor model ...
1. Actors have an address so they can send and receive messages
2. These messages are stored in mailboxes (default is a FIFPO queue)
3. Actors can create other actors.
4. Actors have defined behaviour : functions which define the actions to be taken in
reaction to the message at that point in time
5. Actors don’t always map one-on-one to threads, several actors could belong to an
execution context on one thread.
6. An actor should not talk directly to another but interact through an Actor reference
- which points to the actor
The actor model ...
Actor Systems
Creating an Actor
The Actor trait defines only one abstract method, the above mentioned receive, which implements the behavior of the actor
import akka.actor.Actor
import akka.actor.Logging
class MyActor extends Actor {
val log = Logging(context.system, this)
def receive = {
case "test" => log.info("received test")
case _ => log.info("received unknown message")
}
}
The Actor API
1. self reference to the ActorRef of the actor
2. sender reference sender Actor of the last received message, typically used as
described in Reply to messages
3. supervisorStrategy user overridable definition the strategy to use for supervising
child actors
4. context exposes contextual information for the actor and the current message, such
as:
- factory methods to create child actors (actorOf)s
- system that the actor belongs to
Messages and Immutability
Messages can be any kind of object but have to be immutable. Scala can’t enforce
immutability (yet) so this has to be by convention. Primitives like String, Int, Boolean are
always immutable.
Recommended approach is to use Scala case classes which are immutable and work well
with pattern matching at the receiver side
case class Register(user: User)
val message = Register(user)
The actor model … design patterns
ask pattern - future; await or asynchronous
tell pattern - fire/ forget
Tell :Fire-forget
This is the preferred way of sending messages. No blocking waiting for a message. This
gives the best concurrency and scalability characteristics.
The target actor can use the sender function to reply this to reply to the original sender,
by using sender() ! replyMsg
actorRef ! message
Ask: Send-And-Receive-Future
? sends a message asynchronously and returns a Future representing a possible reply.
import akka.pattern.{ ask, pipe }
import system.dispatcher
// The ExecutionContext that will be used
final case class Result(x: Int, s: String, d: Double)
case object Request
implicit val timeout = Timeout(5 seconds) // needed for `?` below
val f: Future[Result] =
actorC ? Request.mapTo[Double]
f pipeTo actorD
The example demonstrates ask together with the pipeTo pattern on futures.
It is completely non-blocking and asynchronous: ask produces a Future then
pipeTo installs an onComplete-handler on the future to affect the submission of the
aggregated Result to another actor.
We reply with:
Sender ! replyMsg()
Note: There are performance implications of using ask since something needs to keep
track of when it times out and there needs to be something that bridges a Promise into
an ActorRef. Always prefer tell for performance, and only ask if you must
Futures
A Future is a data structure used to retrieve the result of some concurrent operation. This result can be accessed
synchronously (blocking) or asynchronously (non-blocking).
Using an Actor‘s ? method to send a message will return a Future. When using non-blocking it is better to use the
mapTo method to safely try to cast a Future to an expected type. On Complete callback allows you to define
behaviour when the future completes:
import scala.concurrent.Future
import akka.pattern.ask
val future: Future[String] = ask(actor, msg).mapTo[String]
future onComplete {
case Success => //do something
case Failure => //do something
Become/Unbecome
Akka supports hotswapping the Actor’s implementation at runtime. Invoke the become
method to implement the new message handler:
class HotSwapActor extends Actor {
import context._
def angry: Receive = {
case "foo" => sender() ! "I am already angry?"
case "bar" => become(happy)
}
def happy: Receive = {
case "bar" => sender() ! "I am already happy :-)"
case "foo" => become(angry)
}
def receive = {
case "foo" => become(angry)
case "bar" => become(happy)
}
Fault Handling
Each actor is the supervisor of its children, and as such each actor defines fault handling supervisor strategy.
Depending on the nature of the work to be supervised and the nature of the failure, the supervisor can resume,
restart, stop the actor or escalate the failure.
You can create your own or use a combined strategy as shown below:
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import scala.concurrent.duration._
override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
case _: ArithmeticException => Resume
case t =>
super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate)
}
Actors at AT
Use of ask with futures(with handlers for success/failure) and pattern matching
Ask with mapTo/pipeTo very useful for object transformations where the upstream
expects a response in a certain format
Routing to load balance particularly busy actors
FSMs
Delayed restarts with Backoff Supervisor
Querying and Persisting
Test cases
Actors at AT...cont
Very Reliable and Scales Well
If extensively tested few outages
Bugs are usually the result of high efficiency
All the advantages of the JVM with the benefits of functional programming mixed in
Thanks for coming!

More Related Content

What's hot (20)

PPTX
Operators in java
Madishetty Prathibha
 
PPT
Ap Power Point Chpt3 B
dplunkett
 
DOCX
Ruby Interview Questions
Sumanth krishna
 
PDF
Akka - A Brief Intro
Thomas Lockney
 
PPTX
07 flow control
dhrubo kayal
 
PPTX
Presenter deck icenium hol
Dhananjay Kumar
 
PPT
Ap Power Point Chpt8
dplunkett
 
PPTX
Chapter 4.2
sotlsoc
 
ODP
Why you should use a testing framework
Richie Cotton
 
PDF
Operators in java
Ravi_Kant_Sahu
 
PPTX
Intro To C++ - Class 10 - Control Statements: Part 2
Blue Elephant Consulting
 
PPTX
Services Factory Provider Value Constant - AngularJS
Sumanth krishna
 
PDF
: Look up the definition of the standard Comparable interface in the API docu...
hwbloom116
 
PPTX
Intro To C++ - Class 12 - For, do … While
Blue Elephant Consulting
 
PPT
Md04 flow control
Rakesh Madugula
 
PPT
Java findamentals2
Todor Kolev
 
PPTX
Java chapter 3
Abdii Rashid
 
PPTX
Java chapter 6
Abdii Rashid
 
PDF
Effective Unit Test Style Guide
Jacky Lai
 
PPTX
Looping statements
Jaya Kumari
 
Operators in java
Madishetty Prathibha
 
Ap Power Point Chpt3 B
dplunkett
 
Ruby Interview Questions
Sumanth krishna
 
Akka - A Brief Intro
Thomas Lockney
 
07 flow control
dhrubo kayal
 
Presenter deck icenium hol
Dhananjay Kumar
 
Ap Power Point Chpt8
dplunkett
 
Chapter 4.2
sotlsoc
 
Why you should use a testing framework
Richie Cotton
 
Operators in java
Ravi_Kant_Sahu
 
Intro To C++ - Class 10 - Control Statements: Part 2
Blue Elephant Consulting
 
Services Factory Provider Value Constant - AngularJS
Sumanth krishna
 
: Look up the definition of the standard Comparable interface in the API docu...
hwbloom116
 
Intro To C++ - Class 12 - For, do … While
Blue Elephant Consulting
 
Md04 flow control
Rakesh Madugula
 
Java findamentals2
Todor Kolev
 
Java chapter 3
Abdii Rashid
 
Java chapter 6
Abdii Rashid
 
Effective Unit Test Style Guide
Jacky Lai
 
Looping statements
Jaya Kumari
 

Similar to Nairobi JVM meetup : Introduction to akka (20)

PDF
Introducing Akka
Meetu Maltiar
 
PDF
Akka and futures
Knoldus Inc.
 
PPTX
Akka Actors
Dylan Forciea
 
PDF
Actor Model Akka Framework
Harinath Krishnamoorthy
 
PDF
Building Massively Scalable application with Akka 2.0
Knoldus Inc.
 
PPTX
Introduction to Akka - Atlanta Java Users Group
Roy Russo
 
PDF
Akka Actors: an Introduction
Roberto Casadei
 
KEY
Akka london scala_user_group
Skills Matter
 
PDF
Akka (1)
Rahul Shukla
 
PDF
Introduction to concurrent programming with akka actors
datamantra
 
PDF
Introduction to concurrent programming with Akka actors
Shashank L
 
PDF
Dive into Akka Actors
Knoldus Inc.
 
PDF
Akka-intro-training-public.pdf
BernardDeffarges
 
PDF
Introduction to Akka
Knoldus Inc.
 
PDF
Akka lsug skills matter
Skills Matter
 
PDF
Scaling Web Apps with Akka
Maciej Matyjas
 
PDF
The dark side of Akka and the remedy
krivachy
 
PDF
Akka (BeJUG)
Sander Mak (@Sander_Mak)
 
PDF
Message-based communication patterns in distributed Akka applications
Andrii Lashchenko
 
PDF
Effective Akka v2
shinolajla
 
Introducing Akka
Meetu Maltiar
 
Akka and futures
Knoldus Inc.
 
Akka Actors
Dylan Forciea
 
Actor Model Akka Framework
Harinath Krishnamoorthy
 
Building Massively Scalable application with Akka 2.0
Knoldus Inc.
 
Introduction to Akka - Atlanta Java Users Group
Roy Russo
 
Akka Actors: an Introduction
Roberto Casadei
 
Akka london scala_user_group
Skills Matter
 
Akka (1)
Rahul Shukla
 
Introduction to concurrent programming with akka actors
datamantra
 
Introduction to concurrent programming with Akka actors
Shashank L
 
Dive into Akka Actors
Knoldus Inc.
 
Akka-intro-training-public.pdf
BernardDeffarges
 
Introduction to Akka
Knoldus Inc.
 
Akka lsug skills matter
Skills Matter
 
Scaling Web Apps with Akka
Maciej Matyjas
 
The dark side of Akka and the remedy
krivachy
 
Message-based communication patterns in distributed Akka applications
Andrii Lashchenko
 
Effective Akka v2
shinolajla
 
Ad

Recently uploaded (20)

PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Digital water marking system project report
Kamal Acharya
 
PPTX
Distribution reservoir and service storage pptx
dhanashree78
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PPTX
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Design Thinking basics for Engineers.pdf
CMR University
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Digital water marking system project report
Kamal Acharya
 
Distribution reservoir and service storage pptx
dhanashree78
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
澳洲电子毕业证澳大利亚圣母大学水印成绩单UNDA学生证网上可查学历
Taqyea
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Ad

Nairobi JVM meetup : Introduction to akka

  • 1. Akka and Actors Usage patterns at Africa’s Talking
  • 2. What is akka “Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications” - LightBend
  • 3. The actor model An actor is a high level concurrency primitive that allows you to model concurrent computations using entities that interact through message passing.
  • 4. Why akka Distributed High performance Self healing - let it crash model, fault recovery strategies Asynchronous Reactive manifesto - self healing resilient applications
  • 5. The actor ... An actor is a container for State, Behavior, a Mailbox, Child Actors and a Supervisor Strategy. All of this is encapsulated behind an Actor Reference
  • 6. The actor model ... 1. Actors have an address so they can send and receive messages 2. These messages are stored in mailboxes (default is a FIFPO queue) 3. Actors can create other actors. 4. Actors have defined behaviour : functions which define the actions to be taken in reaction to the message at that point in time 5. Actors don’t always map one-on-one to threads, several actors could belong to an execution context on one thread. 6. An actor should not talk directly to another but interact through an Actor reference - which points to the actor
  • 9. Creating an Actor The Actor trait defines only one abstract method, the above mentioned receive, which implements the behavior of the actor import akka.actor.Actor import akka.actor.Logging class MyActor extends Actor { val log = Logging(context.system, this) def receive = { case "test" => log.info("received test") case _ => log.info("received unknown message") } }
  • 10. The Actor API 1. self reference to the ActorRef of the actor 2. sender reference sender Actor of the last received message, typically used as described in Reply to messages 3. supervisorStrategy user overridable definition the strategy to use for supervising child actors 4. context exposes contextual information for the actor and the current message, such as: - factory methods to create child actors (actorOf)s - system that the actor belongs to
  • 11. Messages and Immutability Messages can be any kind of object but have to be immutable. Scala can’t enforce immutability (yet) so this has to be by convention. Primitives like String, Int, Boolean are always immutable. Recommended approach is to use Scala case classes which are immutable and work well with pattern matching at the receiver side case class Register(user: User) val message = Register(user)
  • 12. The actor model … design patterns ask pattern - future; await or asynchronous tell pattern - fire/ forget
  • 13. Tell :Fire-forget This is the preferred way of sending messages. No blocking waiting for a message. This gives the best concurrency and scalability characteristics. The target actor can use the sender function to reply this to reply to the original sender, by using sender() ! replyMsg actorRef ! message
  • 14. Ask: Send-And-Receive-Future ? sends a message asynchronously and returns a Future representing a possible reply. import akka.pattern.{ ask, pipe } import system.dispatcher // The ExecutionContext that will be used final case class Result(x: Int, s: String, d: Double) case object Request implicit val timeout = Timeout(5 seconds) // needed for `?` below val f: Future[Result] = actorC ? Request.mapTo[Double] f pipeTo actorD
  • 15. The example demonstrates ask together with the pipeTo pattern on futures. It is completely non-blocking and asynchronous: ask produces a Future then pipeTo installs an onComplete-handler on the future to affect the submission of the aggregated Result to another actor. We reply with: Sender ! replyMsg() Note: There are performance implications of using ask since something needs to keep track of when it times out and there needs to be something that bridges a Promise into an ActorRef. Always prefer tell for performance, and only ask if you must
  • 16. Futures A Future is a data structure used to retrieve the result of some concurrent operation. This result can be accessed synchronously (blocking) or asynchronously (non-blocking). Using an Actor‘s ? method to send a message will return a Future. When using non-blocking it is better to use the mapTo method to safely try to cast a Future to an expected type. On Complete callback allows you to define behaviour when the future completes: import scala.concurrent.Future import akka.pattern.ask val future: Future[String] = ask(actor, msg).mapTo[String] future onComplete { case Success => //do something case Failure => //do something
  • 17. Become/Unbecome Akka supports hotswapping the Actor’s implementation at runtime. Invoke the become method to implement the new message handler: class HotSwapActor extends Actor { import context._ def angry: Receive = { case "foo" => sender() ! "I am already angry?" case "bar" => become(happy) } def happy: Receive = { case "bar" => sender() ! "I am already happy :-)" case "foo" => become(angry) } def receive = { case "foo" => become(angry) case "bar" => become(happy) }
  • 18. Fault Handling Each actor is the supervisor of its children, and as such each actor defines fault handling supervisor strategy. Depending on the nature of the work to be supervised and the nature of the failure, the supervisor can resume, restart, stop the actor or escalate the failure. You can create your own or use a combined strategy as shown below: import akka.actor.OneForOneStrategy import akka.actor.SupervisorStrategy._ import scala.concurrent.duration._ override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case t => super.supervisorStrategy.decider.applyOrElse(t, (_: Any) => Escalate) }
  • 19. Actors at AT Use of ask with futures(with handlers for success/failure) and pattern matching Ask with mapTo/pipeTo very useful for object transformations where the upstream expects a response in a certain format Routing to load balance particularly busy actors FSMs Delayed restarts with Backoff Supervisor Querying and Persisting Test cases
  • 20. Actors at AT...cont Very Reliable and Scales Well If extensively tested few outages Bugs are usually the result of high efficiency All the advantages of the JVM with the benefits of functional programming mixed in

Editor's Notes

  • #19: Escalate is used if the defined strategy doesn’t cover the exception that was thrown. When the supervisor strategy is not defined for an actor the following exceptions are handled by default: • ActorInitializationException will stop the failing child actor • ActorKilledException will stop the failing child actor • DeathPactException will stop the failing child actor • Exception will restart the failing child actor • Other types of Throwable will be escalated to parent actor If the exception escalate all the way up to the root guardian it will handle it in the same way as the default strategy defined above.
  • #20: mplements the so-called exponen- tial backoff supervision strategy, starting a child actor again when it fails, each time with a growing time delay between restarts. This pattern is useful when the started actor fails 1 because some external resource is not available,