Building reactive systems with Akka

Post on 12-Apr-2017

351 views 3 download

Transcript of Building reactive systems with Akka

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

Part 1 – Introduction to 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

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

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.

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

Part 2 – Scala Crash Course™

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

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 (;)

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

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 ({ } ( ))

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

Part 3 – Hello Reactive World

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

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

Demo

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

A re

activ

e ar

chite

ctur

e

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 - https://playframework.com/