Reactive Programming using RxJava & Akka Actors Thomas Lockney (@tlockney) PDXScala June 14, 2014
What is reactive programming?
Reactive programming is… ● A programming paradigm ● About data-flow ● Not really anything new
Reactive programming is... ● Event-driven ● Scalable ● Resilient ● Responsive (the definition via the manifesto, anyway)
Reorganizing our thoughts a bit... ● Think in terms of discrete events ● Code in terms of behaviors ○ reacting to events ● The system state changes over time based on the flow of events
A brief digression... Amdahl’s law (paraphrased): The maximum increase in speed of a program is limited by the sequential portions of the code.
A brief digression... Amdahl’s law (paraphrased): If you have a program that runs for 4 hours, but 1 hour of runtime is necessarily serial, your program will have at most a 4x speedup with parallelization.
The thing about Amdahl’s law This is death by a thousand cuts. Every line of code is subject to this rule!
Some principles... ● Keep your data immutable ● Share nothing ● Blocking is bad!
Another brief digression...
Once you have: ● Immutable data structures ● Referential transparency ● First-class functions … this gives you ...
well... Functional Programming in the small
RxJava “a library for composing asynchronous and event-based programs using observable sequences for the Java VM”
My translation: RxJava A library for composing behaviors applied to event streams. note: when I say behaviors, think referentially-transparent functions.
Observable ● The “dual” of Iterable ● Or an Observer for sequences ● Somewhat similar to a Future ○ but, again, supports sequences
Creating Observables ● Observable.from(...), Observable.just(...), Observable.range(...), etc. ● Implement the Observable interface ○ explicit calls to onNext(), onError(), onComplete()
Observer & Subscriber Observer ● interface for handling Observables onNext, onError, onComplete Subscriber ● represents a subscription to an observable
RxJava is Reactive ● event-based (data-flows) ● scalable* ● resilient* (???) ● responsive* (???) * - requires some additional work on your end ??? - the story here is not entirely clear or potentially even available in current releases
Akka & the actor model
Actors (the Actor model) Defined by three traits ● a behavior (reacts to events) ● an state model ● send/receive (immutable) messages
Actors ● messages are processed sequentially ● multiple actors can be working at the same time ● actors are not threads ● an actor can block (but should avoid it) ● no shared state
Actor System ● actors interact ● actors exist together within an actor system ● the system defines the outermost bounds
Akka ● Scala-based actor model toolkit ● Provides a lot of extras ● We won’t be talking about those
Akka ● Supervisors ○ provide exception handling ○ “let it crash” ○ orthogonal to actor behavior ● Location transparency ○ abstracts the idea of “where” an actor lives
Akka ● Mailboxes ● Routing ○ e.g., round-robin, smallest mailbox, etc. ● Dispatching ○ essentially how to allocate thread resources
Akka is Reactive ● event-based ● resilient/fault-tolerant ● easily scalable* ● responsive* * - requires some additional work on your end