SlideShare a Scribd company logo
1 of 58
Download to read offline
Jamie Allen
Sr. Director of Global Services
Effective
v2.0
@jamie_allen
Reac%ve'Applica%ons' 2'
Effective	
  Akka
Goal
Communicate	
  as	
  much	
  about	
  what	
  I’ve	
  learned	
  in	
  6+	
  years	
  of	
  actor	
  
development	
  within	
  one	
  hour	
  
We	
  will	
  start	
  with	
  a	
  cursory	
  overview	
  of	
  what	
  Actors	
  are	
  and	
  the	
  
problems	
  they	
  solve,	
  then	
  move	
  into	
  some	
  real-­‐world	
  use	
  cases	
  and	
  
how	
  to	
  test	
  them
3
Effective	
  Akka
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
Effective	
  Akka
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
Effective	
  Akka
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
Effective	
  Akka
What are Actors?
7
C2C1
D1 D2 D3Ac2Ac1
A1 A2 A3 A4
Customers
Accounts &
Devices
Applications
AS DS
Effective	
  Akka
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
USE CASES
Effective	
  Akka
Use Case 1
•A	
  large	
  cable	
  company	
  needs	
  to	
  stop	
  pinging	
  its	
  massive	
  database	
  for	
  
every	
  On	
  Demand	
  web	
  service	
  request	
  from	
  someone	
  using	
  their	
  remote	
  
control	
  
•A	
  live	
  cache	
  of	
  “entitlement”	
  data	
  is	
  required	
  that	
  is	
  updated	
  quickly	
  when	
  
a	
  customer	
  tries	
  to	
  change	
  their	
  service	
  
•Minimal	
  downtime	
  is	
  required	
  as	
  On	
  Demand	
  viewing	
  is	
  one	
  of	
  the	
  
corporation’s	
  largest	
  profit	
  centers
10
Effective	
  Akka
The Time-Based Approach
11
MegaCorp)
DB)
Riak)
Message)
Queue)
Warehoused)
Data)
DB)
Update)
Handler)
Transformer)
Transformer)
Transformer)
Transformer)
Transformer)
Effective	
  Akka
Issues
•A	
  missed	
  event	
  means	
  the	
  cache	
  is	
  now	
  out	
  of	
  synch	
  with	
  the	
  database	
  
•Assuming	
  we	
  even	
  know	
  a	
  failure	
  has	
  occurred	
  
•A	
  reload	
  of	
  the	
  cache	
  for	
  all	
  customers	
  would	
  be	
  2.5	
  hours	
  
•Latency	
  is	
  harder	
  to	
  track	
  and	
  dependent	
  on	
  “burstiness"	
  of	
  updates	
  
•How	
  do	
  you	
  represent	
  deleted	
  accounts?
12
Effective	
  Akka
The Self-Healing Approach
13
MegaCorp)
DB)
Riak)
Warehoused)
Data)
Account)
Retreiver)
Transformer)
Transformer)
Transformer)
Transformer)
Transformer)
Supervisor)
Supervisor)
Effective	
  Akka
Wins
•Fixed	
  and	
  tunable	
  latency	
  for	
  updates	
  depending	
  on	
  number	
  of	
  workers	
  
(and	
  the	
  size	
  of	
  their	
  buckets	
  of	
  accounts)	
  
•Resilience	
  via	
  supervision	
  
•Simpler	
  architecture	
  with	
  less	
  moving	
  parts	
  
•Never	
  out	
  of	
  synch	
  with	
  primary	
  database	
  for	
  longer	
  than	
  the	
  time	
  it	
  takes	
  
to	
  handle	
  the	
  maximum	
  size	
  of	
  a	
  bucket	
  of	
  accounts	
  
•Riak	
  handles	
  accounts	
  to	
  “delete”	
  automatically	
  by	
  tombstoning	
  records	
  
that	
  have	
  not	
  been	
  updated	
  within	
  a	
  time	
  window	
  (session	
  length	
  setting)
14
Effective	
  Akka
Use Case 2
•An	
  actor	
  will	
  receive	
  a	
  request	
  to	
  get	
  all	
  of	
  the	
  account	
  balances	
  for	
  a	
  
customer	
  (savings,	
  checking	
  and	
  money	
  market)	
  
•Actor	
  should	
  not	
  wait	
  to	
  finish	
  handling	
  one	
  request	
  before	
  handling	
  
another	
  
•Actor	
  should	
  receive	
  service	
  proxies	
  from	
  which	
  it	
  can	
  retrieve	
  each	
  
account’s	
  info	
  
•Actor	
  should	
  either	
  get	
  responses	
  from	
  all	
  three	
  within	
  a	
  specified	
  time,	
  or	
  
send	
  a	
  timeout	
  response	
  back	
  to	
  the	
  requestor
15
Effective	
  Akka
Cameo Pattern
16
•How	
  to	
  handle	
  individual	
  messages	
  to	
  an	
  actor	
  without	
  making	
  it	
  do	
  all	
  of	
  
the	
  work	
  before	
  handling	
  the	
  next	
  message	
  
•Similar	
  to	
  the	
  Saga	
  Pattern,	
  but	
  with	
  less	
  overhead	
  and	
  rules
Effective	
  Akka
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
Effective	
  Akka
Transactions?
•Could	
  be!	
  
•You	
  have	
  to	
  write	
  the	
  logic	
  of	
  how	
  to	
  roll	
  back	
  if	
  anything	
  fails	
  
•But	
  you	
  have	
  the	
  control	
  and	
  the	
  context	
  to	
  do	
  it,	
  especially	
  if	
  your	
  effects	
  
are	
  going	
  to	
  multiple	
  external	
  places	
  or	
  data	
  stores
18
Effective	
  Akka
All Code is on GitHub
• I’m	
  going	
  to	
  be	
  showing	
  some	
  reasonably	
  complex	
  examples	
  
• Don’t	
  worry	
  about	
  trying	
  to	
  copy	
  the	
  code	
  from	
  the	
  slides	
  
