effective akka v2.0 - jamie allen - jax london | the ... · effective akka goal communicate as much...

58
Jamie Allen Sr. Director of Global Services Effective v2.0

Upload: others

Post on 31-Oct-2019

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

Jamie AllenSr. Director of Global Services

Effectivev2.0

Page 2: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

@jamie_allen

Reac%ve'Applica%ons' 2'

Page 3: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Goal

CommunicateasmuchaboutwhatI’velearnedin6+yearsofactordevelopmentwithinonehour

WewillstartwithacursoryoverviewofwhatActorsareandtheproblemstheysolve,thenmoveintosomereal-worldusecasesandhowtotestthem

3

Page 4: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

What are Actors?• An abstraction over the primitives of concurrency, asynchrony and

resilience

• The embodiment of single-threaded interactions happening concurrently and asynchronously across all of the resources available to your application

• Finer-grained fault tolerance than thread pool utilities like UncaughtExceptionHandler

• Location transparency in support of greater asynchrony and resilience

4

Page 5: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

What are Actors?• Actors never interact with each other via synchronous method calls,

only messages

• Actors can be domain instances:

• A “customer” representation, live in memory

• Messages arrive and tell them how the world is changing around them

• Actors can be workers:

• Encapsulate no state, they just know what to do when they get messages that have data in them

5

Page 6: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

What are Actors?• Should actors be used everywhere?

• Probably not, but they make excellent “boundaries”

• Across physical nodes

• Across services

• They’re also excellent for defining strategies to handle failures you understand, as well as those you do not

6

Page 7: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

What are Actors?

7

C2C1

D1 D2 D3Ac2Ac1

A1 A2 A3 A4

Customers

Accounts & Devices

Applications

AS DS

Page 8: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Actors and Pure Functional Programming are NOT Mutually Exclusive

• Pure FP is all about statically-checked correctness of logic, particularly with type safety

• Actors are about resilience to failure beyond the type system

• Distributed systems offer no such guarantees except at the protocol level - how do you verify types of what messages you can send/receive through a message broker?

• There is no type checking for hardware failures or network split brains

• Actors help you cope with problems at this level

8

Page 9: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

USE CASES

Page 10: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Case 1

•AlargecablecompanyneedstostoppingingitsmassivedatabaseforeveryOnDemandwebservicerequestfromsomeoneusingtheirremotecontrol

•Alivecacheof“entitlement”dataisrequiredthatisupdatedquicklywhenacustomertriestochangetheirservice

•MinimaldowntimeisrequiredasOnDemandviewingisoneofthecorporation’slargestprofitcenters

10

Page 11: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

The Time-Based Approach

11

MegaCorp)DB)

Riak)

Message)Queue)

Warehoused)Data)

DB)Update)Handler)

Transformer)

Transformer)

Transformer)

Transformer)

Transformer)

Page 12: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Issues

•Amissedeventmeansthecacheisnowoutofsynchwiththedatabase

•Assumingweevenknowafailurehasoccurred

•Areloadofthecacheforallcustomerswouldbe2.5hours

•Latencyishardertotrackanddependenton“burstiness"ofupdates

•Howdoyourepresentdeletedaccounts?

12

Page 13: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

The Self-Healing Approach

13

MegaCorp)DB)

Riak)

Warehoused)Data)

Account)Retreiver)

Transformer)

Transformer)

Transformer)

Transformer)

Transformer)

Supervisor)

Supervisor)

Page 14: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Wins

•Fixedandtunablelatencyforupdatesdependingonnumberofworkers(andthesizeoftheirbucketsofaccounts)

•Resilienceviasupervision

•Simplerarchitecturewithlessmovingparts

•Neveroutofsynchwithprimarydatabaseforlongerthanthetimeittakestohandlethemaximumsizeofabucketofaccounts

•Riakhandlesaccountsto“delete”automaticallybytombstoningrecordsthathavenotbeenupdatedwithinatimewindow(sessionlengthsetting)

14

Page 15: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Case 2

•Anactorwillreceivearequesttogetalloftheaccountbalancesforacustomer(savings,checkingandmoneymarket)

•Actorshouldnotwaittofinishhandlingonerequestbeforehandlinganother

•Actorshouldreceiveserviceproxiesfromwhichitcanretrieveeachaccount’sinfo

