SlideShare a Scribd company logo
CONCURRENCY IN SCALA
    THE AKKA WAY
         Yardena Meymann
   Sr. Software Architect at VMware
           February 2013
SCALABILITY
• The amount of work grows – BIG.*
SCALABILITY
• Scale up and out without breaking down
SCALA
• Programming language for JVM
• Functional and Object-Oriented
• Type-safe
• Powerful
• Concise
• Extensible
• Interoperable with Java
AKKA
• Akka is a toolkit and runtime for building
  highly concurrent, distributed, and fault
  tolerant event-driven applications on the JVM
• Provides unified programming model for
  concurrency, distribution and fault-tolerance
ACTORS
Actor is a fundamental unit of computation
(Carl Hewitt 1973)
•   Local state, isolated from the world
•   Sends messages to other actors
•   Reacts to received messages
•   Actors usually come in systems
ACTORS
• Elastic – grow and shrink on demand
• Lightweight (2.7 M per GB RAM)
• Hot deploy – change behavior at runtime
• Local or distributed
EXAMPLE - SCOTCHDOG
• Inspired by Monit
  http://mmonit.com/monit/
• Cross-platform,
  JVM-based (Scala+Akka)
• Configured with list of services
• Starts and stops services
• Ensures that monitored services are up:
  checks periodically and starts if necessary
HOW IS THIS GOING TO WORK
• Actor per service
• Actor for the watchdog
ACTOR HIERARCHY




                         Watchdog




             Service 1    Service 2   Service 3
AKKA ACTOR HIERARCHY

                root


      system                  user


                           Watchdog
      system
      actors


               Service 1    Service 2   Service 3
WHY THE HIERARCHY IS IMPORTANT
• Fault-tolerance
• “Let it crash”
• Supervisor can resume, restart, terminate
  the subordinate; it can also escalate the
  failure
• One-for-one and one-for-all strategies
LET’S START WITH MESSAGES
•    Service commands
    • start
    • stop
    • is started?
    • monitor/unmonitor
•    Watchdog commands
    • start
    • add service
    • do *** to a specified service
    • …
SERVICE COMMANDS
sealed trait ServiceCommand
case object Start       extends ServiceCommand
case object Stop        extends ServiceCommand
case object IsStarted   extends ServiceCommand
…
• Commands need to be immutable objects
• Case classes and objects make excellent messages
WATCHDOG COMMANDS
case object StartWatching
case class AddService(config: ServiceConfig)
case class PerServiceCommand(
                  service: String,
                  command: ServiceCommand)
...
SENDING MESSAGES
• tell (a.k.a bang, or ! operator) sends a
  message asynchronously and return
  immediately
     service ! Start
• ask (a.k.a ? operator) sends a message
  asynchronously and returns a Future
  representing a possible reply
     service ? IsStarted
HANDLING MESSAGES
• Define actor class
class Service(config: ServiceConfig) extends Actor …
• Actor gives us
  •   ability to create other actors
  •   access to self
  •   access to sender of last message
  •   lifecycle hooks
HANDLING MESSAGES
Define receive method within Actor class
def receive = {
  case Start => //perform start action
  case Stop => //perform stop action
  case IsStarted => {
     //call isStarted action and capture the result
    sender ! result //reply - return the result to sender
  }
}
Heavily uses Scala’s pattern matching
CREATING ACTORS
•   class Watchdog extends Actor …
•   val system = ActorSystem(“Scotchdog”)
•   val watchdog = system.actorOf(
    Props[Watchdog], name = “watchdog”)
•   watchdog is an ActorRef
•   we used default constructor
•   Props allows customizations of actor
    behavior, such as the dispatcher (execution
    strategy). We used the default one
