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

Recommended for you

Reactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDaysReactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDays

Over the past few years, web-applications have started to play an increasingly important role in our lives. We expect them to be always available and the data to be always fresh. This shift into the realm of real-time data processing is now transitioning to physical devices, and Gartner predicts that the Internet of Things will grow to an installed base of 26 billion units by 2020. Reactive web-applications are an answer to the new requirements of high-availability and resource efficiency brought by this rapid evolution. On the JVM, a set of new languages and tools has emerged that enable the development of entirely asynchronous request and data handling pipelines. At the same time, container-less application frameworks are gaining increasing popularity over traditional deployment mechanisms. This talk is going to give you an introduction into one of the most trending reactive web-application stack on the JVM, involving the Scala programming language, the concurrency toolkit Akka and the web-application framework Play. It will show you how functional programming techniques enable asynchronous programming, and how those technologies help to build robust and resilient web-applications.

playframeworkreactiveakka
Building reactive distributed systems with Akka
Building reactive distributed systems with Akka Building reactive distributed systems with Akka
Building reactive distributed systems with Akka

Johan Andrén presented on building reactive distributed systems with Akka. He provided an overview of Akka clustering and distributed data types. He discussed how Akka actors map to distributed systems and Akka's support for eventual consistency through gossip. He also covered distributed pub/sub messaging, cluster singletons that introduce a single point of failure, and cluster sharding to shard actors across nodes.

reactiveclusterdistributed systems
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
2014-02-20 | Akka Concurrency (Vienna Scala User Group)2014-02-20 | Akka Concurrency (Vienna Scala User Group)
2014-02-20 | Akka Concurrency (Vienna Scala User Group)

The document summarizes a presentation about the book "Akka Concurrency" by Derek Wyatt. It provides an overview of the book's content including chapters on actors, testing, routing messages, dataflow concurrency and examples of stateful actors and using the finite state machine pattern. It also notes that while the book provides an exhaustive overview of Akka, it is based on an older version of Akka and does not cover newer features like clustering and persistence.

fsmconcurrencyjvm
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

Recommended for you

Introduction to Apache Kafka- Part 2
Introduction to Apache Kafka- Part 2Introduction to Apache Kafka- Part 2
Introduction to Apache Kafka- Part 2

The document discusses Apache Kafka and provides an introduction to key concepts like consumer groups, producer/consumer APIs, and throughput guarantees. It explains how Kafka can be used for both queuing and publish-subscribe models using consumer groups. Code examples are given for Kafka producers and consumers in Scala and Java along with throughput benchmarks showing Kafka's ability to support millions of messages per second.

kafkaknolxknowledge sharing
The dark side of Akka and the remedy - bp.scala meetup
The dark side of Akka and the remedy - bp.scala meetupThe dark side of Akka and the remedy - bp.scala meetup
The dark side of Akka and the remedy - bp.scala meetup

The document discusses the problems with using Akka actors and proposes solutions using FSM and futures instead of actors for some cases. It describes issues with actors like lack of type safety, mutable state leading to complex code, and difficult debugging. FSMs are presented as a better solution by enforcing state transitions and logging. Futures are also suggested for cases without need for location transparency or real-time data flow that actors provide. The conclusion is that Akka requires a new mindset and can cause headaches if overused, so it's best to only use actors when truly needed.

typesafetyakkafsm
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka

The document introduces Akka, an open-source toolkit for building distributed, concurrent applications on the JVM. It provides a programming model called the actor model that makes it easier to build scalable and fault-tolerant systems. Actors process messages asynchronously and avoid shared state, providing a simpler approach to concurrency than traditional threads and locks. Akka allows actors to be distributed across a network, enabling applications to scale out elastically.

concurrencyakkajava
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

Recommended for you

JVM languages "flame wars"
JVM languages "flame wars"JVM languages "flame wars"
JVM languages "flame wars"

There are many programming languages that can be used on the JVM (Java, Scala, Groovy, Kotlin, ...). In this session, we'll outline the main differences between them and discuss advantages and disadvantages for each.

javagroovyscala
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka Cluster

This document provides an overview of Akka and Akka Cluster, frameworks for building distributed and fault-tolerant applications using actors. It discusses concurrency paradigms like message passing actors, Akka's implementation of the actor model, clustering with Akka Cluster, and using CRDTs for distributed shared state. It also previews demos of building a web crawler application and clustering it across nodes with Akka Cluster.

Introduction of failsafe
Introduction of failsafeIntroduction of failsafe
Introduction of failsafe

This document introduces Failsafe, which provides latency and fault tolerance capabilities for distributed systems. It discusses how Failsafe compares to Hystrix and how it is used at Coupang. Key features of Failsafe include retry policies, circuit breakers, fallbacks, and asynchronous execution. Event listeners and policy composition patterns are also covered.