•Actorshouldeithergetresponsesfromallthreewithinaspecifiedtime,orsendatimeoutresponsebacktotherequestor

15

Page 16: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Cameo Pattern

16

•Howtohandleindividualmessagestoanactorwithoutmakingitdoalloftheworkbeforehandlingthenextmessage

•SimilartotheSagaPattern,butwithlessoverheadandrules

Page 17: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Request Aggregation• When an actor handles a message, it frequently has to perform

multiple interactions with other services to provide a valid response

• We do not want the actor that received the message to be tied up performing that work

17

Request Receiver

Message 1 Handler

Message 2 Handler

Message 3 Handler

Message 1

Message 4

Message 2

Message 3

Message 4 Handler

Message 1 Handler

Page 18: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Transactions?

•Couldbe!

•Youhavetowritethelogicofhowtorollbackifanythingfails

•Butyouhavethecontrolandthecontexttodoit,especiallyifyoureffectsaregoingtomultipleexternalplacesordatastores

18

Page 19: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

All Code is on GitHub• I’mgoingtobeshowingsomereasonablycomplexexamples

• Don’tworryabouttryingtocopythecodefromtheslides

http://github.com/jamie-allen/effective_akka

19

Page 20: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Futures and Promises?

•Iprefernotto.Useanotheractorresponsiblefor:

•Capturingthecontext(originalrequestor)

•Defininghowtohandleresponsesfromotherservices

•Definingthetimeoutthatwillraceagainstyour

•EachFutureandtheresultingAskSupporthaveanadditionalcostthatwedonotalwaysneedtopay

•Futuresdomakeanexcellentmechanismforcallingintoanactorworldfromanon-actorcontext

•Futuresarealsomore“composable”andcanhelpdefineaprobleminmoresimpleterms

•NotethatFuturefailurehandlingviacallbacksisnomorecomposablethanTry/Catch

20

Page 21: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Futures and Promises?

def receive = { case GetCustomerAccountBalances(id) => val futSavings = savingsAccounts ? GetCustomerAccountBalances(id) val futChecking = checkingAccounts ? GetCustomerAccountBalances(id) val futMM = moneyMarketAccounts ? GetCustomerAccountBalances(id)

val futBalances = for { savings <- futSavings.mapTo[Option[List[(Long, BigDecimal)]]] checking <- futChecking.mapTo[Option[List[(Long, BigDecimal)]]] mm <- futMM.mapTo[Option[List[(Long, BigDecimal)]]] } yield AccountBalances(savings, checking, mm) futBalances.map(sender ! _) }}

21

Yuck!

Page 22: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Capturing the Sender

•Thisistrickierthanitsounds

•Youneedthe“sender”valuefromtheactorthatreceivedtheoriginalrequest,notthesenderinsideoftheactorhandlingit!

•Oneofthebiggestsourcesofactorbugs

22

Page 23: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Futures and Promises?

def receive = { case GetCustomerAccountBalances(id) => val futSavings = savingsAccounts ? GetCustomerAccountBalances(id) val futChecking = checkingAccounts ? GetCustomerAccountBalances(id) val futMM = moneyMarketAccounts ? GetCustomerAccountBalances(id)

val futBalances = for { savings <- futSavings.mapTo[Option[List[(Long, BigDecimal)]]] checking <- futChecking.mapTo[Option[List[(Long, BigDecimal)]]] mm <- futMM.mapTo[Option[List[(Long, BigDecimal)]]] } yield AccountBalances(savings, checking, mm) futBalances.map(sender ! _) }

23

Bug!

Page 24: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Futures and Promises?

def receive = { case GetCustomerAccountBalances(id) => val futSavings = savingsAccounts ? GetCustomerAccountBalances(id) val futChecking = checkingAccounts ? GetCustomerAccountBalances(id) val futMM = moneyMarketAccounts ? GetCustomerAccountBalances(id)

val futBalances = for { savings <- futSavings.mapTo[Option[List[(Long, BigDecimal)]]] checking <- futChecking.mapTo[Option[List[(Long, BigDecimal)]]] mm <- futMM.mapTo[Option[List[(Long, BigDecimal)]]] } yield AccountBalances(savings, checking, mm) futBalances.pipeTo(sender) }

24

Fixed!

Page 25: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

The Actor Approach• Use an actor to encapsulate a context, such as a specific user request