http://github.com/jamie-allen/effective_akka
19
Effective	
  Akka
Use Futures and Promises?
•I	
  prefer	
  not	
  to	
  when	
  I	
  want	
  contextual	
  resilience	
  and	
  location	
  transparency.	
  	
  
Use	
  another	
  actor	
  responsible	
  for:	
  
•Capturing	
  the	
  context	
  (original	
  requestor)	
  
•Defining	
  how	
  to	
  handle	
  responses	
  from	
  other	
  services	
  
•Defining	
  the	
  timeout	
  that	
  will	
  race	
  against	
  your	
  
•Each	
  Future	
  and	
  the	
  resulting	
  AskSupport	
  have	
  an	
  additional	
  cost	
  that	
  we	
  
do	
  not	
  always	
  need	
  to	
  pay	
  
•Futures	
  do	
  make	
  an	
  excellent	
  mechanism	
  for	
  calling	
  into	
  an	
  actor	
  world	
  
from	
  a	
  non-­‐actor	
  context	
  
•Futures	
  are	
  also	
  more	
  “composable”	
  and	
  can	
  help	
  define	
  a	
  problem	
  in	
  more	
  
simple	
  terms	
  
•Future	
  failure	
  handling	
  via	
  callbacks	
  is	
  no	
  more	
  composable	
  than	
  Try/Catch
20
Effective	
  Akka
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!
Effective	
  Akka
Capturing the Sender
•This	
  is	
  trickier	
  than	
  it	
  sounds	
  
•You	
  need	
  the	
  “sender”	
  value	
  from	
  the	
  actor	
  that	
  received	
  the	
  original	
  
request,	
  not	
  the	
  sender	
  inside	
  of	
  the	
  actor	
  handling	
  it!	
  
•One	
  of	
  the	
  biggest	
  sources	
  of	
  actor	
  bugs
22
Effective	
  Akka
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!
Effective	
  Akka
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!
Effective	
  Akka
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
Effective	
  Akka
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
Anonymous	
  
Actor
Effective	
  Akka
Use an Anonymous Actor?
•I	
  call	
  this	
  the	
  “Extra”	
  Pattern	
  
•It’s	
  not	
  bad,	
  but	
  it	
  has	
  drawbacks:	
  
•Poor	
  stack	
  traces	
  due	
  to	
  “name	
  mangled”	
  actor	
  names,	
  like	
  $a	
  
•More	
  difficult	
  to	
  maintain	
  the	
  cluttered	
  code,	
  and	
  developers	
  have	
  to	
  
read	
  through	
  the	
  body	
  of	
  the	
  anonymous	
  actor	
  to	
  figure	
  out	
  what	
  it	
  is	
  
doing	
  
•More	
  likely	
  to	
  “close	
  over”	
  state
27
Effective	
  Akka
Create a “Cameo” Actor
•Externalize	
  the	
  behavior	
  of	
  such	
  an	
  anonymous	
  actor	
  into	
  a	
  specific	
  type	
  
•Anyone	
  maintaining	
  the	
  code	
  now	
  has	
  a	
  type	
  name	
  from	
  which	
  they	
  can	
  
infer	
  what	
  the	
  actor	
  is	
  doing	
  
•Most	
  importantly,	
  you	
  can’t	
  close	
  over	
  state	
  from	
  an	
  enclosing	
  actor	
  -­‐	
  it	
  
must	
  be	
  passed	
  explicitly
28
Effective	
  Akka
Create a “Cameo” Actor
class 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
Effective	
  Akka
Remember to Stop the Actor
• When	
  you	
  are	
  finished	
  handling	
  a	
  request,	
  ensure	
  that	
  the	
  actor	
  used	
  is	
  
shutdown	
  
• This	
  is	
  a	
  big	
  memory	
  leak	
  if	
  you	
  don’t	
  
def sendResponseAndShutdown(response: Any) = {
originalSender ! response
log.debug("Stopping context capturing actor")
context.stop(self)
}
30
Effective	
  Akka
Write Tests!
•Always	
  remember	
  to	
  write	
  tests	
  with	
  your	
  actors	
  
•Create	
  unit	
  tests	
  that	
  check	
  the	
  functionality	
  of	
  method	
  calls	
  without	
  actor	
  
interactions	
  using	
  TestActorRef	
  
•Create	
  integration	
  tests	
  that	
  send	
  messages	
  to	
  actors	
  and	
  check	
  the	
  
aggregated	
  results
31
Effective	
  Akka
"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
Effective	
  Akka
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
Effective	
  Akka
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
Best Practices
Effective	
  Akka
Avoid Complexity of Coordination
• If	
  your	
  implementation	
  can	
  be	
  accomplished	
  with	
  no	
  coordination,	
  you	
  
don’t	
  need	
  Remoting	
  or	
  Cluster	
  and	
  are	
  linearly	
  scalable	
  
• Use	
  Remoting	
  if:	
  
• You	
  need	
  to	
  scale	
  across	
  nodes	
  but	
  don’t	
  need	
  awareness	
  of	
  nodes	
  
going	
  down	
  
• You	
  don’t	
  need	
  to	
  scale	
  “tiers”	
  or	
  roles	
  in	
  the	
  cluster	
  independently	
  of	
  
one	
  another	
  
• You	
  can	
  get	
  away	
  with	
  DeathWatch	
  and	
  simple	
  multi-­‐node	
  routing	
  
• Use	
  Cluster	
  if:	
  
• You	
  need	
  to	
  know	
  if	
  nodes	
  went	
  down	
  to	
  spin	
  up	
  an	
  actor	
  elsewhere	
  
• You	
  need	
  independent,	
  managed	
  scalability	
  across	
  tiers
36
Effective	
  Akka
Don’t Create Actors By Type Signature
• Akka	
  Actors	
  can	
  be	
  created	
  by	
  passing	
  a	
  type	
  to	
  the	
  Props	
  constructor	
  
