Slide 1

Slide 1 text

Reactive Data Access with RxJava …and N1QL! Michael Nitschinger, Couchbase Inc. @daschl

Slide 2

Slide 2 text

©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 utilization Reactive? 2

Slide 3

Slide 3 text

©2016 Couchbase Inc. 3 3

Slide 4

Slide 4 text

©2016 Couchbase Inc. 4 §  RxJava 1 §  Reactive Streams (http://www.reactive-streams.org/) §  RxJava 2 §  Reactor 3 §  Akka Streams The Reactive Landscape 4

Slide 5

Slide 5 text

©2016 Couchbase Inc. 5 Detour: N1QL 5 §  Declarative Query Language §  Extends SQL for JSON §  Query UI Built-In §  Full SDK Support §  Based on performant Global Secondary Indexes

Slide 6

Slide 6 text

©2016 Couchbase Inc. 6 Detour: N1QL 6

Slide 7

Slide 7 text

©2016 Couchbase Inc. 7 Detour: N1QL 7

Slide 8

Slide 8 text

©2016 Couchbase Inc. 8 Detour: Async Java Client Stack 8

Slide 9

Slide 9 text

©2016 Couchbase Inc. 9 Detour: Smart Batching Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 9

Slide 10

Slide 10 text

©2016 Couchbase Inc. 10 Detour: Smart Batching Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 10

Slide 11

Slide 11 text

RxJava 101 A Gentle Introduction

Slide 12

Slide 12 text

©2016 Couchbase Inc. 12 §  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 async Future Observable 12

Slide 13

Slide 13 text

©2016 Couchbase Inc. 13 §  Observables are the duals of Iterables §  They describe both Latency and Error side effects. RxJava: Introduction event Iterable (pull) Observable (push) data retrieval T next() onNext(T) error discovery throws Exception onError(Exception) completion returns onCompleted() 13

Slide 14

Slide 14 text

©2016 Couchbase Inc. 14 Consuming Observables 14 §  The Subscriber subscribes and receives events. §  A cold Observable starts when subscribed. §  onNext can be called 0..N times

Slide 15

Slide 15 text

©2016 Couchbase Inc. 15 Single 15 §  Behaves like an Observable §  Emits only a single successful value or an error

Slide 16

Slide 16 text

©2016 Couchbase Inc. 16 Completable 16 §  Signals Completion or Error for a deferred computation.

Slide 17

Slide 17 text

©2016 Couchbase Inc. 17 RxJava: Creating Observables just 17

Slide 18

Slide 18 text

©2016 Couchbase Inc. 18 RxJava: Creating Observables 18

Slide 19

Slide 19 text

©2016 Couchbase Inc. 19 RxJava: Creating Observables 19

Slide 20

Slide 20 text

©2016 Couchbase Inc. 20 RxJava: Creating Observables 20

Slide 21

Slide 21 text

©2016 Couchbase Inc. 21 RxJava: Filtering Observables 21

Slide 22

Slide 22 text

©2016 Couchbase Inc. 22 RxJava: Filtering Observables 22

Slide 23

Slide 23 text

©2016 Couchbase Inc. 23 RxJava: Filtering Observables 23

Slide 24

Slide 24 text

©2016 Couchbase Inc. 24 RxJava: Filtering Observables 24

Slide 25

Slide 25 text

©2016 Couchbase Inc. 25 RxJava: Transforming Observables 25

Slide 26

Slide 26 text

©2016 Couchbase Inc. 26 RxJava: Transforming Observables 26

Slide 27

Slide 27 text

©2016 Couchbase Inc. 27 RxJava: Transforming Observables 27

Slide 28

Slide 28 text

©2016 Couchbase Inc. 28 RxJava: Transforming Observables 28

Slide 29

Slide 29 text

©2016 Couchbase Inc. 29 RxJava: Transforming Observables 29

Slide 30

Slide 30 text

©2016 Couchbase Inc. 30 RxJava: Transforming Observables 30

Slide 31

Slide 31 text

©2016 Couchbase Inc. 31 RxJava: Transforming Observables 31

Slide 32

Slide 32 text

Reactive N1QL Cranking Queries up to Eleven!

Slide 33

Slide 33 text

©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 Types 33

Slide 34

Slide 34 text

©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 Response 34

Slide 35

Slide 35 text

©2016 Couchbase Inc. 35 Simple Query 35

Slide 36

Slide 36 text

©2016 Couchbase Inc. 36 Parameterized Query 36 § Named Params

Slide 37

Slide 37 text

©2016 Couchbase Inc. 37 Parametrized Query 37 § Positional Params

Slide 38

Slide 38 text

©2016 Couchbase Inc. 38 Index Handling 38

Slide 39

Slide 39 text

©2016 Couchbase Inc. 39 Index Handling 39

Slide 40

Slide 40 text

©2016 Couchbase Inc. 40 Conditional Index Creation 40

Slide 41

Slide 41 text

©2016 Couchbase Inc. 41 Transparent Query Caching 41

Slide 42

Slide 42 text

Error Handling with RxJava

Slide 43

Slide 43 text

©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 fast Preparing to Fail 43

Slide 44

Slide 44 text

©2016 Couchbase Inc. 44 Timeouts 44 § 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.

Slide 45

Slide 45 text

©2016 Couchbase Inc. 45 Timeouts: Simple 45

Slide 46

Slide 46 text

©2016 Couchbase Inc. 46 Timeouts: Synchronous API 46

Slide 47

Slide 47 text

©2016 Couchbase Inc. 47 Timeouts: Complex Example 47

Slide 48

Slide 48 text

©2016 Couchbase Inc. 48 Timeouts: Complex Example 48

Slide 49

Slide 49 text

©2016 Couchbase Inc. 49 Coordinated Retry 49 §  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!)

Slide 50

Slide 50 text

©2016 Couchbase Inc. 50 Coordinated Fallback 50

Slide 51

Slide 51 text

©2016 Couchbase Inc. 51 Coordinated Retry: Builder 51 §  Declarative API instead of complicated retryWhen

Slide 52

Slide 52 text

Thank you.