failsafecircuit breakerretry
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

Recommended for you

What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9

Java 9 is just around the corner. In this session, we'll describe the new modularization support (Jigsaw), new JDK tools, enhanced APIs and many performance improvements that were added to the new version.

java 9java
Akka lsug skills matter
Akka lsug skills matterAkka lsug skills matter
Akka lsug skills matter

During the talk, we will build a simple web app using Lift and then introduce Akka ( http://akkasource.org) to help scale it. Specifically, we will demonstrate Remote Actors, "Let it crash" fail over, and Dispatcher. Other Scala oriented tools we will use include sbt and ENSIME mode for emacs.

Multi-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and QuasarMulti-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and Quasar

Everybody wants scalable systems. However, writing non-blocking applications in Java is not an easy task. In this session, we'll go over 3 different frameworks for managing multi-treading and concurrency support (Akka, Vertx and Quasar).

multithreadingquazarakka
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
 …

Recommended for you

React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack

ave time learning on your own. Start Building with React, MongoDB, Express, & Node. The MERN Stack. Learning a new JavaScript framework is difficult. You can spend weeks learning new concepts. If an online example doesn’t work, you may spend countless hours Googling, searching Stack Overflow and blogs for the solution. Take the fast track and learn from an experienced Senior Software Engineer and professional instructor! About this Course  This highly interactive course features a large amount of student labs and hands-on coding. You will be taught how to assemble the complete stack required to build a modern web app using React.js, MongoDB (a NoSQL database) and Express (a framework for web application servers). This course will also cover many other tools that go into building a complete web application: React Router, React-Bootstrap, Redux, Babel, and Webpack.  What You Will Learn • How to use modern JavaScript features • Webpack • Node & Express • Reading and writing data to a MongoDB database • Babel • React • State Management with Redux • Mongoose • And More!

es6reduxjavascript
Building a chatbot – step by step
Building a chatbot – step by stepBuilding a chatbot – step by step
Building a chatbot – step by step

This presentation covers how to build a chatbot - step by step. It covers Facebook, integration using AWS Lex and AWS Lambda

botschatbotsaws lambda
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a cluster

Building data pipelines shouldn't be so hard, you just need to choose the right tools for the task. We will review Akka and Spark streaming, how they work and how to use them and when.

javabigdataspark
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)(_ && _)
    }
   …

Recommended for you

Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh

This document provides an overview of the Scala programming language. Some key points: - Scala runs on the Java Virtual Machine and was created by Martin Odersky at EPFL. - It has been around since 2003 and the current stable release is 2.7.7. Release 2.8 beta 1 is due out soon. - Scala combines object-oriented and functional programming. It has features like pattern matching, actors, XML literals, and more that differ from Java. Everything in Scala is an object.

scotlandtechmeetupjava
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala

Futures allow asynchronous and non-blocking operations in Scala. They represent a value that may be available in the future. Promises allow producing values for futures - a promise can be used to complete a future with a value. Futures can be composed using methods like map, flatMap, and recover to build asynchronous workflows. Callbacks allow non-blocking handling of future results.

scalafunctional programmingbig data
Concurrency and scalability with akka
Concurrency and scalability  with akkaConcurrency and scalability  with akka
Concurrency and scalability with akka

Akka is a toolkit and runtime for building highly concurrent, distributed, and resilient message-driven applications on the JVM. This slides is for my lecture in #zconf 2016. I introduced actor model and some features provided in Akka.

akkaactor modelfunctional programming
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

Recommended for you

Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...

Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organized by Knoldus Software LLP in New Delhi

scala traitsknoldus slidesharescala
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review

Slides from my talk at the Junction (Jan 24, 2013) Single-core performance has hit a ceiling, and building web-scale multi-core applications using imperative programming models is nightmarishly difficult. Parallel programming creates a new set of challenges, best practices and design patterns. Scala is designed to enable building scalable systems, elegantly blending functional and object oriented paradigms into an expressive and concise language, while retaining interoperability with Java. Scala is the fastest growing JVM programming language, being rapidly adopted by leading companies such as Twitter, LinkedIn and FourSquare. This presentation provides a comprehensive overview of the language, which managed to increase type safety while feeling more dynamic, being more concise and improving readability at the same time. We will see how Scala simplifies real life problems by empowering the developer with powerful functional programming primitives, without giving up on the object oriented paradigm. The overview includes tools for multi-core programming in Scala, the type system, collection framework and domain-specific languages. We’ll explore the power of compile-time meta-programming, which is made possible by the newly released Scala 2.10, and get a glimpse into what to expect from 2.11 in 2014. We will also see how Scala helps overcome the inherent limitations of Java, such as type erasure, array covariance and boxing overhead. Multiple examples emphasize how Scala pushes the JVM harder than any other mainstream language through the infinite number of boilerplate busters, increased type safety and productivity boosters from a Java developer’s perspective.