• Define the values you need to retrieve/transform

• Define the behavior for what to do when you get them, or if you only get partial responses (transaction management)

• Define the response to the original requester and SHUT DOWN THE ACTOR

• Send the messages to start the work

• Set up the single competing timeout message send

25

Page 26: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use an Anonymous Actor?class OuterActor extends Actor def receive = LoggingReceive { case DoWork => { val originalSender = sender

context.actorOf(Props(new Actor() { def receive = LoggingReceive { case HandleResponse(value) => timeoutMessager.cancel sendResponseAndShutdown(Response(value)) case WorkTimeout => sendResponseAndShutdown(WorkTimeout) }

def sendResponseAndShutdown(response: Any) = { originalSender ! response context.stop(self) }

someService ! DoWork

import context.dispatcher val timeoutMessager = context.system.scheduler.scheduleOnce(250 milliseconds) { self ! WorkTimeout } })) } }}

26

AnonymousActor}

Page 27: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use an Anonymous Actor?

•Icallthisthe“Extra”Pattern

•It’snotbad,butithasdrawbacks:

•Poorstacktracesdueto“namemangled”actornames,like$a

•Moredifficulttomaintaintheclutteredcode,anddevelopershavetoreadthroughthebodyoftheanonymousactortofigureoutwhatitisdoing

•Morelikelyto“closeover”state

27

Page 28: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Create a “Cameo” Actor

•Externalizethebehaviorofsuchananonymousactorintoaspecifictype

•Anyonemaintainingthecodenowhasatypenamefromwhichtheycaninferwhattheactorisdoing

•Mostimportantly,youcan’tcloseoverstatefromanenclosingactor-itmustbepassedexplicitly

28

Page 29: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Create a “Cameo” Actorclass WorkerActor(dependency: ActorRef) extends Actor { def receive = LoggingReceive { case HandleResponse(value) => timeoutMessager.cancel sendResponseAndShutdown(Response(value)) case WorkTimeout => sendResponseAndShutdown(WorkTimeout) }

def sendResponseAndShutdown(response: Any) = { originalSender ! response context.stop(self) }

// Send request(s) required dependency ! GetData(1L)

import context.dispatcher val timeoutMessager = context.system.scheduler.scheduleOnce( 250 milliseconds, self, WorkTimeout)}

class DelegatingActor extends Actor def receive = LoggingReceive { case DoWork => { val originalSender = sender val worker = context.actorOf(WorkerActor.props(), “worker”) someService.tell(DoWork, worker) } }}

29

Page 30: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Remember to Stop the Actor•Whenyouarefinishedhandlingarequest,ensurethattheactorusedisshutdown

• Thisisabigmemoryleakifyoudon’t

def sendResponseAndShutdown(response: Any) = { originalSender ! response log.debug("Stopping context capturing actor") context.stop(self) }

30

Page 31: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Write Tests!

•Alwaysremembertowritetestswithyouractors

•CreateunitteststhatcheckthefunctionalityofmethodcallswithoutactorinteractionsusingTestActorRef

•Createintegrationteststhatsendmessagestoactorsandchecktheaggregatedresults

31

Page 32: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

"An AccountBalanceRetriever" should { "return a list of account balances" in { val savingsAccountsProxy = system.actorOf(Props(new SavingsAccountsProxyStub()), "svg") val checkingAccountsProxy = system.actorOf(Props(new CheckingAccountsProxyStub()), "chk") val moneyMarketAccountsProxy = system.actorOf(Props(new MoneyMarketAccountsProxyStub()), "mm") val accountBalanceRetriever = system.actorOf( Props(new AccountBalanceRetriever(savingsAccountsProxy, checkingAccountsProxy, moneyMarketAccountsProxy)), "cameo-1") val probe1 = TestProbe() val probe2 = TestProbe()

within(300 milliseconds) { probe1.send(accountBalanceRetriever, GetCustomerAccountBalances(1L)) val result = probe1.expectMsgType[AccountBalances] result must equal(AccountBalances(Some(List((3, 15000))), Some(List((1, 150000), (2, 29000))), Some(List()))) } within(300 milliseconds) { probe2.send(accountBalanceRetriever, GetCustomerAccountBalances(2L)) val result = probe2.expectMsgType[AccountBalances] result must equal(AccountBalances( Some(List((6, 640000), (7, 1125000), (8, 40000))), Some(List((5, 80000))), Some(List((9, 640000), (10, 1125000), (11, 40000))))) } } }

