This talk, given at JDK.io (recording may be available through them at some point) is a whirlwind introduction to RxJava, N1QL and reactive programming in general. It also talks about resiliency and various patterns you are likely to need.
Reactive Data Access with RxJava…and N1QL!Michael Nitschinger, Couchbase Inc.@daschl
View Slide
©2016 Couchbase Inc. 2§ New challenges§ React to user load§ React to failure§ Be responsive under load and failure§ Need new solutions§ Decoupled, event-driven architectures§ Optimal resource utilizationReactive?2
©2016 Couchbase Inc. 33
©2016 Couchbase Inc. 4§ RxJava 1§ Reactive Streams (http://www.reactive-streams.org/)§ RxJava 2§ Reactor 3§ Akka StreamsThe Reactive Landscape4
©2016 Couchbase Inc. 5Detour: N1QL5§ Declarative Query Language§ Extends SQL for JSON§ Query UI Built-In§ Full SDK Support§ Based on performant Global Secondary Indexes
©2016 Couchbase Inc. 6Detour: N1QL6
©2016 Couchbase Inc. 7Detour: N1QL7
©2016 Couchbase Inc. 8Detour: Async Java Client Stack8
©2016 Couchbase Inc. 9Detour: Smart BatchingSource: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html9
©2016 Couchbase Inc. 10Detour: Smart BatchingSource: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html10
RxJava 101A Gentle Introduction
©2016 Couchbase Inc. 12§ Java implementation for Reactive Extensionshttps://github.com/ReactiveX§ A library to compose asynchronous and event-drivenprograms through observable sequences.RxJava: Introductionsingle multiplesync T Iterableasync Future Observable12
©2016 Couchbase Inc. 13§ Observables are the duals of Iterables§ They describe both Latency and Error side effects.RxJava: Introductionevent Iterable (pull) Observable (push)data retrieval T next() onNext(T)error discovery throws Exception onError(Exception)completion returns onCompleted()13
©2016 Couchbase Inc. 14Consuming Observables14§ The Subscriber subscribesand receives events.§ A cold Observablestarts when subscribed.§ onNext can be called0..N times
©2016 Couchbase Inc. 15Single15§ Behaves like an Observable§ Emits only a single successful value or an error
©2016 Couchbase Inc. 16Completable16§ Signals Completion or Error for a deferred computation.
©2016 Couchbase Inc. 17RxJava: Creating Observablesjust17
©2016 Couchbase Inc. 18RxJava: Creating Observables18
©2016 Couchbase Inc. 19RxJava: Creating Observables19
©2016 Couchbase Inc. 20RxJava: Creating Observables20
©2016 Couchbase Inc. 21RxJava: Filtering Observables21
©2016 Couchbase Inc. 22RxJava: Filtering Observables22
©2016 Couchbase Inc. 23RxJava: Filtering Observables23
©2016 Couchbase Inc. 24RxJava: Filtering Observables24
©2016 Couchbase Inc. 25RxJava: Transforming Observables25
©2016 Couchbase Inc. 26RxJava: Transforming Observables26
©2016 Couchbase Inc. 27RxJava: Transforming Observables27
©2016 Couchbase Inc. 28RxJava: Transforming Observables28
©2016 Couchbase Inc. 29RxJava: Transforming Observables29
©2016 Couchbase Inc. 30RxJava: Transforming Observables30
©2016 Couchbase Inc. 31RxJava: Transforming Observables31
Reactive N1QLCranking Queries up to Eleven!
©2016 Couchbase Inc. 33§ Simple§ Executes a single N1QL statement, no options available.§ Parameterized§ Executes a parameterized query with positional or named params§ Prepared Query supported through adhoc(false)Query Types33
©2016 Couchbase Inc. 34§ (Async) N1qlQueryResult§ Observable rows()§ Observable errors()§ Observable info()§ Observable signature()§ Observable finalSuccess()§ Observable status()§ boolean parseSuccess()§ String requestId()§ String clientContextId()Query Response34
©2016 Couchbase Inc. 35Simple Query35
©2016 Couchbase Inc. 36Parameterized Query36§ Named Params
©2016 Couchbase Inc. 37Parametrized Query37§ Positional Params
©2016 Couchbase Inc. 38Index Handling38
©2016 Couchbase Inc. 39Index Handling39
©2016 Couchbase Inc. 40Conditional Index Creation40
©2016 Couchbase Inc. 41Transparent Query Caching41
Error Handlingwith RxJava
©2016 Couchbase Inc. 43§ Things will go wrong, so better plan for it§ Do not trust integration points (including the SDK)§ Synchronous retry & fallback is too damn hard§ RxJava provides (almost) everything you need to§ fallback§ retry§ fail fastPreparing to Fail43
©2016 Couchbase Inc. 44Timeouts44§ 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.
©2016 Couchbase Inc. 45Timeouts: Simple45
©2016 Couchbase Inc. 46Timeouts: Synchronous API46
©2016 Couchbase Inc. 47Timeouts: Complex Example47
©2016 Couchbase Inc. 48Timeouts: Complex Example48
©2016 Couchbase Inc. 49Coordinated Retry49§ 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!)
©2016 Couchbase Inc. 50Coordinated Fallback50
©2016 Couchbase Inc. 51Coordinated Retry: Builder51§ Declarative API instead of complicated retryWhen
Thank you.