Akka & Scala were chosen as the concurrency framework and programming language respectively for an advertising company to optimize machine utilization and handle high throughput and low latency requirements. Akka provides an actor model for building concurrent and distributed applications, while Scala is a functional programming language that integrates well with Akka and Java. The architecture was refactored over time to use Akka actors and Spray REST services built on top of Akka for asynchronous request handling at scale.
5. 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
6. 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
7. 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
9. 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
10. 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)
14. 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)
}
}
}
}
}
}
19. 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
21. Scala Features
lType system – val, var, type inference
lClass Definition
lObjects
lCollections & Ops
lString interpolation
lInteroperability with Java
22. 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
23. Migrating to Scala
lJava/Scala hybrid codebase (No rewrite)
lMaven
lIntelliJ IDEa
lBackward incompatibility
lResources: Coursera, Google Groups, Books, Scala REPL / Worksheet
lScalatest
24. Scalatest Example
import collection.mutable.Stack
import 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()
}
}
}
25. 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
26. 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
27. 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