• If	
  you	
  add	
  parameters	
  to	
  the	
  actor	
  later,	
  you	
  don’t	
  get	
  a	
  compile	
  time	
  error	
  
val myActor = context.actorOf(Props[AccountBalanceResponseHandler])
vs.	
  
val myActor = context.actorOf(Props(new AccountBalanceResponseHandler()))
37
Effective	
  Akka
Create a Props Factory
• Creating	
  an	
  actor	
  within	
  another	
  actor	
  implicitly	
  closes	
  over	
  “this”	
  
• Necessary	
  until	
  (unless?)	
  spores	
  (SIP-­‐21)	
  are	
  part	
  of	
  Scala,	
  always	
  necessary	
  
from	
  the	
  JVM	
  
• Create	
  a	
  Props	
  factory	
  in	
  a	
  companion	
  object	
  
object AccountBalanceResponseHandler {
def props(savingsAccounts: ActorRef,
checkingAccounts: ActorRef,
moneyMarketAccounts: ActorRef,
originalSender: ActorRef): Props = {
Props(new AccountBalanceResponseHandler(savingsAccounts, checkingAccounts,
moneyMarketAccounts, originalSender))
}
}
38
Effective	
  Akka
Keep Your Actors Simple
• Do	
  not	
  conflate	
  responsibilities	
  in	
  actors	
  
• Becomes	
  hard	
  to	
  define	
  the	
  boundaries	
  of	
  responsibility	
  
• Supervision	
  becomes	
  more	
  difficult	
  as	
  you	
  handle	
  more	
  possibilities	
  
• Debugging	
  becomes	
  very	
  difficult
39
Effective	
  Akka
Be Explicit in Supervision
• Every	
  non-­‐leaf	
  node	
  is	
  technically	
  a	
  supervisor	
  
• Create	
  explicit	
  supervisors	
  under	
  each	
  node	
  for	
  each	
  type	
  of	
  child	
  to	
  be	
  
managed
40
Effective	
  Akka
Conflated Supervision
41
C2C1
D1 D2 D3Ac2Ac1
A1 A2 A3 A4
Customers
Accounts &
Devices
Applications
Effective	
  Akka
Explicit Supervision
42
C2C1
D1 D2 D3Ac2Ac1
A1 A2 A3 A4
Customers
Accounts &
Devices
Applications
AS DS
Effective	
  Akka
Use Failure Zones
• Multiple	
  isolated	
  zones	
  with	
  their	
  own	
  resources	
  (thread	
  pools,	
  etc)	
  
• Prevents	
  starvation	
  of	
  actors	
  
• Prevents	
  issues	
  in	
  one	
  branch	
  from	
  affecting	
  another	
  
val responseHandler = system.actorOf(
AccountBalanceResponseHandler.props(),
"cameo-handler").withDispatcher(
"handler-dispatcher")
43
Effective	
  Akka
No Failure Zones
44
C2C1
D1 D2 D3Ac2Ac1
A1 A2 A3 A4
Customers
Accounts &
Devices
Applications
AS DS
Effective	
  Akka
Explicit Failure Zones
45
C2C1
D1 D2 D3Ac2Ac1
A1 A2 A3 A4
Customers
Applications
AS DS
Accounts &
Devices
Effective	
  Akka
Push, Pull or Backpressure?
• If	
  using	
  Reactive	
  Streams	
  (Akka	
  Streams/RxJava/etc),	
  you	
  get	
  back	
  
pressure	
  for	
  free	
  
• If	
  not,	
  you	
  have	
  to	
  choose	
  the	
  model	
  and	
  pain	
  you	
  want	
  to	
  endure	
  
• Pull	
  can	
  load	
  up	
  the	
  producer	
  
• Push	
  can	
  load	
  up	
  the	
  consumer(s)	
  
• Rules:	
  
• Start	
  with	
  no	
  guarantees	
  about	
  delivery	
  
• Add	
  guarantees	
  only	
  where	
  you	
  need	
  them	
  
• Retry	
  until	
  you	
  get	
  the	
  answer	
  you	
  expect,	
  or	
  timeout	
  
• Switch	
  your	
  actor	
  to	
  a	
  "nominal"	
  state	
  if	
  successful
46
Effective	
  Akka
Create Granular Messages
• Non-­‐specific	
  messages	
  about	
  general	
  events	
  are	
  dangerous
47
AccountDeviceAdded(acctNum, deviceNum)
AccountsUpdated
• Can	
  result	
  in	
  "event	
  storms"	
  as	
  all	
  actors	
  react	
  to	
  them	
  
• Use	
  specific	
  messages	
  forwarded	
  to	
  actors	
  for	
  handling
• Don’t	
  reuse	
  messages,	
  even	
  if	
  they	
  have	
  similar	
  usages!	
  
• Hurts	
  refactoring
Effective	
  Akka
Create Specialized Exceptions
• Don't	
  use	
  java.lang.Exception	
  to	
  represent	
  failure	
  in	
  an	
  actor	
  
• Specific	
  exceptions	
  can	
  be	
  handled	
  explicitly	
  
• State	
  can	
  be	
  transferred	
  between	
  actor	
  incarnations	
  in	
  Akka	
  (if	
  need	
  be)
48
Effective	
  Akka
Never Reference “this”
• Actors	
  die	
  
• Doesn't	
  prevent	
  someone	
  from	
  calling	
  into	
  an	
  actor	
  with	
  another	
  thread	
  
• Akka	
  solves	
  this	
  with	
  the	
  ActorRef	
  abstraction	
  
• Never	
  expose/publish	
  “this”	
  
• Loop	
  by	
  sending	
  messages	
  to	
  “self”	
  
• Register	
  by	
  sending	
  references	
  to	
  your	
  “self”
49
Effective	
  Akka
Never Reference “this” - Exception is JMX
• Instrument	
  every	
  actor	
  containing	
  state	
  with	
  JMX	
  MxBeans	
  
• Only	
  use	
  accessors,	
  do	
  not	
  use	
  “operations”	
  
