Building reactive systems with Akka

29
Building Reactive Systems with Akka

Transcript of Building reactive systems with Akka

Page 1: Building reactive systems with Akka

Building Reactive Systems with Akka

Page 2: 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 more complex example

Page 3: Building reactive systems with Akka

Part 1 – Introduction to Akka

Page 4: Building reactive systems with Akka

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

Page 5: Building reactive systems with Akka

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

Page 6: Building reactive systems with Akka

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.

Page 7: Building reactive systems with Akka

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

Page 8: Building reactive systems with Akka

Part 2 – Scala Crash Course™

Page 9: Building reactive systems with Akka

The Scala languageStrong static type systemCompiles to Java bytecode, runs on the JVMMixes object-oriented and functional style

Designed by Martin Odersky (javac)Publicly available since 2004Commercial support by Typesafe Inc. since 2011

Page 10: Building reactive systems with Akka

Scala Crash Course in 9 linesThings to observe: Packages, objects and classes Constructor in object body Values and method calls

Also note there are: no semicolons (;)

Page 11: Building reactive systems with Akka

Scala Crash Course in 9 linesThings 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

Page 12: Building reactive systems with Akka

Scala Crash Course in 9 linesThings 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 ({ } ( ))

Page 13: Building reactive systems with Akka

Scala Crash Course in 9 linesThings to observe: Packages, objects and classes Constructor in object body Values and method calls Method definitions Case classes Higher order functions String interpolationAlso note there are: no semicolons (;) no unnecessary braces ({ } ( )) no explicit types, unless necessary

Page 14: Building reactive systems with Akka

Part 3 – Hello Reactive World

Page 15: Building reactive systems with Akka

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

Page 16: Building reactive systems with Akka

Akka HelloWorld

Page 17: Building reactive systems with Akka

Part 4 – A more complex example

Page 18: Building reactive systems with Akka

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

Page 19: Building reactive systems with Akka

Demo

Page 20: Building reactive systems with Akka

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

Page 21: Building reactive systems with Akka

A re

activ

e ar

chite

ctur

e

Page 22: Building reactive systems with Akka

Code review

Page 23: Building reactive systems with Akka

Code review - Streamer actor

Page 24: Building reactive systems with Akka

Code review - ClientsTracker actor

Page 25: Building reactive systems with Akka

Code review - WSProxy actor

Page 26: Building reactive systems with Akka

Code review - Play! controller class

Page 27: Building reactive systems with Akka

WebSocket JS code

Page 28: Building reactive systems with Akka

Demo – Load Test

Page 29: Building reactive systems with Akka

Resources Twitter streamer source - https://github.com/kjozsa/reactive2 Akka - http://akka.io/ Play! Framework - https://playframework.com/