Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports...

71
Scaling out with Akka Actors J. Suereth

Transcript of Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports...

Page 1: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Scaling out with Akka Actors

J. Suereth

Page 2: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Agenda

● The problem● Recap on what we have● Setting up a Cluster●● Advanced Techniques

Page 3: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Who am I?

● Author Scala In Depth, sbt in Action● Typesafe Employee● Big Nerd

Page 4: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

ScalaDaysJUNE 10TH-12TH 2013, NYC

Page 5: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

The new web

● EVENT DRIVEN● ASYNCHRONOUS● DATA-DRIVEN● BIG DATA● SINGLE PAGE DESIGN● COMPOSITION OF SERVICES● DISTRIBUTED● REACTIVE

Page 6: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

The new web

● EVENT DRIVEN● ASYNCHRONOUS● DATA-DRIVEN● BIG DATA● SINGLE PAGE DESIGN● COMPOSITION OF SERVICES● DISTRIBUTED● REACTIVE

Page 7: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

The problemI can't scale my website

Page 8: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

The Hotel Search Business

Page 9: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

ArchitectureW

eb L

ayer

Ser

vice

Lay

er

Dat

a La

yer

DBMS

Page 10: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Architecture

Web

Lay

er

Ser

vice

Lay

er

Dat

a La

yer DBMS

Web

Lay

er

Ser

vice

Lay

er

Dat

a La

yer

Page 11: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Architecture

DBMS

Page 12: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Architecture

DBMS

Page 13: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

So... we built a Search system that● Finds hotels ● Dynamically grows the search index● Caches previous query results for some time● Detects system overload and returns a cute

animal drawing

Page 14: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Our Current ArchitectureW

eb L

ayer

Ser

vice

Lay

er

Dat

a La

yer

DBMS

Search Index

Page 15: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Let's dig intothe Search Index

Page 16: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Front-End

Backend

Current Actor Layout

CacheThrottle

Serv

ice

Laye

r

Page 17: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Scatter Gather Search Index

● Split documents into Topics○ Create a "leaf" actor for each topic.○ Topic actors have local index

● Categories○ Group topics into categories○ Group categories into more categories○ one root○ delegate queries to topics○ aggregate results

● Dynamically Expands○ Topics can decide to split into categories and sub-

topics

Page 18: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Front End

● Query Cache○ Caches top N query results○ (Not in sample code) Evicts stale cache○ Primary source of speedup!

● Throttler○ Records average query-response-time○ When in "failure" mode, prevents queries from

hitting the system and returns 'failure' response.

Page 19: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Let's remember....

Page 20: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Scatter Phase

Topic 1 Topic 2 Topic 3 Topic 4

Category 1 Category 2

Root

Query

Page 21: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Gather Phase

Topic 1 Topic 2 Topic 3 Topic 4

Category 1 Category 2

Root

Results

Page 22: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Actual Actors

Topic 1 Topic 2 Topic 3 Topic 4

Category 1 Category 2

Root

gatherer

gatherer

gatherer

Page 23: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creates

Throttling

Throttler

Statistics Service

Query

Interceptor

Page 24: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Throttling

Throttler

Statistics Service

Query

Interceptor

Query

Response

Page 25: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Statistics

Throttling

Throttler

Statistics Service

InterceptorResponse

Response

Page 26: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creates

Throttling - Timeouts

Throttler

Statistics Service

Query

Interceptor

Page 27: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Throttling - Timeouts

Throttler

Statistics Service

Query

Interceptor

Query

Timeout

Page 28: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Statistics

Throttling - Timeouts

Throttler

Statistics Service

Interceptor

Response

Page 29: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

FAIL

Statistics

Throttling - Timeouts

Throttler

Statistics Service

Interceptor

Response

Throttler

Page 30: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Throttling - Dropping Queries

Throttler

Statistics Service

Throttler

Query

Response

Page 31: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Throttling - Recovery

Throttler

Statistics Service

Throttler

Timeout

Allow

Page 32: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

What now?

We installed our Search Tree on a huge-mongous server, and it's sucking up all 128GB RAM, and all 24 cores!

.... It's time to scale out

Page 33: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Remember we tried...

DBMS