• Akka	
  Actor	
  Paths	
  are	
  a	
  natural	
  MxBean	
  ObjectName	
  
• Gives	
  you	
  visibility	
  into	
  state	
  that	
  no	
  monitoring	
  tool	
  can	
  provide	
  
• See	
  Will	
  Sargent’s	
  excellent	
  blog	
  post	
  about	
  this	
  at	
  tersesystems.com
50
Effective	
  Akka
Immutable Interactions
•All	
  messages	
  passed	
  between	
  actors	
  should	
  be	
  immutable	
  
•All	
  mutable	
  data	
  to	
  be	
  passed	
  with	
  messages	
  should	
  be	
  copied	
  before	
  
sending	
  
•You	
  don’t	
  want	
  to	
  accidentally	
  expose	
  the	
  very	
  state	
  you’re	
  trying	
  to	
  
protect
51
Effective	
  Akka
Externalize Logic
• Consider	
  using	
  external	
  functions	
  in	
  objects	
  to	
  encapsulate	
  complex	
  
business	
  logic	
  
• Now	
  only	
  data	
  passed	
  in	
  as	
  operands	
  can	
  be	
  accessed,	
  supports	
  
purity	
  
• Easier	
  to	
  unit	
  test	
  outside	
  of	
  actor	
  context	
  
• Not	
  a	
  rule	
  of	
  thumb,	
  but	
  something	
  to	
  consider	
  as	
  complexity	
  increases	
  
• Not	
  as	
  big	
  of	
  an	
  issue	
  with	
  Akka's	
  TestKit
52
Effective	
  Akka
Semantically Useful Logging
• Trace-­‐level	
  logs	
  should	
  have	
  output	
  that	
  you	
  can	
  read	
  easily	
  
• Use	
  line	
  breaks	
  and	
  indentation	
  
• Both	
  Akka	
  and	
  Erlang	
  support	
  hooking	
  in	
  multiple	
  listeners	
  to	
  the	
  event	
  
log	
  stream
53
Effective	
  Akka
Monitoring is Back!
54
Effective	
  Akka
Monitoring is Back!
• Visual representations of actors at runtime are invaluable tools, use statsd
or Coda Hale Metrics APIs to export to your monitoring platform
• 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
Effective	
  Akka
Monitoring is an SPI
• You can tap into the stream and work with it as well
• Will require a Production Success Subscription from Typesafe
56
Effective	
  Akka
AsyncDebugger is 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
• Check out https://github.com/jamie-allen/async_debugger_demo for an
example you can play with and basic getting started instructions
57
©Typesafe 2016– All Rights Reserved

More Related Content

What's hot

Atlas for Enterprise
Atlas for EnterpriseAtlas for Enterprise
Atlas for EnterpriseMongoDB
 
Cqrs and event sourcing in azure
Cqrs and event sourcing in azureCqrs and event sourcing in azure
Cqrs and event sourcing in azureSergey Seletsky
 
Real World Event Sourcing and CQRS
Real World Event Sourcing and CQRSReal World Event Sourcing and CQRS
Real World Event Sourcing and CQRSMatthew Hawkins
 
Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Chris Richardson
 
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018Starling Bank
 
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...Gary Arora
 
Stuff About CQRS
Stuff About CQRSStuff About CQRS
Stuff About CQRSthinkddd
 
An eventful tour from enterprise integration to serverless and functions
An eventful tour from enterprise integration to serverless and functionsAn eventful tour from enterprise integration to serverless and functions
An eventful tour from enterprise integration to serverless and functionsChristian Posta
 
Go Serverless with Azure
Go Serverless with AzureGo Serverless with Azure
Go Serverless with AzureSergey Seletsky
 
Cqrs + event sourcing pyxis v2 - en
Cqrs + event sourcing   pyxis v2 - enCqrs + event sourcing   pyxis v2 - en
Cqrs + event sourcing pyxis v2 - enEric De Carufel
 
Developing applications with a microservice architecture (svcc)
Developing applications with a microservice architecture (svcc)Developing applications with a microservice architecture (svcc)
Developing applications with a microservice architecture (svcc)Chris Richardson
 
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...Chris Richardson
 
Intellias CQRS Framework
Intellias CQRS FrameworkIntellias CQRS Framework
Intellias CQRS FrameworkSergey Seletsky
 
An overview of the Eventuate Platform
An overview of the Eventuate PlatformAn overview of the Eventuate Platform
An overview of the Eventuate PlatformChris Richardson
 
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with SagasJavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with SagasChris Richardson
 
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...HostedbyConfluent
 
Building a Microservices-based ERP System
Building a Microservices-based ERP SystemBuilding a Microservices-based ERP System
Building a Microservices-based ERP SystemMongoDB
 
#JaxLondon keynote: Developing applications with a microservice architecture
#JaxLondon keynote: Developing applications with a microservice architecture#JaxLondon keynote: Developing applications with a microservice architecture
#JaxLondon keynote: Developing applications with a microservice architectureChris Richardson
 

What's hot (20)

Reactive Architectures
Reactive ArchitecturesReactive Architectures
Reactive Architectures
 
Atlas for Enterprise
Atlas for EnterpriseAtlas for Enterprise
Atlas for Enterprise
 
Cqrs and event sourcing in azure
Cqrs and event sourcing in azureCqrs and event sourcing in azure
Cqrs and event sourcing in azure
 
Real World Event Sourcing and CQRS
Real World Event Sourcing and CQRSReal World Event Sourcing and CQRS
Real World Event Sourcing and CQRS
 
Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)Microservices pattern language (microxchg microxchg2016)
Microservices pattern language (microxchg microxchg2016)
 
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018
Building a Reliable Cloud Bank in Java | Starling Bank | QCon 2018
 
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...
Leapfrog into Serverless - a Deloitte-Amtrak Case Study | Serverless Confere...
 
Stuff About CQRS
Stuff About CQRSStuff About CQRS
Stuff About CQRS
 
