+ All Categories
Home > Technology > Akka: Distributed by Design - Björn Antonsson (Typesafe)

Akka: Distributed by Design - Björn Antonsson (Typesafe)

Date post: 06-May-2015
Category:
Upload: jaxlondonconference
View: 1,151 times
Download: 3 times
Share this document with a friend
Description:
Presented at JAX London 2013 Akka is a toolkit and runtime for building highly scalable, distributed, and fault tolerant reactive applications on the JVM using actors. Akka supports scaling both UP (utilizing multi-core processors) and OUT (utilizing the grid/cloud) hence it is "Distributed by Design". This gives the Akka runtime the freedom to do adaptive automatic load-balancing and actor migration, cluster rebalancing, replication and partitioning. All this is made possible through Akkas new decentralized P2P, gossip-based cluster module.
60
Akka - Distributed by Design Björn Antonsson @bantonsson Wednesday, 30 October 13
Transcript
Page 1: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Akka - Distributed by DesignBjörn Antonsson

@bantonsson

Wednesday, 30 October 13

Page 2: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Overview

• Akka• Actors• Distributed by Design• Diving Into The Cluster• What The Future Brings

Wednesday, 30 October 13

Page 3: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Akka

Wednesday, 30 October 13

Page 4: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Akka

• Toolkit and runtime for reactive applications• Write applications that are– Concurrent– Distributed– Fault tolerant– Event-driven

Wednesday, 30 October 13

Page 5: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Akka

• Has multiple tools– Actors– Futures– Dataflow– Remoting– Clustering

Wednesday, 30 October 13

Page 6: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Actors

Wednesday, 30 October 13

Page 7: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Actors

• Isolated lightweight event-based processes• Share nothing• Communicate through async messages• Each actor has a mailbox (message queue)• Each actor has a parent handling its failures• Location transparent (distributable)

Wednesday, 30 October 13

Page 8: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Actors

• An island of sanity in a sea of concurrency• Everything inside the actor is sequential– Processes one message at a time

• Very lightweight– Create millions– Create short lived

• Inherently concurrent

Wednesday, 30 October 13

Page 9: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Actor code sample

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; }}

public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); int counter = 0;

public void onReceive(Object message) { if (message instanceof Greeting) { counter++; log.info("Hello #" + counter + " " + ((Greeting) message).who); } else unhandled(message); }}

Define the message(s) the Actor should be able to respond to

Define the Actor class

Define the Actor’s behavior

Wednesday, 30 October 13

Page 10: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Creating and using Actors

ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( Props.create(GreetingActor.class), "greeter");

greeter.tell(new Greeting("Charlie Parker"), null);

Wednesday, 30 October 13

Page 11: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Actors compared to Objects

• Think of an Actor as an Object• You can't peek inside it• You don't call methods– You send messages (asynchronously)

• You don't get return values– You receive messages (asynchronously)

• The internal state is thread safe

Wednesday, 30 October 13

Page 12: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Why should I care?

Wednesday, 30 October 13

Page 13: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

The world is multicore!

Wednesday, 30 October 13

Page 14: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Amdahl’s Law

Wednesday, 30 October 13

Page 15: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

So what's the catch?

• Really no catch• A different programming paradigm• All about tradeoffs– Some things are easier some harder

• Think different

Wednesday, 30 October 13

Page 16: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Distributed by Design

Wednesday, 30 October 13

Page 17: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Remote Actors

• Sending messages decouples actors• Local or remote doesn't matter

Wednesday, 30 October 13

Page 18: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

NODE 1 NODE 2

Wednesday, 30 October 13

Page 19: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Remote Actors

• Zero code change deployment decision• Add configuration to the Actor Systemakka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka.tcp://MySystem@machine1:2552 } } }}

Configure a Remote Provider

Define Remote Path Protocol Actor System Hostname Port

The "greeter" actor

Wednesday, 30 October 13

Page 20: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Looking up Actors

ActorSelection greeter = system.actorSelection( "akka.tcp://MySystem@machine1:2552/user/greeter");

Wednesday, 30 October 13

Page 21: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Can you see the problem?

Wednesday, 30 October 13