scalamulti-core programmingjvm
Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers

This document provides an introduction to the Scala programming language for Java programmers. It discusses Scala's motivation as a scalable and more productive alternative to Java. Key features covered include static typing, object-oriented and functional programming, traits, pattern matching, and actors. Examples are provided to illustrate concepts like functions as first-class values, partially applied functions, and collection operations. The document concludes by mentioning additional Scala concepts and providing references for further reading.

javascalaprogramming language
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

What's hot

Advanced akka features
Advanced akka featuresAdvanced akka features
Advanced akka features
Grzegorz Duda
 
The dark side of Akka and the remedy
The dark side of Akka and the remedyThe dark side of Akka and the remedy
The dark side of Akka and the remedy
krivachy
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
Felipe
 
Reactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDaysReactive Web-Applications @ LambdaDays
Reactive Web-Applications @ LambdaDays
Manuel Bernhardt
 
Building reactive distributed systems with Akka
Building reactive distributed systems with Akka Building reactive distributed systems with Akka
Building reactive distributed systems with Akka
Johan Andrén
 
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
2014-02-20 | Akka Concurrency (Vienna Scala User Group)2014-02-20 | Akka Concurrency (Vienna Scala User Group)
2014-02-20 | Akka Concurrency (Vienna Scala User Group)
Dominik Gruber
 
Introduction to Apache Kafka- Part 2
Introduction to Apache Kafka- Part 2Introduction to Apache Kafka- Part 2
Introduction to Apache Kafka- Part 2
Knoldus Inc.
 
The dark side of Akka and the remedy - bp.scala meetup
The dark side of Akka and the remedy - bp.scala meetupThe dark side of Akka and the remedy - bp.scala meetup
The dark side of Akka and the remedy - bp.scala meetup
krivachy
 
Introducing Akka
Introducing AkkaIntroducing Akka
Introducing Akka
Jonas Bonér
 
JVM languages "flame wars"
JVM languages "flame wars"JVM languages "flame wars"
JVM languages "flame wars"
Gal Marder
 
Developing distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka ClusterDeveloping distributed applications with Akka and Akka Cluster
Developing distributed applications with Akka and Akka Cluster
Konstantin Tsykulenko
 
Introduction of failsafe
Introduction of failsafeIntroduction of failsafe
Introduction of failsafe
Sunghyouk Bae
 
What’s expected in Java 9
What’s expected in Java 9What’s expected in Java 9
What’s expected in Java 9
Gal Marder
 
Akka lsug skills matter
Akka lsug skills matterAkka lsug skills matter
Akka lsug skills matter
Skills Matter
 
Multi-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and QuasarMulti-threading in the modern era: Vertx Akka and Quasar
Multi-threading in the modern era: Vertx Akka and Quasar
Gal Marder
 
React Development with the MERN Stack
React Development with the MERN StackReact Development with the MERN Stack
React Development with the MERN Stack
Troy Miles
 
Building a chatbot – step by step
Building a chatbot – step by stepBuilding a chatbot – step by step
Building a chatbot – step by step
CodeOps Technologies LLP
 
Stream processing from single node to a cluster
Stream processing from single node to a clusterStream processing from single node to a cluster
Stream processing from single node to a cluster
Gal Marder
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
Stuart Roebuck
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
Stratio
 

What's hot (20)

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

Viewers also liked

Concurrency and scalability with akka
Concurrency and scalability  with akkaConcurrency and scalability  with akka
Concurrency and scalability with akka
Bardia Heydari
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Sanjeev_Knoldus
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
Sagie Davidovich
 
Scala for Java programmers
Scala for Java programmersScala for Java programmers
Scala for Java programmers
輝 子安
 
Akka -- Scalability in Scala and Java
Akka -- Scalability in Scala and JavaAkka -- Scalability in Scala and Java
Akka -- Scalability in Scala and Java
Nadav Wiener
 
Python in 90 minutes
Python in 90 minutesPython in 90 minutes
Python in 90 minutes
Bardia Heydari
 
Comparing JVM Web Frameworks - Devoxx France 2013
Comparing JVM Web Frameworks - Devoxx France 2013Comparing JVM Web Frameworks - Devoxx France 2013
Comparing JVM Web Frameworks - Devoxx France 2013
Matt Raible
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and Scala
Yevgeniy Brikman
 
Event-sourced architectures with Akka
Event-sourced architectures with AkkaEvent-sourced architectures with Akka
Event-sourced architectures with Akka
Sander Mak (@Sander_Mak)
 