32

Page 33: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Add Non-Functional Requirements within(300 milliseconds) { probe1.send(accountBalanceRetriever, GetCustomerAccountBalances(1L)) val result = probe1.expectMsgType[AccountBalances] result must equal(AccountBalances( Some(List((3, 15000))), Some(List((1, 150000), (2, 29000))), Some(List()))) } within(300 milliseconds) { probe2.send(accountBalanceRetriever, GetCustomerAccountBalances(2L)) val result = probe2.expectMsgType[AccountBalances] result must equal(AccountBalances( Some(List((6, 640000), (7, 1125000), (8, 40000))), Some(List((5, 80000))), Some(List((9, 640000), (10, 1125000), (11, 40000))))) }

33

Page 34: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Write Moar Tests!

"return a TimeoutException when timeout is exceeded" in { val checkingAccountsProxy = system.actorOf(Props(new CheckingAccountsProxyStub()),“timeout-chk")

within(250 milliseconds, 500 milliseconds) { probe.send(accountBalanceRetriever, GetCustomerAccountBalances(1L)) probe.expectMsg(AccountRetrievalTimeout) } }

34

Page 35: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

Best Practices

Page 36: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Avoid Complexity of Coordination• Ifyourimplementationcanbeaccomplishedwithnocoordination,youdon’tneedRemotingorClusterandarelinearlyscalable

• UseRemotingif:

• Youneedtoscaleacrossnodesbutdon’tneedawarenessofnodesgoingdown

• Youdon’tneedtoscale“tiers”orrolesintheclusterindependentlyofoneanother

• YoucangetawaywithDeathWatchandsimplemulti-noderouting

• UseClusterif:

• Youneedtoknowifnodeswentdowntospinupanactorelsewhere

• Youneedindependent,managedscalabilityacrosstiers

36

Page 37: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Don’t Create Actors By Type Signature• AkkaActorscanbecreatedbypassingatypetothePropsconstructor

• Ifyouaddparameterstotheactorlater,youdon’tgetacompiletimeerror

val myActor = context.actorOf(Props[AccountBalanceResponseHandler])

37

Page 38: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Create a Props Factory• Creatinganactorwithinanotheractorimplicitlyclosesover“this”

• Necessaryuntilspores(SIP-21)arepartofScala,alwaysnecessaryfromJava

• CreateaPropsfactoryinacompanionobject

object AccountBalanceResponseHandler { def props(savingsAccounts: ActorRef, checkingAccounts: ActorRef, moneyMarketAccounts: ActorRef, originalSender: ActorRef): Props = {

Props(new AccountBalanceResponseHandler(savingsAccounts, checkingAccounts, moneyMarketAccounts, originalSender)) }}

38

Page 39: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Keep Your Actors Simple

• Donotconflateresponsibilitiesinactors

• Becomeshardtodefinetheboundariesofresponsibility

• Supervisionbecomesmoredifficultasyouhandlemorepossibilities

• Debuggingbecomesverydifficult

39

Page 40: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Be Explicit in Supervision

• Everynon-leafnodeistechnicallyasupervisor

• Createexplicitsupervisorsundereachnodeforeachtypeofchildtobemanaged

40

Page 41: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Conflated Supervision

41

C2C1

D1 D2 D3Ac2Ac1

A1 A2 A3 A4

Customers

Accounts & Devices

Applications

Page 42: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Explicit Supervision

42

C2C1

D1 D2 D3Ac2Ac1

A1 A2 A3 A4

Customers

Accounts & Devices

Applications

AS DS

Page 43: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Use Failure Zones

• Multipleisolatedzoneswiththeirownresources(threadpools,etc)

• Preventsstarvationofactors

• Preventsissuesinonebranchfromaffectinganother

val responseHandler = system.actorOf( AccountBalanceResponseHandler.props(), "cameo-handler").withDispatcher( "handler-dispatcher")

43

Page 44: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

No Failure Zones

44

C2C1

D1 D2 D3Ac2Ac1

A1 A2 A3 A4

Customers

Accounts & Devices

Applications

AS DS

Page 45: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Explicit Failure Zones

45

C2C1

D1 D2 D3Ac2Ac1

A1 A2 A3 A4

Customers

Applications

AS DS

Accounts & Devices

Page 46: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Push, Pull or Backpressure?• IfusingReactiveStreams(AkkaStreams/RxJava/etc),yougetback

pressureforfree

• Ifnot,youhavetochoosethemodelandpainyouwanttoendure

• Pullcanloaduptheproducer

• Pushcanloaduptheconsumer(s)

• Rules:

• Startwithnoguaranteesaboutdelivery

• Addguaranteesonlywhereyouneedthem

• Retryuntilyougettheansweryouexpect,ortimeout

• Switchyouractortoa"nominal"stateifsuccessful

46

Page 47: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Create Granular Messages

• Non-specificmessagesaboutgeneraleventsaredangerous

47

AccountDeviceAdded(acctNum, deviceNum)

AccountsUpdated

• Canresultin"eventstorms"asallactorsreacttothem

• Usespecificmessagesforwardedtoactorsforhandling

• Don’treusemessages,eveniftheyhavesimilarusages!

• Hurtsrefactoring

Page 48: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Create Specialized Exceptions

• Don'tusejava.lang.Exceptiontorepresentfailureinanactor

• Specificexceptionscanbehandledexplicitly

• StatecanbetransferredbetweenactorincarnationsinAkka(ifneedbe)

48

Page 49: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Never Reference “this”

• Actorsdie

• Doesn'tpreventsomeonefromcallingintoanactorwithanotherthread

• AkkasolvesthiswiththeActorRefabstraction

• Neverexpose/publish“this”

• Loopbysendingmessagesto“self”

• Registerbysendingreferencestoyour“self”

49

Page 50: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Never Reference “this” - Exception is JMX• InstrumenteveryactorcontainingstatewithJMXMxBeans

• Onlyuseaccessors,donotuse“operations”

• AkkaActorPathsareanaturalMxBeanObjectName

• Givesyouvisibilityintostatethatnomonitoringtoolcanprovide

• SeeWillSargent’sexcellentblogpostaboutthisattersesystems.com

50

Page 51: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Immutable Interactions

•Allmessagespassedbetweenactorsshouldbeimmutable

•Allmutabledatatobepassedwithmessagesshouldbecopiedbeforesending

•Youdon’twanttoaccidentallyexposetheverystateyou’retryingtoprotect

51

Page 52: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Externalize Logic

• Considerusingexternalfunctionsinobjectstoencapsulatecomplexbusinesslogic

• Nowonlydatapassedinasoperandscanbeaccessed,supportspurity

• Easiertounittestoutsideofactorcontext

• Notaruleofthumb,butsomethingtoconsiderascomplexityincreases

• NotasbigofanissuewithAkka'sTestKit

52

Page 53: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Semantically Useful Logging

• Trace-levellogsshouldhaveoutputthatyoucanreadeasily

• Uselinebreaksandindentation

• BothAkkaandErlangsupporthookinginmultiplelistenerstotheeventlogstream

53

Page 54: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Monitoring is Coming Back!

54

Page 55: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Monitoring is Coming Back!• Visual representations of actors at runtime are invaluable tools

• Keep an eye out for actors whose mailboxes never drain and keep getting bigger

• Look out for message handling latency that keeps going higher

• These are signs that the actors cannot handle their load

• Optimize with routers

• Rethink usage of Dispatchers

• Look at “throughput” setting for some groups of actors to batch message handling

55

Page 56: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

Monitoring will be a SPI• You can tap into the stream and work with it as well

• Will work with Graphite, Coda Hale Metrics, statsd and more

• Will require a Production Success Subscription from Typesafe

56

Page 57: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

EffectiveAkka

AsyncDebugger is Now Here in Scala IDE v4.2!• Feature of the FOSS Scala IDE

• Ability to “walk” an actor message send and see where it goes

• Ability to retrace backwards where a message came from, and see the state of the actor at that time

• Ability to “walk” into Futures as well

• Documentation: http://scala-ide.org/docs/current-user-doc/features/async-debugger/index.html

57

Page 58: Effective Akka v2.0 - Jamie Allen - JAX London | The ... · Effective Akka Goal Communicate as much about what I’ve learned in 6+ years of actor development within one hour We will

©Typesafe 2015 – All Rights Reserved