Web

Lay

er

Ser

vice

Lay

er

Dat

a La

yer

Search Index

Web

Lay

er

Ser

vice

Lay

er

Dat

a La

yer

Page 34: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Now we want

DBMS

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Cluster Node

Spring Application Akka Search Index Node

Ria

k

Page 35: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Using Akka Clustering

● Akka now supports automatic cluster membership and notification○ Considered experimental in 2.1○ We're using 2.2-M2 for this talks

● Let's identify portions of our application and how we can scale them out

Page 36: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Setting up an Akka Cluster

Page 37: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Your BuildlibraryDependencies ++= Seq( "com.typesafe.akka" %% "akka-actor" % "2.2-M2", "com.typesafe.akka" %% "akka-cluster-experimental" % "2.2-M2")

<dependency>

<groupId>com.typesafe.akka</groupId>

<artifactId>akka-actor-${scala.version}</artifactId>

<version>2.2-M2</version>

</dependency>

<dependency>

<groupId>com.typesafe.akka</groupId>

<artifactId>akka-cluster-experimental-${scala.version}</artifactId>

<version>2.2-M2</version>

</dependency>

Page 38: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Application Configurationakka {

actor {

provider = "akka.cluster.ClusterActorRefProvider"

}

remote {

log-remote-lifecycle-events = off

netty.tcp {

hostname = "127.0.0.1"

port = 0

}

}

cluster {

seed-nodes = [

"akka.tcp://[email protected]:2551" ,

"akka.tcp://[email protected]:2552" ]

auto-down = on

}

}

Actor references become cluster-ified

Nodes we look for to join the cluster

Page 39: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Code

val system = ActorSystem("ClusterSystem")

Page 40: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Front-End

Backend

Remember the Actor Layout

CacheThrottle

Page 41: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Step #1Let's automatically generate throttle and cache on

every cluster node.

Page 42: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creation code unchanged

system.actorOf(Props[FrontEnd]), "search-front-end")

This runs on every cluster node where we want a frontend

Page 43: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Registration on the FrontEndcase class RegisterSearchTree(tree: ActorRef)

