akka and scala @ inneractive
TRANSCRIPT
![Page 1: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/1.jpg)
AKKA & Scala @ Inneractive
![Page 2: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/2.jpg)
Intro
Gal Aviv
R&D Group Manager
![Page 3: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/4.jpg)
Architecture
Richard Grossman
System Architect
![Page 5: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/8.jpg)
Akka Basics – The Actor Model
![Page 9: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/11.jpg)
Akka Basics – Dispatcher
![Page 12: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/12.jpg)
Akka Basics – Router
![Page 13: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/13.jpg)
Akka Basics - State & Supervision
![Page 14: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/15.jpg)
Inneractive Actor's Model
![Page 16: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/16.jpg)
Development
Aviem Zur
Senior Server-Side Developer
![Page 17: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/18.jpg)
![Page 19: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/19.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/20.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/21.jpg)
Scala Features
lType system – val, var, type inference
lClass Definition
lObjects
lCollections & Ops
lString interpolation
lInteroperability with Java
![Page 22: AKKA and Scala @ Inneractive](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/22.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/23.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/24.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/25.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/26.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/27.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022052316/55a646281a28ab392e8b4593/html5/thumbnails/28.jpg)
{ Thanks}