An eventful tour from enterprise integration to serverless and functions
An eventful tour from enterprise integration to serverless and functionsAn eventful tour from enterprise integration to serverless and functions
An eventful tour from enterprise integration to serverless and functions
 
Go Serverless with Azure
Go Serverless with AzureGo Serverless with Azure
Go Serverless with Azure
 
Cqrs + event sourcing pyxis v2 - en
Cqrs + event sourcing   pyxis v2 - enCqrs + event sourcing   pyxis v2 - en
Cqrs + event sourcing pyxis v2 - en
 
Developing applications with a microservice architecture (svcc)
Developing applications with a microservice architecture (svcc)Developing applications with a microservice architecture (svcc)
Developing applications with a microservice architecture (svcc)
 
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...
YOW! Perth: Cubes, Hexagons, Triangles, and More: Understanding the Microserv...
 
Intellias CQRS Framework
Intellias CQRS FrameworkIntellias CQRS Framework
Intellias CQRS Framework
 
CQRS and Event Sourcing
CQRS and Event SourcingCQRS and Event Sourcing
CQRS and Event Sourcing
 
An overview of the Eventuate Platform
An overview of the Eventuate PlatformAn overview of the Eventuate Platform
An overview of the Eventuate Platform
 
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with SagasJavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
JavaOne2017: ACID Is So Yesterday: Maintaining Data Consistency with Sagas
 
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...
Leveraging Data in Motion | Jun Rao, Co-Founder, Confluent | Kafka Summit APA...
 
Building a Microservices-based ERP System
Building a Microservices-based ERP SystemBuilding a Microservices-based ERP System
Building a Microservices-based ERP System
 
#JaxLondon keynote: Developing applications with a microservice architecture
#JaxLondon keynote: Developing applications with a microservice architecture#JaxLondon keynote: Developing applications with a microservice architecture
#JaxLondon keynote: Developing applications with a microservice architecture
 

Similar to Effective Akka v2

Continuation_alan_20220503.pdf
Continuation_alan_20220503.pdfContinuation_alan_20220503.pdf
Continuation_alan_20220503.pdfShen yifeng
 
Eric Proegler Oredev Performance Testing in New Contexts
Eric Proegler Oredev Performance Testing in New ContextsEric Proegler Oredev Performance Testing in New Contexts
Eric Proegler Oredev Performance Testing in New ContextsEric Proegler
 
Building Reactive applications with Akka
Building Reactive applications with AkkaBuilding Reactive applications with Akka
Building Reactive applications with AkkaKnoldus Inc.
 
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...Amazon Web Services
 
Building Event-Driven Services with Apache Kafka
Building Event-Driven Services with Apache KafkaBuilding Event-Driven Services with Apache Kafka
Building Event-Driven Services with Apache Kafkaconfluent
 
Event-Driven Architectures Done Right | Tim Berglund, Confluent
Event-Driven Architectures Done Right | Tim Berglund, ConfluentEvent-Driven Architectures Done Right | Tim Berglund, Confluent
Event-Driven Architectures Done Right | Tim Berglund, ConfluentHostedbyConfluent
 
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2:  Building Event-Driven Services with Apache KafkaEvent Driven Services Part 2:  Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2: Building Event-Driven Services with Apache KafkaBen Stopford
 
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...Amazon Web Services
 
Software Architectures, Week 4 - Message-based Architectures, Message Bus
Software Architectures, Week 4 - Message-based Architectures, Message BusSoftware Architectures, Week 4 - Message-based Architectures, Message Bus
Software Architectures, Week 4 - Message-based Architectures, Message BusAngelos Kapsimanis
 
Dev Lakhani, Data Scientist at Batch Insights "Real Time Big Data Applicatio...
Dev Lakhani, Data Scientist at Batch Insights  "Real Time Big Data Applicatio...Dev Lakhani, Data Scientist at Batch Insights  "Real Time Big Data Applicatio...
Dev Lakhani, Data Scientist at Batch Insights "Real Time Big Data Applicatio...Dataconomy Media
 
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)confluent
 
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Technologies
 
Inside Kafka Streams—Monitoring Comcast’s Outside Plant
Inside Kafka Streams—Monitoring Comcast’s Outside Plant Inside Kafka Streams—Monitoring Comcast’s Outside Plant
Inside Kafka Streams—Monitoring Comcast’s Outside Plant confluent
 
Agile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAgile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAraf Karsh Hamid
 
The End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementThe End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementRicardo Jimenez-Peris
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application DesignGlobalLogic Ukraine
 
Akka for big data developers
Akka for big data developersAkka for big data developers
Akka for big data developersTaras Fedorov
 
Avoiding the Pit of Despair - Event Sourcing with Akka and Cassandra
Avoiding the Pit of Despair - Event Sourcing with Akka and CassandraAvoiding the Pit of Despair - Event Sourcing with Akka and Cassandra
Avoiding the Pit of Despair - Event Sourcing with Akka and CassandraLuke Tillman
 
OnPrem Monitoring.pdf
OnPrem Monitoring.pdfOnPrem Monitoring.pdf
OnPrem Monitoring.pdfTarekHamdi8
 

Similar to Effective Akka v2 (20)

Effective Akka v2.0 - Jamie Allen
Effective Akka v2.0 - Jamie AllenEffective Akka v2.0 - Jamie Allen
Effective Akka v2.0 - Jamie Allen
 
Continuation_alan_20220503.pdf
Continuation_alan_20220503.pdfContinuation_alan_20220503.pdf
Continuation_alan_20220503.pdf
 
Eric Proegler Oredev Performance Testing in New Contexts
Eric Proegler Oredev Performance Testing in New ContextsEric Proegler Oredev Performance Testing in New Contexts
Eric Proegler Oredev Performance Testing in New Contexts
 
Building Reactive applications with Akka
Building Reactive applications with AkkaBuilding Reactive applications with Akka
Building Reactive applications with Akka
 
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
AWS re:Invent 2016: How Fulfillment by Amazon (FBA) and Scopely Improved Resu...
 
