Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Streams High Level

Ersin Ertan
November 04, 2016

Reactive Streams High Level

Reactive streams and reactive extensions talk, high level overview of philosophy and concepts. Uses RxJava as base.

Ersin Ertan

November 04, 2016
Tweet

More Decks by Ersin Ertan

Other Decks in Technology

Transcript

  1. Examples use concepts from the RxJava implementation of reactive streams,

    your language may have alternate naming and more/less concepts depending on the implementation
  2. Reactive Streams Spec http://www.reactive-streams.org/ Live data, unknown source size, asynchronous,

    backpressure aware(consumer buffering) Set of interfaces to interop between systems(language neutral) that adhere to the spec Max utilization of CPU, multi core, parallel processing Networking aware(TCP, UDP, HTTP and WebSockets) serialization/deserialization
  3. Commonly Confused Terms Observable - is an object that emits

    data Observer - parent class of an object that can see a stream of data Subscriber - an observer that watches the stream of data from an observable, and implements code to deal with the data Subscription - the object of a subscriber who is watching an observable source, can unsubscribe from the source
  4. Observer Pattern - Push Data Model Model streams as synchronous

    or asynchronous Observables push changes to subscribers Subscribers receive changes from Observables Subscribers implement data manipulation, data reaction, error handling Do work upon subscription, or at the end of a subscription Termination conditions like success, error, or non terminating
  5. Eventbus - Existing Solution Pros: Data transfer between architectural layers/threads,

    flexible and easy to use Cons: Data transfer between architectural layers/threads, flexible and easy to use Dynamically typed spaghetti code on a bad architecture/understanding Hard to reason about, unknown life time management depending on the pub/sub implementation(static/dynamic) Multiple events, timing issues, chaining events requires state management, cognitive load
  6. Observables Sources - Hot and Cold Observable is the function

    that connects you to a data producer Hot produces data regardless subscriptions, often in great quantity Cold produces data only when subscribed to, often in fixed amount Both may or may not terminate, thus data can always be streamed.
  7. Specialized Source Types/Classes Single - single return, or error Completable

    - on complete message, or error *new* Maybe - single or completable, or error
  8. Observers - Subscriptions Handles the onNext, onError, onCompleted methods from

    an observable source Called on whatever thread it was assigned on Can offer deterministic cancellation if implemented Many observers can be subscribed to one observable, but can receive totally different parts/transforms of the data
  9. Schedulers - Thread Manipulation io - unbounded thread-pool, low cpu

    use: network, database, file system computation - bounded thread pool per cpu processor, long run/intense tasks immediate - executes work right away on current thread trampoline - queues work on current thread newThread - per call, uncommon due to heavy thread creation costs
  10. Single Context - Functional Composition Combine multiple observables using operators

    to create a new stream based on composition logic(smaller and more declarative expression) startWith, merge, zip, and, then, when, combineLatest, join, groupJoin, switchOnNext Distributed composition logic, have operations chained together and referenced as a single operation to be applied on the stream of data
  11. Operators - Data Transformation Manipulate streamed data using any kind

    of operation like: take, combine, switch, skip sources of data, or to create new data streams. buffer - bundle data into N objects and emit the bundle cache - save results for future calls delay - wait N amount of time time before continuing map - apply a function on the data and return the data(can be a new type) zip - combine observables into one new observable
  12. Subject - Both an Observable and Observer Can subscribe to

    one or more observables, can pass items it observes as retransmissions or new data AsyncSubject - emits the last value only after the observable completes BehaviorSubject - on subscription, emits the most recent data and continues to stream its observed data PublishSubject - emits entire history of data that was streamed before subscription ReplaySubject - emits entire history of data to subscribers and continues, may have a buffer limit
  13. Additional Benefits Once you understand the concept, open source libraries

    with reactive interfaces Clear code semantic with an understanding of data source, not state Deterministic threading abstraction to build upon, sequence, parallel, async Easier to test by injecting mock values into the stream Common cross domain technological terminology for any project Community of adoption: http://www.reactivemanifesto.org/ Long term knowledge for system and CPU management
  14. Complications Knowledge of Backpressure and how to handle it Subscribing/unsubscribing

    management for lifetime(else memory leaks) Large API of operators, additional APIs for programming language specific details Scattered and versioned learning resources Language implementation feature disparity(based on community commit effort) Project adoption is difficult, must be done in small phases, to manage complexity Requires a new way of thinking