Transcript
Page 1: Building Massively Scalable application with Akka 2.0

Achieving massive scalability with Akka

Vikas Hazrati @

Page 2: Building Massively Scalable application with Akka 2.0

2

about

CTO at Knoldus Software

Co­Founder at MyCellWasStolen.com

Community Editor at InfoQ.com

Dabbling with Scala – last 40 months

Enterprise grade implementations on Scala – 18 months 

Page 3: Building Massively Scalable application with Akka 2.0

akka

A actor­based concurrency framework

Provides solutions for non blocking concurrency

Written in Scala, also works in Java

  Open source 

  Now at version 2.0.2

Lead developer and founder: Jonas Boner

JRockit, AspectWerkz, AspectJ, Terracotta

Page 4: Building Massively Scalable application with Akka 2.0

what?

Platform for next generation, event driven, scalable and fault tolerant 

architectures on the JVM.

Page 5: Building Massively Scalable application with Akka 2.0

why akka

simpler concurrency

event driven

scale up or scale out

fault tolerance

location transparency / remoting

scala and java api

Page 6: Building Massively Scalable application with Akka 2.0

how to use

As a jar in your WEB­INF/lib

As a microkernel and drop your application in it

Page 7: Building Massively Scalable application with Akka 2.0

aha actors

message­Passing Concurrency

share NOTHING

isolated lightweight processes

communicates through messages

asynchronous and non­blocking

Page 8: Building Massively Scalable application with Akka 2.0

actorsdefined in the 1973 paper by Carl Hewitt

popularized by Erlang

alleviates the dev 

from explicit locking

and thread management

easy to write concurrent and parallel systems

actors like objects BUT dont share state

Page 9: Building Massively Scalable application with Akka 2.0
Page 10: Building Massively Scalable application with Akka 2.0

actor system

envision breaking task into subtasks

Page 11: Building Massively Scalable application with Akka 2.0

actor system

each actor has one supervisor

Nice co workers, dont bother others

Do not block (external interaction)

Do not pass mutable objects

Page 12: Building Massively Scalable application with Akka 2.0

mutability, aah!scala> var sum =0sum: Int = 0

scala> val list = (1 to 1000).toList.par

scala> list.foreach(sum += _); sumres7: Int = 452474

scala> var sum =0sum: Int = 0

scala> list.foreach(sum += _); sumres8: Int = 497761

scala> var sum =0sum: Int = 0

scala> list.foreach(sum += _); sumres9: Int = 422508

Page 13: Building Massively Scalable application with Akka 2.0

actor

Actor

state

behavior

mail-box

children

supervise strategy

actor reference

freely passed

Location transparency

hidesstate

Page 14: Building Massively Scalable application with Akka 2.0

defining an actor

import akka.actor.{ Actor, Props }

class MyFirstActor extends Actor {  def receive = {    case msg => println("Hello!!")  }}

Page 15: Building Massively Scalable application with Akka 2.0

create

import akka.actor.{ ActorSystem, Props }

val system = ActorSystem("firstApp")val myFirstActor = system.actorOf(Props[MyFirstActor])

MyFirstActor is an ActorRefCreate a top level actor

Page 16: Building Massively Scalable application with Akka 2.0

messages

! fire and forget

? ask – send and receive future

import akka.pattern.ask

implicit val timeout = Timeout(50000 milliseconds)

val future = myActor ? "hello"

Await.result(future, timeout.duration).asInstanceOf[Int]

myFirstActor ! “Hello”

Page 17: Building Massively Scalable application with Akka 2.0

reply

import akka.actor.Actor

class LongWorkingActor extends Actor {  def receive = {    case number: Int =>      sender ! (“Hi I received ” + number)  }}

Page 18: Building Massively Scalable application with Akka 2.0

stop

system stop myFirstActor

Page 19: Building Massively Scalable application with Akka 2.0

routers

RoundRobin

Random

SmallestMailBox

BroadCast

...

val router = system.actorOf(Props[RouterWorkerActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))

Page 20: Building Massively Scalable application with Akka 2.0

fault tolerance

let it crash

linked set of actors

Page 21: Building Massively Scalable application with Akka 2.0

all for one

Page 22: Building Massively Scalable application with Akka 2.0

one for one

Page 23: Building Massively Scalable application with Akka 2.0

supervisor strategy

class Supervisor extends Actor {  override val supervisorStrategy =  OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {    case _: ArithmeticException => Resume    case _: NullPointerException => Restart    case _: IllegalArgumentException => Stop    case _: Exception => Escalate  }}

Page 24: Building Massively Scalable application with Akka 2.0

supervisor hierarchies

Page 25: Building Massively Scalable application with Akka 2.0

other concepts

Remote Actors

Akka STM

Akka Serialization

Persistence

Page 26: Building Massively Scalable application with Akka 2.0

problem statement

Page 27: Building Massively Scalable application with Akka 2.0

considerations

Page 28: Building Massively Scalable application with Akka 2.0

hmm...

STM – X

Serialization – X

Persistence – X

Remoting – ?/X

Page 29: Building Massively Scalable application with Akka 2.0
Page 30: Building Massively Scalable application with Akka 2.0
Page 31: Building Massively Scalable application with Akka 2.0
Page 32: Building Massively Scalable application with Akka 2.0

dispatchers

Event based dispatcher

PinnedDispatcher

BalancingDispatcher

Calling ThreadDispatcher

Page 33: Building Massively Scalable application with Akka 2.0
Page 34: Building Massively Scalable application with Akka 2.0

ala carte'

Page 35: Building Massively Scalable application with Akka 2.0

used in ...

Page 36: Building Massively Scalable application with Akka 2.0

more information

http://blog.knoldus.com/tag/akka/

http://akka.io

[email protected]

https://github.com/knoldus/AkkaKnolX

Page 37: Building Massively Scalable application with Akka 2.0

Everyone ! “Thanks”


Recommended