122
Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors Jonas Bonér Scalable Solutions [email protected] twitter: @jboner Wednesday, February 16, 2011

Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Akka:Simpler Concurrency, Scalability &

Fault-tolerance through Actors

Jonas BonérScalable Solutions

[email protected] : @jboner

Wednesday, February 16, 2011

Page 2: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

The problem

It is way too hard to build:1. correct highly concurrent systems

2. truly scalable systems

3. fault-tolerant systems that self-heals

...using “state-of-the-art” tools

Wednesday, February 16, 2011

Page 3: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

akkaIntroducing

Wednesday, February 16, 2011

Page 4: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Vision

Simpler

Concurrency

Scalability

Fault-tolerance

Wednesday, February 16, 2011

Page 5: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Vision

...with One single unified

Programming model

Runtime service

Wednesday, February 16, 2011

Page 6: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Manage system overload

Wednesday, February 16, 2011

Page 7: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Scale up & Scale out

Wednesday, February 16, 2011

Page 8: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Replicate and distribute for fault-tolerance

Wednesday, February 16, 2011

Page 9: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Transparent load balancingWednesday, February 16, 2011

Page 10: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM

Simpler ConcurrencyEvent-driven Architecture

True ScalabilityFault-tolerance

Transparent RemotingJava & Scala API

Overview

Wednesday, February 16, 2011

Page 11: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

CORE SERVICES

ARCHITECTURE

Wednesday, February 16, 2011

Page 12: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ADD-ON MODULES

ARCHITECTURE

Wednesday, February 16, 2011

Page 13: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ARCHITECTURE

CLOUDY AKKA

Wednesday, February 16, 2011

Page 14: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

FINANCE

• Stock trend Analysis & Simulation

• Event-driven messaging systems

BETTING & GAMING

• Massive multiplayer online gaming

• High throughput and transactional betting

TELECOM

• Streaming media network gateways

SIMULATION

• 3D simulation engines

E-COMMERCE

• Social media community sites

SOME EXAMPLES:

WHERE IS AKKA USED?

Wednesday, February 16, 2011

Page 15: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

What is an Actor?

Wednesday, February 16, 2011

Page 16: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Behavior

State

Actor

Wednesday, February 16, 2011

Page 17: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 18: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 19: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 20: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 21: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 22: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Behavior

State

Actor

Wednesday, February 16, 2011

Page 23: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Event-drivenThread

Behavior

State

Actor

Wednesday, February 16, 2011

Page 24: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Akka Actorsone tool in the toolbox

Wednesday, February 16, 2011

Page 25: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Actor Model Benefits• Easier to reason about• Raised abstraction level• Easier to avoid– Race conditions– Deadlocks– Starvation– Live locks

Wednesday, February 16, 2011

Page 26: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

case  object  Tick

class  Counter  extends  Actor  {    var  counter  =  0

   def  receive  =  {        case  Tick  =>              counter  +=  1            println(counter)    }}

Actors

Wednesday, February 16, 2011

Page 27: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  counter  =  actorOf[Counter]

Create Actors

counter is an ActorRef

Wednesday, February 16, 2011

Page 28: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  actor  =  actorOf(new  MyActor(..))

Create Actors

create actor with constructor arguments

Wednesday, February 16, 2011

Page 29: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  counter  =  actorOf[Counter]counter.start

Start actors

Wednesday, February 16, 2011

Page 30: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  counter  =  actorOf[Counter].start

Start actors

Wednesday, February 16, 2011

Page 31: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  counter  =  actorOf[Counter].startcounter.stop

Stop actors

Wednesday, February 16, 2011

