Building Reactive Applications with Akka (in Scala)

download Building Reactive Applications with Akka (in Scala)

of 114

  • date post

    19-Aug-2014
  • Category

    Engineering

  • view

    6.030
  • download

    8

Embed Size (px)

description

Abstract: The demands and expectations for applications have changed dramatically in recent years. Applications today are deployed on a wide range of infrastructure; from mobile devices up to thousands of nodes running in the cloud—all powered by multi-core processors. They need to be rich and collaborative, have a real-time feel with millisecond response time and should never stop running. Additionally, modern applications are a mashup of external services that need to be consumed and composed to provide the features at hand. We are seeing a new type of applications emerging to address these new challenges—these are being called Reactive Applications. In this talk we will introduce you to Akka and discuss how it can help you deliver on the four key traits of Reactive; Event-Driven, Scalable, Resilient and Responsive. We will start with the basics of Akka and work our way towards some of its more advanced modules such as Akka Cluster and Akka Persistence—all driven through code and practical examples.

Transcript of Building Reactive Applications with Akka (in Scala)

  • Building Reactive Applications with Akka Jonas Bonr Typesafe CTO & co-founder @jboner
  • This is an era of profound change.
  • Reactive Applications Implications are massive, change is unavoidable 3 ! ! ! Users are demanding richer and more personalized experiences. ! Yet, at the same time, expecting blazing fast load time. Users ! ! ! Mobile and HTML5; Data and compute clouds; scaling on demand. ! Modern application technologies are fueling the always-on, real- time user expectation. Applications ! ! ! Businesses are being pushed to react to these changing user expectations ! ...and embrace modern application requirements. ! ! ! ! ! ! ! Businesses
  • As a matter of necessity, businesses are going Reactive.
  • Reactive Applications Reactive applications share four traits 5
  • Reactive applications react to changes in the world around them.
  • Event-Driven Loosely coupled architecture, easier to extend, maintain, evolve Asynchronous and non-blocking Concurrent by design, immutable state Lower latency and higher throughput 7 Clearly, the goal is to do these operations concurrently and non-blocking, so that entire blocks of seats or sections are not locked. Were able to find and allocate seats under load in less than 20ms without trying very hard to achieve it. Andrew Headrick, Platform Architect, Ticketfly
  • Introducing the Actor Model.
  • 9 The Actor Model
  • 9 A computational model that embodies: The Actor Model
  • 9 A computational model that embodies: Processing The Actor Model
  • 9 A computational model that embodies: Processing Storage The Actor Model
  • 9 A computational model that embodies: Processing Storage Communication The Actor Model
  • 9 A computational model that embodies: Processing Storage Communication Supports 3 axiomswhen an Actor receives a message it can: The Actor Model
  • 9 A computational model that embodies: Processing Storage Communication Supports 3 axiomswhen an Actor receives a message it can: 1. Create new Actors The Actor Model
  • 9 A computational model that embodies: Processing Storage Communication Supports 3 axiomswhen an Actor receives a message it can: 1. Create new Actors 2. Send messages to Actors it knows The Actor Model
  • 9 A computational model that embodies: Processing Storage Communication Supports 3 axiomswhen an Actor receives a message it can: 1. Create new Actors 2. Send messages to Actors it knows 3. Designate how it should handle the next message it receives The Actor Model
  • The essence of an actor 0. DEFINE 1. CREATE 2. SEND 3. BECOME 4. SUPERVISE 10
  • 0. DEFINE 11 case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }
  • 0. DEFINE 11 Define the message(s) the Actor should be able to respond to case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } }
  • 0. DEFINE 11 Define the message(s) the Actor should be able to respond to case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } } Define the Actor class
  • 0. DEFINE 11 Define the message(s) the Actor should be able to respond to case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(s"Hello ${who}") } } Define the Actor class Define the Actors behavior
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") 1. CREATE
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") 1. CREATE Create an Actor system
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") 1. CREATE Create an Actor system Actor configuration
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Give it a name 1. CREATE Create an Actor system Actor configuration
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Give it a name 1. CREATE Create the Actor Create an Actor system Actor configuration
  • case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello " + who) } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") Give it a name 1. CREATE Create the ActorYou get an ActorRef back Create an Actor system Actor configuration
  • Guardian System Actor Actors can form hierarchies
  • Guardian System Actor system.actorOf(Props[Foo], Foo) Actors can form hierarchies
  • Foo Guardian System Actor system.actorOf(Props[Foo], Foo) Actors can form hierarchies
  • Foo Guardian System Actor context.actorOf(Props[A], A) Actors can form hierarchies
  • A Foo Guardian System Actor context.actorOf(Props[A], A) Actors can form hierarchies
  • A B BarFoo C B E A D C Guardian System Actor Actors can form hierarchies
  • A B BarFoo C B E A D C Guardian System Actor Name resolutionlike a file-system
  • A B BarFoo C B E A D C /Foo Guardian System Actor Name resolutionlike a file-system
  • A B BarFoo C B E A D C /Foo /Foo/A Guardian System Actor Name resolutionlike a file-system
  • A B BarFoo C B E A D C /Foo /Foo/A /Foo/A/B Guardian System Actor Name resolutionlike a file-system
  • A B BarFoo C B E A D C /Foo /Foo/A /Foo/A/B /Foo/A/D Guardian System Actor Name resolutionlike a file-system
  • 2. SEND 15 case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(sHello ${who}") } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker")
  • 2. SEND 15 case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info(sHello ${who}") } } ! val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter ! Greeting("Charlie Parker") Send the message asynchronously
  • Bring it together 16 case class Greeting(who: String) ! class GreetingActor extends Actor with ActorLogging { def re