Page 22: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Fixed addressesakka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /greeter { remote = akka.tcp://MySystem@machine1:2552 } } }}

ActorSelection greeter = system.actorSelection( "akka.tcp://MySystem@machine1:2552/user/greeter");

Wednesday, 30 October 13

Page 23: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Diving Into The Cluster

Wednesday, 30 October 13

Page 24: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Akka Cluster 2.2

• Gossip-Based Cluster Membership• Failure Detector• Cluster DeathWatch• Cluster-Aware Routers

Wednesday, 30 October 13

Page 25: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Membership

• Node ring à la Riak / Dynamo• Gossip-protocol for state dissemination• Vector Clocks to resolve conflicts• Peer based failure detector

Wednesday, 30 October 13

Page 26: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Node ring with gossiping Members

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

Gossip

Wednesday, 30 October 13

Page 27: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Gossip Protocol

• Cluster Membership• Leader Determination• Targets Random Node– Partly biased towards nodes with older state

• Push-Pull based– Sender only sends his version number– Receiver asks for newer information

Wednesday, 30 October 13

Page 28: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Vector Clocks

• Partial ordering in a distributed system• Detects causality violations• Used to reconcile and merge cluster state

Wednesday, 30 October 13

Page 29: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Failure Detection

• Uses The Phi Accrual Failure Detector• Peer Based with limited targets– B monitors A– A sends heart beats to B– B samples inter-arrival time to expect next beat– B measures continuum of deadness of A– B marks A as unreachable if A is dead enough

Wednesday, 30 October 13

Page 30: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

Heartbeat

Wednesday, 30 October 13

Page 31: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

MemberNode

Heartbeat

Wednesday, 30 October 13

Page 32: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Member States

• Joining• Up• Leaving• Exiting• Down• Removed• Unreachable*

unreachable*

joining

up

leaving

exitingdown

(leader action)

(fd*)

(fd*)

(fd*)

(fd*)

leave

(leader action)

(leader action)

join

removed

Wednesday, 30 October 13

Page 33: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Leader

• No SPOF• Can be any node• No handover involved• Deterministically recognized by all nodes – always the first member in the sorted

membership ring

Wednesday, 30 October 13

Page 34: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Leader Duties

• Shift members from– Joining to Up– Exiting to Removed– Up to Down (auto-downing) to Removed

• Can only be performed on convergence

Wednesday, 30 October 13

Page 35: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Convergence

• A Node sees that all other Nodes have seen this version of the gossip

• Is always local to that Node• Unreachable Nodes blocks this• Mark Unreachable Nodes as Down to proceed– Manual Ops intervention– Automatic action

Wednesday, 30 October 13

Page 36: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Metrics

• Gossip based• Metrics about the Nodes in the Cluster– Load– CPU Usage– Processors– Heap Memory• Used, Committed, Max

Wednesday, 30 October 13

Page 37: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Roles

• Assign roles to Nodes (named tags)• Nodes can have multiple roles• Restrict work to certain roles• Deterministically recognized role leader

Wednesday, 30 October 13

Page 38: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Events

• Subscribe to be notified• Membership Changes– Up, Exited, Removed

• Leader Changed• Metrics Changed• Role Leader Changed• Member Unreachable

Wednesday, 30 October 13

Page 39: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster DeathWatch

• Triggered by marking node «A» Down– Tell parents of their lost children on «A»– Kill all children of actors on «A»– Send Terminated for actors on «A»

Wednesday, 30 October 13

Page 40: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Building on The Cluster

Wednesday, 30 October 13

Page 41: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Load Balancing

• Cluster aware routers– Round Robin Router– Consistent Hashing Router– Adaptive Load Balancing Router• Use Cluster Metrics to select target• CPU/Memory/Load

Wednesday, 30 October 13

Page 42: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Cluster Contributions/Patterns

• Distributed Pub/Sub Mediator– Publish and Subscribe to message flows

• Cluster Singleton– HA singleton actor instance within the cluster

• Cluster Client– Let other systems connect to the cluster

Wednesday, 30 October 13

Page 43: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

DistributedPubSubMediator

Frontend Master

Mediator Mediator

Wednesday, 30 October 13

Page 44: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

DistributedPubSubMediator

Frontend Master

Mediator Mediator

Put

Wednesday, 30 October 13

Page 45: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

DistributedPubSubMediator

Frontend Master

Mediator Mediator

Send

Wednesday, 30 October 13

Page 46: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

DistributedPubSubMediator

Frontend Master

Mediator Mediator

Send

Wednesday, 30 October 13

Page 47: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

ClusterSingleton

ClusterSingletonManager

ClusterSingletonManager

MasterMaster(Standby)

Wednesday, 30 October 13

Page 48: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

ClusterSingleton

ClusterSingletonManager

ClusterSingletonManager

MasterMaster(Standby)

Wednesday, 30 October 13

Page 49: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

ClusterSingleton

ClusterSingletonManager

ClusterSingletonManager

Master Master

Wednesday, 30 October 13

Page 50: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

ClusterClient & ClusterSingleton

Master

Mediator

Mediator

Receptionist

Master(Standby)

Receptionist

ClusterClient

ClusterClient

Worker

Worker

Wednesday, 30 October 13

Page 51: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Typesafe ActivatorDistributed Workers Cluster Template

• http://typesafe.com/platform/getstarted

Wednesday, 30 October 13

Page 52: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

What The Future Brings

Wednesday, 30 October 13

Page 53: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Gossip Optimizations

• Several times faster Vector Clock comparison• Fewer Vector Clock comparisons• Gossip message size cut in half• Gossip message scrubbing• Lazy deserialization of Gossip messages

Wednesday, 30 October 13

Page 54: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Return from Unreachable

• Unreachableto Reachable

• Cluster ismore resilientto fluctuations

unreachable*

joining

up

leaving

exitingdown

(leader action)

(fd*)

(fd*)

(fd*)

(fd*)

leave

(leader action)

(leader action)

join

removed

Wednesday, 30 October 13

Page 55: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Rebuilt Routers

• Rebuilt from the ground• Routing logic usable in Actors• Actor Selection as routees• Improved Cluster behavior

Wednesday, 30 October 13

Page 56: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Persistence

• New module akka-persistence• Command sourcing & event sourcing• Based on the proven Eventsourced library• Migrate actors by persisting their state

Wednesday, 30 October 13

Page 57: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Resources

Wednesday, 30 October 13

Page 58: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Survey and Resources

• Help Akka get better. Fill out the survey!– http://tinyurl.com/akka-survey

• Akka Cluster Documentation– http://tinyurl.com/akka-cluster

• Akka Cluster in Production Blog Post – Ryan Tanner• http://tinyurl.com/akka-at-conspire

Wednesday, 30 October 13

Page 59: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

@bantonsson

Coursera Course

• Principles of Reactive Programming byMartin Odersky, Erik Meijer and Roland Kuhn– Starts 4th of November 2013– 7 weeks– Workload: 5-7 hours a week– Free as in free beer

• https://www.coursera.org/course/reactive

Wednesday, 30 October 13

Page 60: Akka: Distributed by Design  - Björn Antonsson (Typesafe)

Björn Antonsson@bantonsson @akkateam

[email protected]

Wednesday, 30 October 13


Recommended