Building Event-Driven Services with Apache Kafka
Building Event-Driven Services with Apache KafkaBuilding Event-Driven Services with Apache Kafka
Building Event-Driven Services with Apache Kafka
 
Event-Driven Architectures Done Right | Tim Berglund, Confluent
Event-Driven Architectures Done Right | Tim Berglund, ConfluentEvent-Driven Architectures Done Right | Tim Berglund, Confluent
Event-Driven Architectures Done Right | Tim Berglund, Confluent
 
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2:  Building Event-Driven Services with Apache KafkaEvent Driven Services Part 2:  Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
 
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
Reliability of the Cloud: How AWS Achieves High Availability (ARC317-R1) - AW...
 
Software Architectures, Week 4 - Message-based Architectures, Message Bus
Software Architectures, Week 4 - Message-based Architectures, Message BusSoftware Architectures, Week 4 - Message-based Architectures, Message Bus
Software Architectures, Week 4 - Message-based Architectures, Message Bus
 
Dev Lakhani, Data Scientist at Batch Insights "Real Time Big Data Applicatio...
Dev Lakhani, Data Scientist at Batch Insights  "Real Time Big Data Applicatio...Dev Lakhani, Data Scientist at Batch Insights  "Real Time Big Data Applicatio...
Dev Lakhani, Data Scientist at Batch Insights "Real Time Big Data Applicatio...
 
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)
3 Ways to Deliver an Elastic, Cost-Effective Cloud Architecture (ANZ)
 
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeterCA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
CA Security Communities Webcast - CA SSO Performance Testing with CA BlazeMeter
 
Inside Kafka Streams—Monitoring Comcast’s Outside Plant
Inside Kafka Streams—Monitoring Comcast’s Outside Plant Inside Kafka Streams—Monitoring Comcast’s Outside Plant
Inside Kafka Streams—Monitoring Comcast’s Outside Plant
 
Agile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven DesignAgile, User Stories, Domain Driven Design
Agile, User Stories, Domain Driven Design
 
The End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional ManagementThe End of a Myth: Ultra-Scalable Transactional Management
The End of a Myth: Ultra-Scalable Transactional Management
 
Patterns of Distributed Application Design
Patterns of Distributed Application DesignPatterns of Distributed Application Design
Patterns of Distributed Application Design
 
Akka for big data developers
Akka for big data developersAkka for big data developers
Akka for big data developers
 
Avoiding the Pit of Despair - Event Sourcing with Akka and Cassandra
Avoiding the Pit of Despair - Event Sourcing with Akka and CassandraAvoiding the Pit of Despair - Event Sourcing with Akka and Cassandra
Avoiding the Pit of Despair - Event Sourcing with Akka and Cassandra
 
OnPrem Monitoring.pdf
OnPrem Monitoring.pdfOnPrem Monitoring.pdf
OnPrem Monitoring.pdf
 

More from shinolajla

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rsshinolajla
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_productshinolajla
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynoteshinolajla
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetupshinolajla
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdasshinolajla
 
20150411 mutability matrix of pain scala
20150411 mutability matrix of pain scala20150411 mutability matrix of pain scala
20150411 mutability matrix of pain scalashinolajla
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff poshinolajla
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performanceshinolajla
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaioshinolajla
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3shinolajla
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesubshinolajla
 
Effective Actors
Effective ActorsEffective Actors
Effective Actorsshinolajla
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 

More from shinolajla (15)

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_product
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynote
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetup
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
 
20150411 mutability matrix of pain scala
20150411 mutability matrix of pain scala20150411 mutability matrix of pain scala
20150411 mutability matrix of pain scala
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff po
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performance
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaio
 
Cpu Caches
Cpu CachesCpu Caches
Cpu Caches
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesub
 
Effective Actors
Effective ActorsEffective Actors
Effective Actors
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
CPU Caches
CPU CachesCPU Caches
CPU Caches
 

Recently uploaded

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 

Recently uploaded (20)

TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 

