Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Building Reactive Systems with Akka
Today’s Agenda
 Part 1 – Introduction to Akka
 Part 2 – Scala Crash Course™
 Part 3 – Hello Reactive World
 Part 4 – A...
Part 1 – Introduction to Akka
Problems with traditional systems
 Extreme growth of data and number of clients
 Mobile web (*), Internet of Things, etc...
Properties of Reactive Systems
 Responsive
 Gives low-latency responses even for overloaded systems
 Resilient
 Stays ...
Introducing Akka
 A framework and runtime engine for building reactive systems on the JVM
 Implements the Actor model (m...
Introducing Akka Actors
 Actors are the base building blocks of actor systems
 Actors are lightweight objects
 ~300 byt...
Part 2 – Scala Crash Course™
The Scala language
 Strong static type system
 Compiles to Java bytecode, runs on the JVM
 Mixes object-oriented and fu...
Scala Crash Course in 9 lines
 Things to observe:
 Packages, objects and classes
 Constructor in object body
 Values a...
Scala Crash Course in 9 lines
 Things to observe:
 Packages, objects and classes
 Constructor in object body
 Values a...
Scala Crash Course in 9 lines
 Things to observe:
 Packages, objects and classes
 Constructor in object body
 Values a...
Scala Crash Course in 9 lines
 Things to observe:
 Packages, objects and classes
 Constructor in object body
 Values a...
Part 3 – Hello Reactive World
Akka basics in Scala
 Actors live in an ActorSystem
 val system = ActorSystem(“hello”)
 Actors are created via Props (f...
Akka HelloWorld
Part 4 – A more complex example
Building a Twitter streamer
 Twitter == poor man’s Big Data (up to 50msg/sec)
 Goal: Build a web application which displ...
Demo
A reactive architecture
 System components:
 Akka Event Bus
 Play! Framework controller
 Actors in the system:
 1 x S...
Areactivearchitecture
Code review
Code review - Streamer actor
Code review - ClientsTracker actor
Code review - WSProxy actor
Code review - Play! controller class
WebSocket JS code
Demo – Load Test
Resources
 Twitter streamer source - https://github.com/kjozsa/reactive2
 Akka - http://akka.io/
 Play! Framework - htt...
Upcoming SlideShare
Loading in …5
×

Building reactive systems with Akka

416 views

Published on

A short intro to reactive systems, Scala, Akka and the Play Framework, with a Twitter based live demonstration and performance meters. Find the sample code at https://github.com/kjozsa/reactive2

Published in: Software
  • Be the first to comment

Building reactive systems with Akka

  1. 1. Building Reactive Systems with Akka
  2. 2. Today’s Agenda  Part 1 – Introduction to Akka  Part 2 – Scala Crash Course™  Part 3 – Hello Reactive World  Part 4 – A more complex example
  3. 3. Part 1 – Introduction to Akka
  4. 4. Problems with traditional systems  Extreme growth of data and number of clients  Mobile web (*), Internet of Things, etc.  Threads won’t scale  1 thread for each user  1 Mb minimum stack size / thread on 64 bit VMs  10 000 concurrent users => do the math   Existing programming models won’t scale either  Writing bug-free thread synchronization / locking code manually is extreme hard  Mutable state is the root of all evil  Mutable state is the basic idea of Object Oriented Programming.. (oops)  Polling sucks, we live in a realtime world
  5. 5. Properties of Reactive Systems  Responsive  Gives low-latency responses even for overloaded systems  Resilient  Stays responsive even upon system failures  Message driven  Asynchronous and non-blocking, concurrent by design  Has no mutable shared state  Elastic  Can scale out horizontally on demand
  6. 6. Introducing Akka  A framework and runtime engine for building reactive systems on the JVM  Implements the Actor model (mathematical model of Carl Hewitt, 1973)  Asynchronous, distributed by design  Resilient and self-healing system (motto: “Let it crash”)  High performance  50 million message / sec  2.5 million actor instances per GB of heap space  Written in the Scala language, has Scala and Java bindings  Open source with Apache2 license, commercially supported by Typesafe Inc.
  7. 7. Introducing Akka Actors  Actors are the base building blocks of actor systems  Actors are lightweight objects  ~300 bytes / instance  Encapsulate state and behavior  Have no shared state ever  Actors are asynchronous and non-blocking  Communicate only via message passing  Have a mailbox for inbound messages  Always process messages in order  Actors live in hierarchy  Used for supervising  Actors have a reference  Phone number analogy
  8. 8. Part 2 – Scala Crash Course™
  9. 9. The Scala language  Strong static type system  Compiles to Java bytecode, runs on the JVM  Mixes object-oriented and functional style  Designed by Martin Odersky (javac)  Publicly available since 2004  Commercial support by Typesafe Inc. since 2011
  10. 10. Scala Crash Course in 9 lines  Things to observe:  Packages, objects and classes  Constructor in object body  Values and method calls  Also note there are:  no semicolons (;)
  11. 11. Scala Crash Course in 9 lines  Things to observe:  Packages, objects and classes  Constructor in object body  Values and method calls  Method definitions  Also note there are:  no semicolons (;)  no explicit types, unless necessary
  12. 12. Scala Crash Course in 9 lines  Things to observe:  Packages, objects and classes  Constructor in object body  Values and method calls  Method definitions  Case classes  Also note there are:  no semicolons (;)  no explicit types, unless necessary  no unnecessary braces ({ } ( ))
  13. 13. Scala Crash Course in 9 lines  Things to observe:  Packages, objects and classes  Constructor in object body  Values and method calls  Method definitions  Case classes  Higher order functions  String interpolation  Also note there are:  no semicolons (;)  no unnecessary braces ({ } ( ))  no explicit types, unless necessary
  14. 14. Part 3 – Hello Reactive World
  15. 15. Akka basics in Scala  Actors live in an ActorSystem  val system = ActorSystem(“hello”)  Actors are created via Props (factories)  val actor: ActorRef = system.actorOf(Props(classOf[MyActor.class]))  Messages are most often plain case classes / case objects  case class HelloMessage(message: String)  ActorRefs are used to send messages  actor ! HelloMessage(s“hello from $self”)
  16. 16. Akka HelloWorld
  17. 17. Part 4 – A more complex example
  18. 18. Building a Twitter streamer  Twitter == poor man’s Big Data (up to 50msg/sec)  Goal: Build a web application which displays the Twitter stream and the number of connected users  Tech stack  Twitter4J – twitter API  Play! – Scala web framework with WebSocket support, built on Akka  Akka – actors based middleware  Websocket JS API, JQuery, Bootstrap CSS  Gatling – stress test tool
  19. 19. Demo
  20. 20. A reactive architecture  System components:  Akka Event Bus  Play! Framework controller  Actors in the system:  1 x Streamer – connects to Twitter, receives status updates  n x WSProxy – proxies events via WebSocket to client  1 x ClientsTracker – tracks the number of clients, publishes the number every N seconds  WebSocket in the browser:  Registers with Play! controller, connects to Actor  Receives push messages in JSON  Replaces DOM with JQuery
  21. 21. Areactivearchitecture
  22. 22. Code review
  23. 23. Code review - Streamer actor
  24. 24. Code review - ClientsTracker actor
  25. 25. Code review - WSProxy actor
  26. 26. Code review - Play! controller class
  27. 27. WebSocket JS code
  28. 28. Demo – Load Test
  29. 29. Resources  Twitter streamer source - https://github.com/kjozsa/reactive2  Akka - http://akka.io/  Play! Framework - https://playframework.com/

×