class FrontEnd extends Actor with ...{ ... def receive: Receive = { case RegisterSearchTree(tree) => // Now we create the cache + throttler }} The backend will now tell the

frontend where it is, as each frontend cluster member registers.

Page 44: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Create Cluster-Aware Backendclass TreeTop .. extends Actor {

val searchTree: ActorRef = createSearchTree()

val cluster = Cluster(context.system)

override def preStart(): Unit = cluster.subscribe(self, classOf[MemberUp])

override def postStop(): Unit = cluster.unsubscribe(self)

...}

A new "top" on the scatter-gather tree registers for cluster membership events

Page 45: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Create Cluster-Aware Backend def receive: Receive = {

case q: SearchQuery => searchTree.tell(q, sender)

case h: AddHotel => searchTree.tell(h, sender)

case MemberUp(member) => val memberFrontEnd = context.actorFor( RootActorPath(member.address) / "user" / "search-front-end") memberFrontEnd ! RegisterTree(self) }

Notify the local "search-front-end" when a member joins the cluster

Page 46: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

What we have now

cluster member #1

top

cluster member #2

front-end

MemberUp

MemberUp

RegisterTree

Cache Throttle

Page 47: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Just one node?

MemberUp message is still fired, so front end still finds the back end.

Page 48: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Recap #1

Can use Cluster membership notifications to register important services with each other.

Page 49: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Step #2Ensure the Search Tree can survive node failure

Page 50: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Cluster Singleton Pattern

● Construct a Manager on every cluster node● Managers communicate and elect a "leader"● On leader failure, a new leader is chosen● Create local proxy actor who keeps track of

where the leader is.● Issues

○ Bottleneck○ Delay in failure recovery (single point of failure)

See: http://doc.akka.io/docs/akka/snapshot/contrib/cluster-singleton.html

Page 51: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creating the Singletonimport akka.contrib.pattern.ClusterSingletonManager

system.actorOf(Props( new ClusterSingletonManager( singletonProps = _ => Props(new NodeManager("top", db)), singletonName = "search-tree", terminationMessage = PoisonPill, role = None)), name = "singleton")

Page 52: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creating the Singletonimport akka.contrib.pattern.ClusterSingletonManager

system.actorOf(Props( new ClusterSingletonManager( singletonProps = _ => Props(new NodeManager("top", db)), singletonName = "search-tree", terminationMessage = PoisonPill, role = None)), name = "singleton")

Page 53: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creating the Singletonimport akka.contrib.pattern.ClusterSingletonManager

system.actorOf(Props( new ClusterSingletonManager( singletonProps = _ => Props(new NodeManager("top", db)), singletonName = "search-tree", terminationMessage = PoisonPill, role = None)), name = "singleton")

Page 54: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creating the Proxyclass TreeTopProxy extends Actor { val cluster = Cluster(context.system)

override def preStart(): Unit = cluster.subscribe(self, classOf[LeaderChanged])

override def postStop(): Unit = cluster.unsubscribe(self)

var leaderAddress: Option[Address] = None ...

Page 55: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Creating the Proxy (part 2) ... def receive = { case state: CurrentClusterState => leaderAddress = state.leader case LeaderChanged(leader) => leaderAddress = leader case msg => singleton foreach { _ forward msg } } def singleton: Option[ActorRef] = leaderAddress map (a => context.actorFor(RootActorPath(a) / "user" / "singleton" / "search-tree"))}

Page 56: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Visualizing

cluster member #1 cluster member #2

Proxy ProxyManager Manager

LeaderChanged

lookup

lookup

Page 57: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Visualizing

cluster member #1 cluster member #2

Proxy ProxyManager Manager

LeaderChanged

lookuplookup

Stop

Page 58: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Step #3Fragment the Search Tree

Page 59: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

We still have scaling issues

cluster member #1 (LEADER)

Cache / Throttle TreeProxy

cluster member #2

Cache / Throttle TreeProxy

cluster member #3

Cache / Throttle TreeProxy

Page 60: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

What are routers?

● Layer between ActorRef / Actors● Route messages to underlying actors● Non-Cluster Examples:

○ Round Robin○ Scatter Gather (first-found)○ Consistent Hashing○ Random○ Broadcast

Page 61: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Tree with local routers

Category

router router router

Topic-1(i-1)

Topic-1(i-2)

Topic-2(i-1)

Topic-2(i-2)

Topic-3(i-1)

Topic-3(i-2)

Page 62: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Clustered Router

Like local routers, but actor instances may be on other nodes.

Page 63: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Clustered Routerprops.withRouter( ClusterRouterConfig( BroadcastRouter(1), ClusterRouterSettings( totalInstances = 3, maxInstancesPerNode = 1, allowLocalRoutees = true, useRole = None ) ))

Local Router

Cluster Router

Page 64: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Tree with remote routers

Category

router router router

Topic-1(i-1) Topic-1

(i-2)

Topic-2(i-1)

Topic-2(i-2)

Topic-3(i-1)

Topic-3(i-2)

Page 65: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Metrics based Routing

● Requires "sigar" dependency to enable● Examples:

○ AdaptiveLoadBalancingRouter■ heap■ cpu■ load■ mix

Page 66: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

RecapClustered system design with Actors

Page 67: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Actor Systems

● Partition state into small pieces● Communicate with immutable messages● Spawn new actors to track temporary state● Design as a Topology● Partition threads on the topology● Bubble errors on the topology

Page 68: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Clustered Actor Systems

● Partition Topology on nodes in the cluster○ Limit instances with routers○ Register with other clusters using cluster listeners○ Use roles to fragment actors across the cluster○ Keep "singleton" actors on the leader or role leader

● Avoid excessive inter-node messaging○ Use statistics based routing○ Fragment in 'large pieces'

● Allow time for cluster convergence and fault detection

Page 69: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Key PointEnsure your system can recover from failure

Page 70: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Resources

● http://github.com/jsuereth/intro-to-actorsExample code (clusters branch)

● http://akka.ioAkka concurrency framework for the JVM

Page 71: Akka Actors Scaling out with - chariotsolutions.com€¦ · Using Akka Clustering Akka now supports automatic cluster membership and notification Considered experimental in 2.1 We're

Questions?