Page 32: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  MyActor  extends  Actor  {

   override  def  preStart  =  {        ...  //  called  before  ‘start’      }

   override  def  postStop  =  {        ...  //  called  after  ‘stop’    }}

life-cycle callbacks

Wednesday, February 16, 2011

Page 33: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

counter  !  Tick  

Send: !

fire-forget

Wednesday, February 16, 2011

Page 34: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  result  =  (actor  !!  Message).as[String]

Send: !!

uses Future under the hood (with time-out)

Wednesday, February 16, 2011

Page 35: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

//  returns  a  futureval  future  =  actor  !!!  Messagefuture.awaitval  result  =  future.get

...Futures.awaitOne(List(fut1,  fut2,  ...))Futures.awaitAll(List(fut1,  fut2,  ...))

Send: !!!

returns the Future directly

Wednesday, February 16, 2011

Page 36: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Futureval  future1,  future2,  future3  =      new  DefaultCompletableFuture(1000)

future1.awaitfuture2.onComplete(f  =>  ...)future3.map((f)  =>  ...)

future1.completeWithResult(...)  future2.completeWithException(...)future3.completeWith(future2)

Wednesday, February 16, 2011

Page 37: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  SomeActor  extends  Actor  {    def  receive  =  {        case  User(name)  =>              //  use  reply            self.reply(“Hi  ”  +  name)    }}

Reply

Wednesday, February 16, 2011

Page 38: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  SomeActor  extends  Actor  {    def  receive  =  {        case  User(name)  =>              //  store  away  the  sender            //  to  use  later  or              //  somewhere  else            ...  =  self.sender    }}

Reply

Wednesday, February 16, 2011

Page 39: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  SomeActor  extends  Actor  {    def  receive  =  {        case  User(name)  =>              //  store  away  the  sender  future            //  to  resolve  later  or              //  somewhere  else            ...  =  self.senderFuture    }}

Reply

Wednesday, February 16, 2011

Page 40: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

HotSwap

self  become  {    //  new  body    case  NewMessage  =>          ...    }

Wednesday, February 16, 2011

Page 41: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

HotSwap

actor  !  HotSwap  {    //  new  body    case  NewMessage  =>          ...    }

Wednesday, February 16, 2011

Page 42: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

HotSwap

self.unbecome()

Wednesday, February 16, 2011

Page 43: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  actors  =  Actor.registry.actors()val  actors  =  Actor.registry.actorsFor[TYPE]val  actors  =  Actor.registry.actorsFor(id)val  actor    =  Actor.registry.actorFor(uuid)Actor.registry.shutdownAll()

ActorRegistry

Wednesday, February 16, 2011

Page 44: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  MyActor  extends  Actor  {    self.dispatcher  =  Dispatchers        .newThreadBasedDispatcher(self)        ...}

actor.dispatcher  =  dispatcher  //  before  started

Set dispatcher

Wednesday, February 16, 2011

Page 45: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Let it crash fault-tolerance

Wednesday, February 16, 2011

Page 46: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Stolen from

ErlangWednesday, February 16, 2011

Page 47: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

9 nines

Wednesday, February 16, 2011

Page 48: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

...let’s take a standard OO application

Wednesday, February 16, 2011

Page 49: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 50: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Which components have critically important state

and explicit error handling?

Wednesday, February 16, 2011

Page 51: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 52: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Classification of State• Scratch data• Static data• Supplied at boot time• Supplied by other components

• Dynamic data• Data possible to recompute• Input from other sources; data

that is impossible to recompute

Wednesday, February 16, 2011

Page 53: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Classification of State• Scratch data• Static data• Supplied at boot time• Supplied by other components

• Dynamic data• Data possible to recompute• Input from other sources; data

that is impossible to recompute

Wednesday, February 16, 2011

Page 54: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Classification of State• Scratch data• Static data• Supplied at boot time• Supplied by other components

• Dynamic data• Data possible to recompute• Input from other sources; data

that is impossible to recompute

Must be protected

by any means

Wednesday, February 16, 2011

Page 55: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Fault-tolerant onion-layered Error Kernel

Wednesday, February 16, 2011

Page 56: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

An actor-based application with explicit error handling and

critically important state in an

Error Kernel

Wednesday, February 16, 2011

Page 57: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 58: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 59: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 60: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 61: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 62: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 63: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 64: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 65: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 66: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 67: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 68: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 69: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 70: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 71: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ErrorKernel

Wednesday, February 16, 2011

Page 72: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 73: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 74: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Node 1 Node 2

Wednesday, February 16, 2011

Page 75: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 76: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Wednesday, February 16, 2011

Page 77: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Node 1 Node 2

Wednesday, February 16, 2011

Page 78: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Node 1 Node 2

Replication

Wednesday, February 16, 2011

Page 79: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

AllForOneStrategy(    problems,    maxNrOfRetries,      withinTimeRange)

OneForOneStrategy(    problems,    maxNrOfRetries,      withinTimeRange)

Fault handlers

Wednesday, February 16, 2011

Page 80: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

link(actor)unlink(actor)

startLink(actor)spawnLink[MyActor]

Linking

Wednesday, February 16, 2011

Page 81: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  Supervisor  extends  Actor  {    faultHandler  =  OneForOneStrategy(        List(classOf[Throwable])          5,  5000))

   def  receive  =  {        case  Register(actor)  =>              link(actor)    }}

Supervision

Wednesday, February 16, 2011

Page 82: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  FaultTolerantService  extends  Actor  {    ...    override  def  preRestart(reason:  Throwable)  =  {        ...  //  clean  up  before  restart    }    override  def  postRestart(reason:  Throwable)  =  {        ...  //  init  after  restart    }}

Manage failure

Wednesday, February 16, 2011

Page 83: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Supervisor  supervisor  =  Supervisor(    SupervisorConfig(        AllForOneStrategy(List(classOf[Exception]),  3,  1000),                Supervise(                    actor1,                    Permanent)  ::                Supervise(                    actor2,                    Temporary)  ::                Nil        ))

Declarative config

Wednesday, February 16, 2011

Page 84: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Remote Actors

Wednesday, February 16, 2011

Page 85: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

//  use  host  &  port  in  configActor.remote.start()

Actor.remote.start("localhost",  2552)

Remote Server

Scalable implementation based on NIO (Netty) & Protobuf

Wednesday, February 16, 2011

Page 86: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Two types of

remote actorsClient initiated & managedServer initiated & managed

Wednesday, February 16, 2011

Page 87: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

import  Actor._

val  service  =  remote.actorOf[MyActor](host,  port)

service  !  message

Client-managedsupervision works across nodes

Wednesday, February 16, 2011

Page 88: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

import  Actor._

remote.register(“service:id”,  actorOf[MyService])

Server-managedregister and manage actor on server

client gets “dumb” proxy handle

server partWednesday, February 16, 2011

Page 89: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  service  =  remote.actorFor(    “service:id”,      “darkstar”,      9999)

service  !  message

Server-managed

client part

Wednesday, February 16, 2011

Page 90: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

import  Actor._

remote.register(actorOf[MyService])remote.registerByUuid(actorOf[MyService])remote.registerPerSession(    “service:id”,  actorOf[MyService])

remote.unregister(“service:id”)  remote.unregister(actorRef)

Server-managed

server partWednesday, February 16, 2011

Page 91: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

akka  {    remote  {        secure-­‐cookie  =  "050E0A0D0D06010A00000900040D060F0C09060B"        server  {            require-­‐cookie  =  on            untrusted-­‐mode  =  on        }    }}

Remoting Security

Erlang-style secure cookieWednesday, February 16, 2011

Page 92: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Clustered Actors

Wednesday, February 16, 2011

Page 93: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

• Subscription-based cluster membership service• Highly available cluster registry for actors• Highly available centralized configuration service• Automatic replication with automatic fail-over

upon node crash• Transparent and user-configurable load-balancing• Transparent adaptive cluster rebalancing• Leader election

Cloudy Akka

Wednesday, February 16, 2011

Page 94: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ZooKeeperZooKeeperZooKeeperEnsemble

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Wednesday, February 16, 2011

Page 95: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ZooKeeperZooKeeperZooKeeperEnsemble

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Wednesday, February 16, 2011

Page 96: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ZooKeeperZooKeeperZooKeeperEnsemble

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Wednesday, February 16, 2011

Page 97: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ZooKeeperZooKeeperZooKeeperEnsemble

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaDashboard

AkkaStatistics

Server

Wednesday, February 16, 2011

Page 98: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

ZooKeeperZooKeeperZooKeeperEnsemble

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaDashboard

AkkaStatistics

Server

Wednesday, February 16, 2011

Page 99: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

   //  Ping  Pong  messages    @serializable  sealed  trait  PingPong    case  object  Ball  extends  PingPong    case  object  Stop  extends  PingPong

Example: Clustered Actors

Wednesday, February 16, 2011

Page 100: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 @serializable  class  PingActor  extends  Actor  {      var  count  =  0

     def  receive  =  {          case  Ball  =>              if  (count  <  NrOfPings)  {                  println("-­‐-­‐-­‐-­‐>>  PING  (%s)"  format  count)                  count  +=  1                  self  reply  Ball              }  else  {                  self.sender.foreach(_  !!  Stop)                  self.stop              }      }  }

Example: Clustered Actors

Wednesday, February 16, 2011

Page 101: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 @serializable  class  PongActor  extends  Actor  {      def  receive  =  {          case  Ball  =>              self  reply  Ball          case  Stop  =>              self.stop      }  }

Example: Clustered Actors

Wednesday, February 16, 2011

Page 102: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 Cluster.startLocalCluster()

 val  localNode      =  Cluster.newNode(NodeAddress(CLUSTER_NAME,  "node0",  port  =  9991)).start  val  remoteNodes  =  Cluster.newNode(NodeAddress(CLUSTER_NAME,  "node1",  port  =  9992)).start  ::                                      Cluster.newNode(NodeAddress(CLUSTER_NAME,  "node2",  port  =  9993)).start  ::                                      Cluster.newNode(NodeAddress(CLUSTER_NAME,  "node3",  port  =  9994)).start  ::                                      Cluster.newNode(NodeAddress(CLUSTER_NAME,  "node4",  port  =  9995)).start  ::  Nil

Example: Clustered Actors

Wednesday, February 16, 2011

Page 103: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Example: Clustered Actors

Wednesday, February 16, 2011

Page 104: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])

Example: Clustered Actors

Wednesday, February 16, 2011

Page 105: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

Example: Clustered Actors

Wednesday, February 16, 2011

Page 106: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head

Example: Clustered Actors

Wednesday, February 16, 2011

Page 107: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head  

Example: Clustered Actors

Wednesday, February 16, 2011

Page 108: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head    val  pong  =  localNode.ref(

Example: Clustered Actors

Wednesday, February 16, 2011

Page 109: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head    val  pong  =  localNode.ref(      actorId  =  PONG_SERVICE,  router  =  Router.RoundRobin)

Example: Clustered Actors

Wednesday, February 16, 2011

Page 110: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head    val  pong  =  localNode.ref(      actorId  =  PONG_SERVICE,  router  =  Router.RoundRobin)

 implicit  val  replyTo  =  Some(pong)  //  set  the  reply  address

Example: Clustered Actors

Wednesday, February 16, 2011

Page 111: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

 localNode  store  (classOf[PingActor])  localNode  store  (classOf[PongActor],  5)

 val  ping  =  localNode.use[PingActor](actorId  =  PING_SERVICE).head    val  pong  =  localNode.ref(      actorId  =  PONG_SERVICE,  router  =  Router.RoundRobin)

 implicit  val  replyTo  =  Some(pong)  //  set  the  reply  address  ping  !  Ball                                              //  serve

Example: Clustered Actors

Wednesday, February 16, 2011

Page 112: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Deploy as dependency JAR in WEB-INF/lib etc. Run as stand-alone microkernel OSGi-enabled; drop in any OSGi container (Spring DM server, Karaf etc.)

How to run it?

Wednesday, February 16, 2011

Page 113: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

AMQP

Dataflow

Security

...and much much more

HTTP

Guice

OSGiPubSub

JTA

FSM

Persistence

STM

Spring

Camel

Wednesday, February 16, 2011

Page 114: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

GroovyJRuby

Use Akka from

JavaScala

Wednesday, February 16, 2011

Page 115: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Akka

1.0Released today

Wednesday, February 16, 2011

Page 116: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Get it and learn morehttp://akka.io

Wednesday, February 16, 2011

Page 117: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

EOFWednesday, February 16, 2011

Page 118: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

Akka Camel

Wednesday, February 16, 2011

Page 119: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  MyConsumer  extends  Actor  with  Consumer  {    def  endpointUri  =  "file:data/input"      def  receive  =  {        case  msg:  Message  =>              log.info("received  %s"  format            msg.bodyAs(classOf[String]))    }}

Camel: consumer

Wednesday, February 16, 2011

Page 120: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  MyConsumer  extends  Actor  with  Consumer  {    def  endpointUri  =          "jetty:http://0.0.0.0:8877/camel/test"      def  receive  =  {        case  msg:  Message  =>              reply("Hello  %s"  format                          msg.bodyAs(classOf[String]))    }}

Camel: consumer

Wednesday, February 16, 2011

Page 121: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

class  CometProducer      extends  Actor  with  Producer  {        def  endpointUri  =          "cometd://localhost:8111/test"}

Camel: producer

Wednesday, February 16, 2011

Page 122: Scalable Solutions - Jfokus€¦ · Akka is the platform for the next generation event-driven, scalable and fault-tolerant architectures on the JVM Simpler Concurrency Event-driven

val  producer  =  actorOf[CometProducer].start

val  time  =  "Current  time:  "  +  new  Dateproducer  !  time

Camel: producer

Wednesday, February 16, 2011