3. Localytics
Real time mobile analytics platform
40M+ events per day and growing rapidly
3x growth over the past 3 months
Heavy users of Scala/Akka/NoSql
We are hiring (seriously, come talk to me)
8. SEDA
Staged Event Driven Architecture
"Decomposes a complex, event-driven
application into a set of stages connected
by queues." 1
"The most fundamental aspect of the SEDA
architecture is the programming model that
supports stage-level backpressure and load
management." 1
1. http://www.eecs.harvard.edu/~mdw/proj/seda/
10. Backpressure
Manditory to prevent OutOfMemoryError
● Messages backup in memory faster than they
can be processed
Cassandra was seriously bitten by this
● Less crappy failure mode when swamped with
inserts than "run out of memory and gc-storm
to death" (CASSANDRA-401)
● Add backpressure to StorageProxy
(CASSANDRA-685)
11. Backpressure
Mailboxes
case class UnboundedMailbox(val blocking: Boolean = false) extends MailboxType
case class BoundedMailbox(
val blocking: Boolean = false,
val capacity: Int = {
if (Dispatchers.MAILBOX_CAPACITY < 0) Int.MaxValue
else Dispatchers.MAILBOX_CAPACITY
},
val pushTimeOut: Duration = Dispatchers.MAILBOX_PUSH_TIME_OUT
) extends MailboxType
Backpressure Mailbox
BoundedMailbox(false, QUEUE_SIZE, Duration(-1, "millis"))
13. Stages
One actor class per stage
Shared dispatcher
Individually tunable
● I/O Bound
● CPU Bound
Easier to reason about
Code reuse
14. Dispatchers
ThreadBasedDispatcher
● Binds one actor to its own thread
ExecutorBasedEventDrivenDispatcher
● Must be shared between actors
ExecutorBasedEventDrivenWorkStealingDispatcher
● Must be shared between actors of the same type
15. Queues
SEDA has a queue per stage model
Akka actors have their own mailbox
How do we evenly distribute work?
16. Work Stealing
ExecutorBasedEventDrivenWorkStealingDispatcher
"Actors of the same type can be set up to share this
dispatcher and during execution time the different
actors will steal messages from other actors if they
have less messages to process" 1
1. http://doc.akka.io/dispatchers-scala
17. Work Stealing
Really a work "donating" dispatcher
"I have implemented a work stealing dispatcher for
Akka actors. Although its called "work stealing" the
implementation actually behaves more as "work
donating" because the victim actor takes the initiative.
I.e. it actually donates work to its thief, rather
than having the thief steal work from the victim." 1
1. http://janvanbesien.blogspot.com/2010/03/load-balancing-actors-with-work.html
19. Work Stealing
Sending actor will block on the receiving actors
mailbox before it can "donate"
Might be fixed in Akka 1.1
● I owe @viktorklang a test of his latest changes
20. Load Balancing
Can we distribute work on the sender side?
21. Load Balancing
Routing.loadBalancerActor()
● Creates a new actor that forwards
messages in a load balancing fashion
InfiniteIterator
● CyclicIterator
● SmallestMailboxFirstIterator