HANDLING MESSAGES - WATCHDOG
def receive = {
 case AddService(config) => {
     val service = context.actorOf(Props(new Service(config)),
                                 config.name)
     service ! Monitor
 }
 case PerServiceCommand(service, command) =>
     context.actorFor(service) forward command
 …
ACTOR REFERENCES
• Top level actors are created with
  system, child actors are created with parent
  actor’s context
• Existing actors can be found with
  context/system actorFor
• ActorRef can be sent to another actor
FUTURES
val future = watchdog ?
        PerServiceCommand(“myserver", IsStarted)
There are several ways to obtain the value:
• val result = Await.result(future, 5 seconds).
                           asInstanceOf[Boolean]
• future onComplete {
        case Success(value: Boolean) => …
        case Failure(_) => …
    }
MORE ADVANCED EXAMPLE
• We want the watchdog to return a combined
  status of all services – “up” if all services are
  up, otherwise “down”
  •   We will define:
      val services =
              new scala.collection.mutable.HashSet[String]
      within Watchdog class
  •   It is safe to have mutable state within an actor!
  •   When adding services: services += config.name
MORE ADVANCED EXAMPLE
def receive = {
   case Status => {
        val responses =
                  for (child <- services;
                      serviceActor = context.actorFor(child));
                      future = serviceActor ? IsStarted)
                  yield future.asInstanceOf[Future[Boolean]]
        sender ! Future.reduce(responses)(_ && _)
    }
   …
SCHEDULER
• We want to check periodically if a service is
  up, and report the status at any time
• For this purpose we can use Akka’s built-in
  scheduler
• …
SCHEDULER EXAMPLE
class ServiceMonitor(config: ServiceConfig) extends Actor with ActorLogging {
 var status = false
 var polling: Option[Cancellable] = None
 def receive = {
  case IsStarted => sender ! status
  case Monitor =>
   polling = Some(scheduler.schedule(Duration.Zero, config.interval, self, Tick))
  case Tick =>
   sender ? config.isStarted onSuccess { case res: Boolean => status = res }
  case UnMonitor => polling.foreach(_.cancel)
  …
FURTHER IMPROVEMENTS
• In practice, the service actor is more
  complex: for example, we don’t want to start
  a service again if it is already started or is
  starting up now
• To address this we use Akka’s Finite State
  Machine actors, a.k.a FSM
• This is a topic for a longer talk, so for now…
CONCLUSIONS
•   Akka makes concurrency (and
    distribution, which we didn’t cover) easier
•   Programming with actors require a paradigm
    shift, may be challenging at first, but worth it
•   There is more to Akka then we’ve covered
•   If you like Akka, but want to program in
    Java, you can use Java API
•   Akka integrates with tools like Spring, Apache
    Camel and ZeroMQ
RESOURCES
•   http://www.scala-lang.org
•   http://akka.io
•   http://typesafe.com
•   http://letitcrash.com
•   http://www.manning.com/roestenburg/
•   http://www.cakesolutions.net/teamblogs/2012/07/03/
    akka-patterns/
•   Coming soon https://github.com/yardena/scotchdog
THANK YOU

More Related Content

ZIP
Above the clouds: introducing Akka
KEY
The Why and How of Scala at Twitter
PPTX
Akka Actor presentation
PPTX
Developing distributed applications with Akka and Akka Cluster
PDF
Akka Cluster in Java - JCConf 2015
PDF
Actor Model Akka Framework
PPTX
Introduction to Akka - Atlanta Java Users Group
PDF
System Integration with Akka and Apache Camel
Above the clouds: introducing Akka
The Why and How of Scala at Twitter
Akka Actor presentation
Developing distributed applications with Akka and Akka Cluster
Akka Cluster in Java - JCConf 2015
Actor Model Akka Framework
Introduction to Akka - Atlanta Java Users Group
System Integration with Akka and Apache Camel

What's hot (20)

PDF
Advanced akka features
PDF
The dark side of Akka and the remedy
PDF
Short intro to scala and the play framework
PDF
Reactive Web-Applications @ LambdaDays
PDF
Building reactive distributed systems with Akka
PDF
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
ODP
Introduction to Apache Kafka- Part 2
PPTX
The dark side of Akka and the remedy - bp.scala meetup
PDF
Introducing Akka
PPTX
JVM languages "flame wars"
PPTX
Developing distributed applications with Akka and Akka Cluster
PDF
Introduction of failsafe
PPTX
What’s expected in Java 9
PDF
Akka lsug skills matter
PPTX
Multi-threading in the modern era: Vertx Akka and Quasar
PDF
React Development with the MERN Stack
PDF
Building a chatbot – step by step
PPTX
Stream processing from single node to a cluster
PDF
Scala @ TechMeetup Edinburgh
PDF
Introduction to Asynchronous scala
Advanced akka features
The dark side of Akka and the remedy
Short intro to scala and the play framework
Reactive Web-Applications @ LambdaDays
Building reactive distributed systems with Akka
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
Introduction to Apache Kafka- Part 2
The dark side of Akka and the remedy - bp.scala meetup
Introducing Akka
JVM languages "flame wars"
Developing distributed applications with Akka and Akka Cluster
Introduction of failsafe
What’s expected in Java 9
Akka lsug skills matter
Multi-threading in the modern era: Vertx Akka and Quasar
React Development with the MERN Stack
Building a chatbot – step by step
Stream processing from single node to a cluster
Scala @ TechMeetup Edinburgh
Introduction to Asynchronous scala
Ad

Viewers also liked (9)

PDF
Concurrency and scalability with akka
ODP
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
PDF
Scala 2013 review
PDF
Scala for Java programmers
PDF
Akka -- Scalability in Scala and Java
PDF
Python in 90 minutes
PDF
Comparing JVM Web Frameworks - Devoxx France 2013
PDF
Play Framework: async I/O with Java and Scala
PDF
Event-sourced architectures with Akka
Concurrency and scalability with akka
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala 2013 review
Scala for Java programmers
Akka -- Scalability in Scala and Java
Python in 90 minutes
Comparing JVM Web Frameworks - Devoxx France 2013
Play Framework: async I/O with Java and Scala
Event-sourced architectures with Akka
Ad

Similar to Concurrency in Scala - the Akka way (20)

PPTX
Scale up your thinking
KEY
Akka london scala_user_group
PDF
Introduction to Actor Model and Akka
PPTX
Scalamen and OT
PDF
Workflow as code with Azure Durable Functions
PDF
BPM-3 Advanced Workflow Deep Dive
KEY
Introduction to Actor Model and Akka
PPTX
Reactive Streams - László van den Hoek
PPTX
Azure Function Workflow
PPTX
Process Orchestration with Flowable and Spring Boot
PPTX
[NDC 2019] Enterprise-Grade Serverless
PPTX
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
PDF
Backday Xebia : Akka, the reactive toolkit
PDF
Reactive Microservices with JRuby and Docker
PDF
Introduction to Akka
PDF
Web Services Automated Testing via SoapUI Tool
PDF
Writing Asynchronous Programs with Scala & Akka
PDF
Akka (1)
PPTX
Building a document e-signing workflow with Azure Durable Functions
PDF
A Jouney Through Wonderland - Jimdo
Scale up your thinking
Akka london scala_user_group
Introduction to Actor Model and Akka
Scalamen and OT
Workflow as code with Azure Durable Functions
BPM-3 Advanced Workflow Deep Dive
Introduction to Actor Model and Akka
Reactive Streams - László van den Hoek
Azure Function Workflow
Process Orchestration with Flowable and Spring Boot
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
Backday Xebia : Akka, the reactive toolkit
Reactive Microservices with JRuby and Docker
Introduction to Akka
Web Services Automated Testing via SoapUI Tool
Writing Asynchronous Programs with Scala & Akka
Akka (1)
Building a document e-signing workflow with Azure Durable Functions
A Jouney Through Wonderland - Jimdo

Concurrency in Scala - the Akka way

  • 1. CONCURRENCY IN SCALA THE AKKA WAY Yardena Meymann Sr. Software Architect at VMware February 2013
  • 2. SCALABILITY • The amount of work grows – BIG.*
  • 3. SCALABILITY • Scale up and out without breaking down
  • 4. SCALA • Programming language for JVM • Functional and Object-Oriented • Type-safe • Powerful • Concise • Extensible • Interoperable with Java
  • 5. AKKA • Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM • Provides unified programming model for concurrency, distribution and fault-tolerance
  • 6. ACTORS Actor is a fundamental unit of computation (Carl Hewitt 1973) • Local state, isolated from the world • Sends messages to other actors • Reacts to received messages • Actors usually come in systems
  • 7. ACTORS • Elastic – grow and shrink on demand • Lightweight (2.7 M per GB RAM) • Hot deploy – change behavior at runtime • Local or distributed
  • 8. EXAMPLE - SCOTCHDOG • Inspired by Monit http://mmonit.com/monit/ • Cross-platform, JVM-based (Scala+Akka) • Configured with list of services • Starts and stops services • Ensures that monitored services are up: checks periodically and starts if necessary
  • 9. HOW IS THIS GOING TO WORK • Actor per service • Actor for the watchdog
  • 10. ACTOR HIERARCHY Watchdog Service 1 Service 2 Service 3
  • 11. AKKA ACTOR HIERARCHY root system user Watchdog system actors Service 1 Service 2 Service 3
  • 12. WHY THE HIERARCHY IS IMPORTANT • Fault-tolerance • “Let it crash” • Supervisor can resume, restart, terminate the subordinate; it can also escalate the failure • One-for-one and one-for-all strategies
  • 13. LET’S START WITH MESSAGES • Service commands • start • stop • is started? • monitor/unmonitor • Watchdog commands • start • add service • do *** to a specified service • …
  • 14. SERVICE COMMANDS sealed trait ServiceCommand case object Start extends ServiceCommand case object Stop extends ServiceCommand case object IsStarted extends ServiceCommand … • Commands need to be immutable objects • Case classes and objects make excellent messages
  • 15. WATCHDOG COMMANDS case object StartWatching case class AddService(config: ServiceConfig) case class PerServiceCommand( service: String, command: ServiceCommand) ...
  • 16. SENDING MESSAGES • tell (a.k.a bang, or ! operator) sends a message asynchronously and return immediately service ! Start • ask (a.k.a ? operator) sends a message asynchronously and returns a Future representing a possible reply service ? IsStarted
  • 17. HANDLING MESSAGES • Define actor class class Service(config: ServiceConfig) extends Actor … • Actor gives us • ability to create other actors • access to self • access to sender of last message • lifecycle hooks
  • 18. HANDLING MESSAGES Define receive method within Actor class def receive = { case Start => //perform start action case Stop => //perform stop action case IsStarted => { //call isStarted action and capture the result sender ! result //reply - return the result to sender } } Heavily uses Scala’s pattern matching
  • 19. CREATING ACTORS • class Watchdog extends Actor … • val system = ActorSystem(“Scotchdog”) • val watchdog = system.actorOf( Props[Watchdog], name = “watchdog”) • watchdog is an ActorRef • we used default constructor • Props allows customizations of actor behavior, such as the dispatcher (execution strategy). We used the default one
  • 20. HANDLING MESSAGES - WATCHDOG def receive = { case AddService(config) => { val service = context.actorOf(Props(new Service(config)), config.name) service ! Monitor } case PerServiceCommand(service, command) => context.actorFor(service) forward command …
  • 21. ACTOR REFERENCES • Top level actors are created with system, child actors are created with parent actor’s context • Existing actors can be found with context/system actorFor • ActorRef can be sent to another actor
  • 22. FUTURES val future = watchdog ? PerServiceCommand(“myserver", IsStarted) There are several ways to obtain the value: • val result = Await.result(future, 5 seconds). asInstanceOf[Boolean] • future onComplete { case Success(value: Boolean) => … case Failure(_) => … }
  • 23. MORE ADVANCED EXAMPLE • We want the watchdog to return a combined status of all services – “up” if all services are up, otherwise “down” • We will define: val services = new scala.collection.mutable.HashSet[String] within Watchdog class • It is safe to have mutable state within an actor! • When adding services: services += config.name
  • 24. MORE ADVANCED EXAMPLE def receive = { case Status => { val responses = for (child <- services; serviceActor = context.actorFor(child)); future = serviceActor ? IsStarted) yield future.asInstanceOf[Future[Boolean]] sender ! Future.reduce(responses)(_ && _) } …
  • 25. SCHEDULER • We want to check periodically if a service is up, and report the status at any time • For this purpose we can use Akka’s built-in scheduler • …
  • 26. SCHEDULER EXAMPLE class ServiceMonitor(config: ServiceConfig) extends Actor with ActorLogging { var status = false var polling: Option[Cancellable] = None def receive = { case IsStarted => sender ! status case Monitor => polling = Some(scheduler.schedule(Duration.Zero, config.interval, self, Tick)) case Tick => sender ? config.isStarted onSuccess { case res: Boolean => status = res } case UnMonitor => polling.foreach(_.cancel) …
  • 27. FURTHER IMPROVEMENTS • In practice, the service actor is more complex: for example, we don’t want to start a service again if it is already started or is starting up now • To address this we use Akka’s Finite State Machine actors, a.k.a FSM • This is a topic for a longer talk, so for now…
  • 28. CONCLUSIONS • Akka makes concurrency (and distribution, which we didn’t cover) easier • Programming with actors require a paradigm shift, may be challenging at first, but worth it • There is more to Akka then we’ve covered • If you like Akka, but want to program in Java, you can use Java API • Akka integrates with tools like Spring, Apache Camel and ZeroMQ
  • 29. RESOURCES • http://www.scala-lang.org • http://akka.io • http://typesafe.com • http://letitcrash.com • http://www.manning.com/roestenburg/ • http://www.cakesolutions.net/teamblogs/2012/07/03/ akka-patterns/ • Coming soon https://github.com/yardena/scotchdog