63
Above the Clouds: Introducing Akka Fredrik Ekholdt Typesafe Email: [email protected] Twitter: @ekhfre Wednesday, November 23, 11

Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Above the Clouds:Introducing Akka

Fredrik EkholdtTypesafe

Email: [email protected] : @ekhfre

Wednesday, November 23, 11

Page 2: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

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, November 23, 11

Page 3: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Introducing

Wednesday, November 23, 11

Page 4: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Introducing

Akka (Áhkká):

The name comes from the goddess in the Sami mythology that represented all the wisdom and

beauty in the world.

It is also the name of a beautiful mountain in Laponia in the north part of Sweden

Wednesday, November 23, 11

Page 5: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Introducing

View from the summit

Wednesday, November 23, 11

Page 6: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Vision

Simpler

Concurrency

Scalability

Fault-tolerance

Wednesday, November 23, 11

Page 7: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

CORE SERVICES

THE TOOLBOX...

Wednesday, November 23, 11

Page 8: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

The Actor...

Wednesday, November 23, 11

Page 9: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Behavior

State

Actor

Wednesday, November 23, 11

Page 10: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 11: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 12: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 13: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 14: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 15: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Behavior

State

Actor

Wednesday, November 23, 11

Page 16: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Event-driven

Behavior

State

Actor

Wednesday, November 23, 11

Page 17: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka ActorsLet the hakking begin!

Text

Wednesday, November 23, 11

Page 18: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

•Create an Actor: class AnActor extends Actor•Start an Actor: actorOf[AnActor].start()•Safe

•Encapsulated state•One message at a time

•Asynchronous:•bang (!)

•Reply: •self.reply ( m )•actor ? “message”

Take aways

Wednesday, November 23, 11

Page 19: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Futureswithout blocking

using

Wednesday, November 23, 11

Page 20: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

FuturePromise

Future with value

Wednesday, November 23, 11

Page 21: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

FuturePromise

Future with value

Wednesday, November 23, 11

Page 22: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

FuturePromise

Future with value

Wednesday, November 23, 11

Page 23: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

•Use Futures when you need something in the Future without blocking

•Use onComplete•Compose Futures

•for - comprehensions•Check out the Futures utilities (firstCompletedOf

and much, much more)

Take aways

Wednesday, November 23, 11

Page 24: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Remote ActorsAnother snippet of code...

Wednesday, November 23, 11

Page 25: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

New Remote Actors

Wednesday, November 23, 11

Page 26: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

appContext.actorOf[MyActor](“my-service”)

Name

Bind the actor to a name

Wednesday, November 23, 11

Page 27: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

•Actor name is virtual and decoupled from how it is deployed

•If no deployment configuration exists then actor is deployed as local

•The same system can be configured as distributed without code change (even change at runtime)

•Write as local but deploy as distributed in the cloud without code change

•Allows runtime to dynamically and adaptively change topology

Deployment

Wednesday, November 23, 11

Page 28: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

akka { actor { deployment { /path/to/my-service { router = "round-robin" nr-of-instances = 3 remote {

nodes = ["wallace:2552", "gromit:2552"] } } } }}

Deployment configuration

Wednesday, November 23, 11

Page 29: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

•Decentralized P2P gossip-based cluster membership (dynamo-style w/ vector clocks, hand-off on fail-over etc.)

•Automatic adaptive cluster rebalancing•Automatic cluster-wide deployment•Highly available configuration service•Automatic replication with automatic fail-over upon

node crash•Transparent and user-configurable load-balancing•...and much more

The runtime provides

Wednesday, November 23, 11

Page 30: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Node

Wednesday, November 23, 11

Page 31: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Nodeval ping = actorOf[Ping](“ping”)val pong = actorOf[Pong](“pong”)

pong ! Ball(ping)

Wednesday, November 23, 11

Page 32: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Nodeval ping = actorOf[Ping](“ping”)val pong = actorOf[Pong](“pong”)

pong ! Ball(ping)

Ping Pong

Wednesday, November 23, 11

Page 33: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Node

AkkaCluster Node

Ping Pong

Wednesday, November 23, 11

Page 34: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Akka Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Wednesday, November 23, 11

Page 35: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Wednesday, November 23, 11

Page 36: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

akka { actor { deployment { /ping {} /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Wednesday, November 23, 11

Page 37: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

akka { actor { deployment { /ping {} /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping

Pong

Wednesday, November 23, 11

Page 38: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

akka { actor { deployment { /ping {} /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping

Pong

Pong

PongWednesday, November 23, 11

Page 39: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Summingup

Wednesday, November 23, 11

Page 40: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Let it crash fault-tolerance

Wednesday, November 23, 11

Page 41: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

...let’s take a standard OO application

Wednesday, November 23, 11

Page 42: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Wednesday, November 23, 11

Page 43: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Which components have critically important state

and explicit error handling?

Wednesday, November 23, 11

Page 44: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Wednesday, November 23, 11

Page 45: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Fault-tolerant onion-layered Error Kernel

Wednesday, November 23, 11

Page 46: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 47: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 48: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 49: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 50: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 51: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ErrorKernel

Wednesday, November 23, 11

Page 52: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Node 1 Node 2

Wednesday, November 23, 11

Page 53: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

// from within an actorcontext.actorOf[MyActor]

transparent and automatic fault handling by design

Parental automatic supervision

Wednesday, November 23, 11

Page 54: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

A

B

BarFoo

C

BE

A

D

C

Guardian Actor

Parental automatic supervision

Wednesday, November 23, 11

Page 55: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

A

B

BarFoo

C

BE

A

D

C

Guardian Actor

/Foo

Parental automatic supervision

Wednesday, November 23, 11

Page 56: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

A

B

BarFoo

C

BE

A

D

C

Guardian Actor

/Foo

/Foo/A

Parental automatic supervision

Wednesday, November 23, 11

Page 57: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

A

B

BarFoo

C

BE

A

D

C

Guardian Actor

/Foo

/Foo/A

/Foo/A/B

Parental automatic supervision

Wednesday, November 23, 11

Page 58: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Supervisionclass MySupervisor extends Actor { faultHandler = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate

   },   maxNrOfRetries = None,   withinTimeRange = None)

def receive = { case NewUser(name) => ... = context.actorOf[User](name) }}

Wednesday, November 23, 11

Page 59: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Supervisionclass MySupervisor extends Actor { faultHandler = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate

   },   maxNrOfRetries = None,   withinTimeRange = None)

def receive = { case NewUser(name) => ... = context.actorOf[User](name) }}

AllForOneStrategy

Wednesday, November 23, 11

Page 60: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

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

Manage failure

Wednesday, November 23, 11

Page 61: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

ARCHITECTURE

TYPESAFE STACK

ADD-ONS

Wednesday, November 23, 11

Page 62: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

Get it and learn morehttp://akka.io

http://typesafe.com

https://gist.github.com/1388237

Wednesday, November 23, 11

Page 63: Above the Clouds: Introducing Akka - GOTO Conferencegotocon.com/dl/goto-prague-2011/slides/FredrikEkholdt... · 2011-11-23 · Introducing Akka (çhkk ): The name comes from the goddess

EOFWednesday, November 23, 11