Couchbase Live Europe 2015: Reactive Programming with RxJava for Big Data Querying with N1QL
Transcript of Couchbase Live Europe 2015: Reactive Programming with RxJava for Big Data Querying with N1QL
©2015 Couchbase Inc.
New challenges need new solutions
Modern applications need to React to user load
React to failure
Be responsive under load and failure
Decoupled, event-driven architectures are the foundation.
Resources need to be utilized as best as possible. Waiting for IO is bad.
Why Reactive?
2
©2015 Couchbase Inc.
Detour: Smart Batching
Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html4
©2015 Couchbase Inc.
Detour: Smart Batching
Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html5
©2015 Couchbase Inc.
Java implementation for Reactive Extensions https://github.com/ReactiveX
A library to compose asynchronous and event-driven programs through observable sequences.
RxJava: Introduction
single multiple
sync T Iterable<T>
async Future<T> Observable<T>
7
©2015 Couchbase Inc.
Observables are the duals of Iterables
They describe both Latency and Error side effects.
RxJava: Introduction
event Iterable<T> (pull) Observable<T> (push)
data retrieval T next() onNext(T)
error discovery throws Exception onError(Exception)
completion returns onCompleted()
8
©2015 Couchbase Inc.
Consuming Observables
9
The Observer subscribes and receives events.
A cold Observablestarts when subscribed.
onNext can be called0..N times
©2015 Couchbase Inc.
Resiliency is key
Things will go wrong, so better plan for it
Do not aim for QA, aim for production
Do not trust integration points
Treat the Database (SDK) as an integration point
Reacting to Failure
30
©2015 Couchbase Inc.
Timeouts
31
The network is unreliable
Servers fail
The SDK contains bugs
Always specify timeouts and deal with them!
The synchronous wrapper defines them for you all the time.
©2015 Couchbase Inc.
Coordinated Retry
35
Fail fast Don’t let your system get stuck
Shed load with backpressure
Retry immediately won’t help
either linear or exponential back-off necessary
have a strategy if retry also doesn’t work (Fallbacks!)
©2015 Couchbase Inc.
More Patterns
38
Circuit Breaker (https://github.com/Netflix/Hystrix)
Closed if everything okay
Opens once the integration point breaks
Fail fast if open
Rechecking with half-open
Bulkheads Contain errors into manageable compartments
Don’t let your ship sink!