AKKA and Scala @ Inneractive

28
AKKA & Scala @ Inneractive

Transcript of AKKA and Scala @ Inneractive

Page 1: AKKA and Scala @ Inneractive

AKKA & Scala @ Inneractive

Page 2: AKKA and Scala @ Inneractive

Intro

Gal Aviv

R&D Group Manager

Page 3: AKKA and Scala @ Inneractive

What we do…

RTB Networks

Advertiser

Advertiser

Advertiser

Advertiser

Advertiser

$$$

1M/min

250msRIR Networks

SAPI Networks

Video Networks

RAPI Networks

Page 4: AKKA and Scala @ Inneractive

Architecture

Richard Grossman

System Architect

Page 5: AKKA and Scala @ Inneractive

Version 1.0

lBased on Tomcat Server

lSpring MVC as HTTP request handler

lRequests to ad networks serially w/ Apache HTTP client

lHorizontal Scaling + AWS scaling policies worked

lDB Access Read/Write for each query

Page 6: AKKA and Scala @ Inneractive

RTB and Parallel HTTP World

l~ 30 HTTP requests out for 1 request in

lLatency requirements 150ms max per RTB request

lMobile advertising requires parallel HTTP

lRequires heavy resources

lMust optimize machine utilization

Page 7: AKKA and Scala @ Inneractive

Concurrency Framework

lWhy a framework for concurrent software?

lConcurrent application at very high scale is difficult

lRxJava, Akka, fiber co-routine (Quasar)

lAkka + Spray is the winner

Page 8: AKKA and Scala @ Inneractive

Akka Basics – The Actor Model

Page 9: AKKA and Scala @ Inneractive

Akka Example

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

greeter ! Greeting("Charlie Parker") // Send Message to actor

Page 10: AKKA and Scala @ Inneractive

Future with Akka

class GreetingActor extends Actor with ActorLogging {

def receive = {

case Greeting(who) ⇒ sender ! "Hello " + who

}

}

val future = greeter ? Greeting("Charlie Parker")

// Option 1: Future callback

future onComplete {

case Success(message) => //Have response here when future complete

case Failure(t) => println("An error has occured: " + t.getMessage)

}

// Option 2: Blocking code

Result = Await.result(future, 100 millis)

Page 11: AKKA and Scala @ Inneractive

Akka Basics – Dispatcher

Page 12: AKKA and Scala @ Inneractive

Akka Basics – Router

Page 13: AKKA and Scala @ Inneractive

Akka Basics - State & Supervision

Page 14: AKKA and Scala @ Inneractive

Spray Framework

lREST/HTTP integration on top of Akka

lAsynchronous

lActor-based

lLightweight

object Main extends App with SimpleRoutingApp {

implicit val system = ActorSystem("my-system")

val greeter = system.actorOf(Props[GreetingActor],

name = "greeter")

startServer(interface = "localhost", port = 8080) {

path("hello") {

get {

clientIP { ip =>

complete {

greeter ? Greeting(ip)

}

}

}

}

}

}

Page 15: AKKA and Scala @ Inneractive

Inneractive Actor's Model

Page 16: AKKA and Scala @ Inneractive

Development

Aviem Zur

Senior Server-Side Developer

Page 17: AKKA and Scala @ Inneractive

Akka in Production

lConsultation

lMonitoring:

lTypesafe Console (EOL, now Activator)

lKamon (Actor Monitoring: Mailbox Size, Time in Mailbox, Processing Time)

lDatadog – Monitoring SaaS

Page 18: AKKA and Scala @ Inneractive
Page 19: AKKA and Scala @ Inneractive

Akka in Production

lSpray-servlet on top of Tomcat 7 (Servlet 3) as a precursor to Spray standalone

lBlocking code

lBenchmarking

lBack-pressure (memory issues)

lDispatchers & routers tuning

Page 20: AKKA and Scala @ Inneractive

Why Scala?

lScala is:

lObject-oriented

lFunctional

lStatically typed - with type inference

lConcise - typically 3 to 4 times more than Java

lBased on JVM - full compatibility with Java

lSpray and Akka

Page 21: AKKA and Scala @ Inneractive

Scala Features

lType system – val, var, type inference

lClass Definition

lObjects

lCollections & Ops

lString interpolation

lInteroperability with Java

Page 22: AKKA and Scala @ Inneractive

More Scala Features

lFunctions as values - first class citizen

lLazy evaluation

lCase Classes & Pattern matching

lDIY Operators & DSLs

lOptions – No more null

lImplicit Conversion

lFutures

lTraits

Page 23: AKKA and Scala @ Inneractive

Migrating to Scala

lJava/Scala hybrid codebase (No rewrite)

lMaven

lIntelliJ IDEa

lBackward incompatibility

lResources: Coursera, Google Groups, Books, Scala REPL / Worksheet

lScalatest

Page 24: AKKA and Scala @ Inneractive

Scalatest Exampleimport collection.mutable.Stackimport org.scalatest._

class ExampleSpec extends FlatSpec with Matchers {

"A Stack" should "pop values in last-in-first-out order" in {val stack = new Stack[Int]stack.push(1)stack.push(2)stack.pop() should be (2)stack.pop() should be (1)

}

it should "throw NoSuchElementException if an empty stack is popped" in {val emptyStack = new Stack[Int]a [NoSuchElementException] should be thrownBy {

emptyStack.pop()}

}}

Page 25: AKKA and Scala @ Inneractive

Migrating to Akka

lActor model vs. stack

lChain of Responsibility / Actors hybrid (Again, no rewrite)

lRough breakdown of existing code into actors

lRefactor blocking code

lSpring bridge

Page 26: AKKA and Scala @ Inneractive

Resources

lCoursera

lFunctional Programming Principles in Scala

lPrinciples of Reactive Programming

lGoogle Groups

lScala Google Group

lAkka Google Group

lSpray Google Group

lBooks

lScala for the

Impatient

lAkka Essentials

lEffective Akka

lScala REPL / Worksheet

Page 27: AKKA and Scala @ Inneractive

What's Next ?

lAkka actor to micro-service

lCluster of more than 100 Machines talking each other

lAkka Streaming to manage back pressure at actor level

lMESOS clustering framework

Page 28: AKKA and Scala @ Inneractive

{ Thanks}