Scala + Akka + ning/async-http-client - Vancouver Scala meetup February 2015
AKKA and Scala @ Inneractive
Transcript of AKKA and Scala @ Inneractive
AKKA & Scala @ Inneractive
Intro
Gal Aviv
R&D Group Manager
What we do…
RTB Networks
Advertiser
Advertiser
Advertiser
Advertiser
Advertiser
$$$
1M/min
250msRIR Networks
SAPI Networks
Video Networks
RAPI Networks
Architecture
Richard Grossman
System Architect
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
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
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
Akka Basics – The Actor Model
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
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)
Akka Basics – Dispatcher
Akka Basics – Router
Akka Basics - State & Supervision
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)
}
}
}
}
}
}
Inneractive Actor's Model
Development
Aviem Zur
Senior Server-Side Developer
Akka in Production
lConsultation
lMonitoring:
lTypesafe Console (EOL, now Activator)
lKamon (Actor Monitoring: Mailbox Size, Time in Mailbox, Processing Time)
lDatadog – Monitoring SaaS
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
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
Scala Features
lType system – val, var, type inference
lClass Definition
lObjects
lCollections & Ops
lString interpolation
lInteroperability with Java
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
Migrating to Scala
lJava/Scala hybrid codebase (No rewrite)
lMaven
lIntelliJ IDEa
lBackward incompatibility
lResources: Coursera, Google Groups, Books, Scala REPL / Worksheet
lScalatest
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()}
}}
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
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
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
{ Thanks}