Effective Akka v2

  • 1. Jamie Allen Sr. Director of Global Services Effective v2.0
  • 3. Effective  Akka Goal Communicate  as  much  about  what  I’ve  learned  in  6+  years  of  actor   development  within  one  hour   We  will  start  with  a  cursory  overview  of  what  Actors  are  and  the   problems  they  solve,  then  move  into  some  real-­‐world  use  cases  and   how  to  test  them 3
  • 4. Effective  Akka 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
  • 5. Effective  Akka 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
  • 6. Effective  Akka 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
  • 7. Effective  Akka What are Actors? 7 C2C1 D1 D2 D3Ac2Ac1 A1 A2 A3 A4 Customers Accounts & Devices Applications AS DS
  • 8. Effective  Akka 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
  • 10. Effective  Akka Use Case 1 •A  large  cable  company  needs  to  stop  pinging  its  massive  database  for   every  On  Demand  web  service  request  from  someone  using  their  remote   control   •A  live  cache  of  “entitlement”  data  is  required  that  is  updated  quickly  when   a  customer  tries  to  change  their  service   •Minimal  downtime  is  required  as  On  Demand  viewing  is  one  of  the   corporation’s  largest  profit  centers 10
  • 11. Effective  Akka The Time-Based Approach 11 MegaCorp) DB) Riak) Message) Queue) Warehoused) Data) DB) Update) Handler) Transformer) Transformer) Transformer) Transformer) Transformer)
  • 12. Effective  Akka Issues •A  missed  event  means  the  cache  is  now  out  of  synch  with  the  database   •Assuming  we  even  know  a  failure  has  occurred   •A  reload  of  the  cache  for  all  customers  would  be  2.5  hours   •Latency  is  harder  to  track  and  dependent  on  “burstiness"  of  updates   •How  do  you  represent  deleted  accounts? 12
  • 13. Effective  Akka The Self-Healing Approach 13 MegaCorp) DB) Riak) Warehoused) Data) Account) Retreiver) Transformer) Transformer) Transformer) Transformer) Transformer) Supervisor) Supervisor)
  • 14. Effective  Akka Wins •Fixed  and  tunable  latency  for  updates  depending  on  number  of  workers   (and  the  size  of  their  buckets  of  accounts)   •Resilience  via  supervision   •Simpler  architecture  with  less  moving  parts   •Never  out  of  synch  with  primary  database  for  longer  than  the  time  it  takes   to  handle  the  maximum  size  of  a  bucket  of  accounts   •Riak  handles  accounts  to  “delete”  automatically  by  tombstoning  records   that  have  not  been  updated  within  a  time  window  (session  length  setting) 14
  • 15. Effective  Akka Use Case 2 •An  actor  will  receive  a  request  to  get  all  of  the  account  balances  for  a   customer  (savings,  checking  and  money  market)   •Actor  should  not  wait  to  finish  handling  one  request  before  handling   another   •Actor  should  receive  service  proxies  from  which  it  can  retrieve  each   account’s  info   •Actor  should  either  get  responses  from  all  three  within  a  specified  time,  or   send  a  timeout  response  back  to  the  requestor 15
  • 16. Effective  Akka Cameo Pattern 16 •How  to  handle  individual  messages  to  an  actor  without  making  it  do  all  of   the  work  before  handling  the  next  message   •Similar  to  the  Saga  Pattern,  but  with  less  overhead  and  rules
  • 17. Effective  Akka 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
  • 18. Effective  Akka Transactions? •Could  be!   •You  have  to  write  the  logic  of  how  to  roll  back  if  anything  fails   •But  you  have  the  control  and  the  context  to  do  it,  especially  if  your  effects   are  going  to  multiple  external  places  or  data  stores 18
  • 19. Effective  Akka All Code is on GitHub • I’m  going  to  be  showing  some  reasonably  complex  examples   • Don’t  worry  about  trying  to  copy  the  code  from  the  slides   http://github.com/jamie-allen/effective_akka 19
  • 20. Effective  Akka Use Futures and Promises? •I  prefer  not  to  when  I  want  contextual  resilience  and  location  transparency.     Use  another  actor  responsible  for:   •Capturing  the  context  (original  requestor)   •Defining  how  to  handle  responses  from  other  services   •Defining  the  timeout  that  will  race  against  your   •Each  Future  and  the  resulting  AskSupport  have  an  additional  cost  that  we   do  not  always  need  to  pay   •Futures  do  make  an  excellent  mechanism  for  calling  into  an  actor  world   from  a  non-­‐actor  context   •Futures  are  also  more  “composable”  and  can  help  define  a  problem  in  more   simple  terms   •Future  failure  handling  via  callbacks  is  no  more  composable  than  Try/Catch 20
  • 21. Effective  Akka 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!
  • 22. Effective  Akka Capturing the Sender •This  is  trickier  than  it  sounds   •You  need  the  “sender”  value  from  the  actor  that  received  the  original   request,  not  the  sender  inside  of  the  actor  handling  it!   •One  of  the  biggest  sources  of  actor  bugs 22
  • 23. Effective  Akka 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!
  • 24. Effective  Akka 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!
  • 25. Effective  Akka 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
  • 26. Effective  Akka 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 Anonymous   Actor
  • 27. Effective  Akka Use an Anonymous Actor? •I  call  this  the  “Extra”  Pattern   •It’s  not  bad,  but  it  has  drawbacks:   •Poor  stack  traces  due  to  “name  mangled”  actor  names,  like  $a   •More  difficult  to  maintain  the  cluttered  code,  and  developers  have  to   read  through  the  body  of  the  anonymous  actor  to  figure  out  what  it  is   doing   •More  likely  to  “close  over”  state 27
  • 28. Effective  Akka Create a “Cameo” Actor •Externalize  the  behavior  of  such  an  anonymous  actor  into  a  specific  type   •Anyone  maintaining  the  code  now  has  a  type  name  from  which  they  can   infer  what  the  actor  is  doing   •Most  importantly,  you  can’t  close  over  state  from  an  enclosing  actor  -­‐  it   must  be  passed  explicitly 28
  • 29. Effective  Akka Create a “Cameo” Actor class 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
  • 30. Effective  Akka Remember to Stop the Actor • When  you  are  finished  handling  a  request,  ensure  that  the  actor  used  is   shutdown   • This  is  a  big  memory  leak  if  you  don’t   def sendResponseAndShutdown(response: Any) = { originalSender ! response log.debug("Stopping context capturing actor") context.stop(self) } 30
  • 31. Effective  Akka Write Tests! •Always  remember  to  write  tests  with  your  actors   •Create  unit  tests  that  check  the  functionality  of  method  calls  without  actor   interactions  using  TestActorRef   •Create  integration  tests  that  send  messages  to  actors  and  check  the   aggregated  results 31
  • 32. Effective  Akka "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
  • 33. Effective  Akka 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
  • 34. Effective  Akka 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
  • 36. Effective  Akka Avoid Complexity of Coordination • If  your  implementation  can  be  accomplished  with  no  coordination,  you   don’t  need  Remoting  or  Cluster  and  are  linearly  scalable   • Use  Remoting  if:   • You  need  to  scale  across  nodes  but  don’t  need  awareness  of  nodes   going  down   • You  don’t  need  to  scale  “tiers”  or  roles  in  the  cluster  independently  of   one  another   • You  can  get  away  with  DeathWatch  and  simple  multi-­‐node  routing   • Use  Cluster  if:   • You  need  to  know  if  nodes  went  down  to  spin  up  an  actor  elsewhere   • You  need  independent,  managed  scalability  across  tiers 36
  • 37. Effective  Akka Don’t Create Actors By Type Signature • Akka  Actors  can  be  created  by  passing  a  type  to  the  Props  constructor   • If  you  add  parameters  to  the  actor  later,  you  don’t  get  a  compile  time  error   val myActor = context.actorOf(Props[AccountBalanceResponseHandler]) vs.   val myActor = context.actorOf(Props(new AccountBalanceResponseHandler())) 37
  • 38. Effective  Akka Create a Props Factory • Creating  an  actor  within  another  actor  implicitly  closes  over  “this”   • Necessary  until  (unless?)  spores  (SIP-­‐21)  are  part  of  Scala,  always  necessary   from  the  JVM   • Create  a  Props  factory  in  a  companion  object   object AccountBalanceResponseHandler { def props(savingsAccounts: ActorRef, checkingAccounts: ActorRef, moneyMarketAccounts: ActorRef, originalSender: ActorRef): Props = { Props(new AccountBalanceResponseHandler(savingsAccounts, checkingAccounts, moneyMarketAccounts, originalSender)) } } 38
  • 39. Effective  Akka Keep Your Actors Simple • Do  not  conflate  responsibilities  in  actors   • Becomes  hard  to  define  the  boundaries  of  responsibility   • Supervision  becomes  more  difficult  as  you  handle  more  possibilities   • Debugging  becomes  very  difficult 39
  • 40. Effective  Akka Be Explicit in Supervision • Every  non-­‐leaf  node  is  technically  a  supervisor   • Create  explicit  supervisors  under  each  node  for  each  type  of  child  to  be   managed 40
  • 41. Effective  Akka Conflated Supervision 41 C2C1 D1 D2 D3Ac2Ac1 A1 A2 A3 A4 Customers Accounts & Devices Applications
  • 42. Effective  Akka Explicit Supervision 42 C2C1 D1 D2 D3Ac2Ac1 A1 A2 A3 A4 Customers Accounts & Devices Applications AS DS
  • 43. Effective  Akka Use Failure Zones • Multiple  isolated  zones  with  their  own  resources  (thread  pools,  etc)   • Prevents  starvation  of  actors   • Prevents  issues  in  one  branch  from  affecting  another   val responseHandler = system.actorOf( AccountBalanceResponseHandler.props(), "cameo-handler").withDispatcher( "handler-dispatcher") 43
  • 44. Effective  Akka No Failure Zones 44 C2C1 D1 D2 D3Ac2Ac1 A1 A2 A3 A4 Customers Accounts & Devices Applications AS DS
  • 45. Effective  Akka Explicit Failure Zones 45 C2C1 D1 D2 D3Ac2Ac1 A1 A2 A3 A4 Customers Applications AS DS Accounts & Devices
  • 46. Effective  Akka Push, Pull or Backpressure? • If  using  Reactive  Streams  (Akka  Streams/RxJava/etc),  you  get  back   pressure  for  free   • If  not,  you  have  to  choose  the  model  and  pain  you  want  to  endure   • Pull  can  load  up  the  producer   • Push  can  load  up  the  consumer(s)   • Rules:   • Start  with  no  guarantees  about  delivery   • Add  guarantees  only  where  you  need  them   • Retry  until  you  get  the  answer  you  expect,  or  timeout   • Switch  your  actor  to  a  "nominal"  state  if  successful 46
  • 47. Effective  Akka Create Granular Messages • Non-­‐specific  messages  about  general  events  are  dangerous 47 AccountDeviceAdded(acctNum, deviceNum) AccountsUpdated • Can  result  in  "event  storms"  as  all  actors  react  to  them   • Use  specific  messages  forwarded  to  actors  for  handling • Don’t  reuse  messages,  even  if  they  have  similar  usages!   • Hurts  refactoring
  • 48. Effective  Akka Create Specialized Exceptions • Don't  use  java.lang.Exception  to  represent  failure  in  an  actor   • Specific  exceptions  can  be  handled  explicitly   • State  can  be  transferred  between  actor  incarnations  in  Akka  (if  need  be) 48
  • 49. Effective  Akka Never Reference “this” • Actors  die   • Doesn't  prevent  someone  from  calling  into  an  actor  with  another  thread   • Akka  solves  this  with  the  ActorRef  abstraction   • Never  expose/publish  “this”   • Loop  by  sending  messages  to  “self”   • Register  by  sending  references  to  your  “self” 49
  • 50. Effective  Akka Never Reference “this” - Exception is JMX • Instrument  every  actor  containing  state  with  JMX  MxBeans   • Only  use  accessors,  do  not  use  “operations”   • Akka  Actor  Paths  are  a  natural  MxBean  ObjectName   • Gives  you  visibility  into  state  that  no  monitoring  tool  can  provide   • See  Will  Sargent’s  excellent  blog  post  about  this  at  tersesystems.com 50
  • 51. Effective  Akka Immutable Interactions •All  messages  passed  between  actors  should  be  immutable   •All  mutable  data  to  be  passed  with  messages  should  be  copied  before   sending   •You  don’t  want  to  accidentally  expose  the  very  state  you’re  trying  to   protect 51
  • 52. Effective  Akka Externalize Logic • Consider  using  external  functions  in  objects  to  encapsulate  complex   business  logic   • Now  only  data  passed  in  as  operands  can  be  accessed,  supports   purity   • Easier  to  unit  test  outside  of  actor  context   • Not  a  rule  of  thumb,  but  something  to  consider  as  complexity  increases   • Not  as  big  of  an  issue  with  Akka's  TestKit 52
  • 53. Effective  Akka Semantically Useful Logging • Trace-­‐level  logs  should  have  output  that  you  can  read  easily   • Use  line  breaks  and  indentation   • Both  Akka  and  Erlang  support  hooking  in  multiple  listeners  to  the  event   log  stream 53
  • 55. Effective  Akka Monitoring is Back! • Visual representations of actors at runtime are invaluable tools, use statsd or Coda Hale Metrics APIs to export to your monitoring platform • 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
  • 56. Effective  Akka Monitoring is an SPI • You can tap into the stream and work with it as well • Will require a Production Success Subscription from Typesafe 56
  • 57. Effective  Akka AsyncDebugger is 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 • Check out https://github.com/jamie-allen/async_debugger_demo for an example you can play with and basic getting started instructions 57
  • 58. ©Typesafe 2016– All Rights Reserved