Viewers also liked (9)

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

Similar to Concurrency in Scala - the Akka way

Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
Yardena Meymann
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
Skills Matter
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
Yung-Lin Ho
 
Scalamen and OT
Scalamen and OTScalamen and OT
Scalamen and OT
getch123
 
Workflow as code with Azure Durable Functions
Workflow as code with Azure Durable FunctionsWorkflow as code with Azure Durable Functions
Workflow as code with Azure Durable Functions
Massimo Bonanni
 
BPM-3 Advanced Workflow Deep Dive
BPM-3 Advanced Workflow Deep DiveBPM-3 Advanced Workflow Deep Dive
BPM-3 Advanced Workflow Deep Dive
Alfresco Software
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
Yung-Lin Ho
 
Reactive Streams - László van den Hoek
Reactive Streams - László van den HoekReactive Streams - László van den Hoek
Reactive Streams - László van den Hoek
RubiX BV
 
Azure Function Workflow
Azure Function WorkflowAzure Function Workflow
Azure Function Workflow
Andrea Tosato
 
Process Orchestration with Flowable and Spring Boot
Process Orchestration with Flowable and Spring BootProcess Orchestration with Flowable and Spring Boot
Process Orchestration with Flowable and Spring Boot
Chavdar Baikov
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
KatyShimizu
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
KatyShimizu
 
Backday Xebia : Akka, the reactive toolkit
Backday Xebia : Akka, the reactive toolkitBackday Xebia : Akka, the reactive toolkit
Backday Xebia : Akka, the reactive toolkit
Publicis Sapient Engineering
 
Reactive Microservices with JRuby and Docker
Reactive Microservices with JRuby and DockerReactive Microservices with JRuby and Docker
Reactive Microservices with JRuby and Docker
John Scattergood
 
Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
Knoldus Inc.
 
Web Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI ToolWeb Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI Tool
Sperasoft
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
Yardena Meymann
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
Rahul Shukla
 
Building a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable FunctionsBuilding a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable Functions
Joonas Westlin
 
A Jouney Through Wonderland - Jimdo
A Jouney Through Wonderland - JimdoA Jouney Through Wonderland - Jimdo
A Jouney Through Wonderland - Jimdo
Johann Paulus Almeida
 

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

Scale up your thinking
Scale up your thinkingScale up your thinking
Scale up your thinking
 
Akka london scala_user_group
Akka london scala_user_groupAkka london scala_user_group
Akka london scala_user_group
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Scalamen and OT
Scalamen and OTScalamen and OT
Scalamen and OT
 
Workflow as code with Azure Durable Functions
Workflow as code with Azure Durable FunctionsWorkflow as code with Azure Durable Functions
Workflow as code with Azure Durable Functions
 
BPM-3 Advanced Workflow Deep Dive
BPM-3 Advanced Workflow Deep DiveBPM-3 Advanced Workflow Deep Dive
BPM-3 Advanced Workflow Deep Dive
 
Introduction to Actor Model and Akka
Introduction to Actor Model and AkkaIntroduction to Actor Model and Akka
Introduction to Actor Model and Akka
 
Reactive Streams - László van den Hoek
Reactive Streams - László van den HoekReactive Streams - László van den Hoek
Reactive Streams - László van den Hoek
 
Azure Function Workflow
Azure Function WorkflowAzure Function Workflow
Azure Function Workflow
 
Process Orchestration with Flowable and Spring Boot
Process Orchestration with Flowable and Spring BootProcess Orchestration with Flowable and Spring Boot
Process Orchestration with Flowable and Spring Boot
 
[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless[NDC 2019] Enterprise-Grade Serverless
[NDC 2019] Enterprise-Grade Serverless
 
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
[NDC 2019] Functions 2.0: Enterprise-Grade Serverless
 
Backday Xebia : Akka, the reactive toolkit
Backday Xebia : Akka, the reactive toolkitBackday Xebia : Akka, the reactive toolkit
Backday Xebia : Akka, the reactive toolkit
 
Reactive Microservices with JRuby and Docker
Reactive Microservices with JRuby and DockerReactive Microservices with JRuby and Docker
Reactive Microservices with JRuby and Docker
 
Introduction to Akka
Introduction to AkkaIntroduction to Akka
Introduction to Akka
 
Web Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI ToolWeb Services Automated Testing via SoapUI Tool
Web Services Automated Testing via SoapUI Tool
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
Akka (1)
Akka (1)Akka (1)
Akka (1)
 
Building a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable FunctionsBuilding a document e-signing workflow with Azure Durable Functions
Building a document e-signing workflow with Azure Durable Functions
 
A Jouney Through Wonderland - Jimdo
A Jouney Through Wonderland - JimdoA Jouney Through Wonderland - Jimdo
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