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
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. 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. 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. 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
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. 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. 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. 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. 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
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”)
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
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