Slide 1

Slide 1 text

REACTIVE  DATA  ACCESS  WITH  RXJAVA   …  INCLUDING  N1QL! Michael  Nitschinger,  Couchbase @daschl

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

©2015  Couchbase  Inc.   3   Why  Reactive?   3  

Slide 4

Slide 4 text

©2015  Couchbase  Inc.   4   Detour:  Async  Java  Client  Stack   4  

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

RxJava  101   A  Gentle  Introduction  

Slide 8

Slide 8 text

©2015  Couchbase  Inc.   8   §  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   8  

Slide 9

Slide 9 text

©2015  Couchbase  Inc.   9   §  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()   9  

Slide 10

Slide 10 text

©2015  Couchbase  Inc.   10   Consuming  Observables   10   §  The  Observer  subscribes     and  receives  events.   §  A  cold  Observable   starts  when  subscribed.   §  onNext  can  be  called   0..N  times  

Slide 11

Slide 11 text

©2015  Couchbase  Inc.   11   RxJava:  Creating  Observables   just 11  

Slide 12

Slide 12 text

©2015  Couchbase  Inc.   12   RxJava:  Creating  Observables   12  

Slide 13

Slide 13 text

©2015  Couchbase  Inc.   13   RxJava:  Creating  Observables   13  

Slide 14

Slide 14 text

©2015  Couchbase  Inc.   14   RxJava:  Creating  Observables   14  

Slide 15

Slide 15 text

©2015  Couchbase  Inc.   15   RxJava:  Creating  Observables   15  

Slide 16

Slide 16 text

©2015  Couchbase  Inc.   16   RxJava:  Creating  Observables   16  

Slide 17

Slide 17 text

©2015  Couchbase  Inc.   17   RxJava:  Transforming  Observables   17  

Slide 18

Slide 18 text

©2015  Couchbase  Inc.   18   RxJava:  Transforming  Observables   18  

Slide 19

Slide 19 text

©2015  Couchbase  Inc.   19   RxJava:  Transforming  Observables   19  

Slide 20

Slide 20 text

©2015  Couchbase  Inc.   20   RxJava:  Transforming  Observables   20  

Slide 21

Slide 21 text

©2015  Couchbase  Inc.   21   RxJava:  Transforming  Observables   21  

Slide 22

Slide 22 text

©2015  Couchbase  Inc.   22   RxJava:  Transforming  Observables   22  

Slide 23

Slide 23 text

©2015  Couchbase  Inc.   23   RxJava:  Transforming  Observables   23  

Slide 24

Slide 24 text

©2015  Couchbase  Inc.   24   RxJava:  Transforming  Observables   24  

Slide 25

Slide 25 text

©2015  Couchbase  Inc.   25   RxJava:  Transforming  Observables   25  

Slide 26

Slide 26 text

©2015  Couchbase  Inc.   26   RxJava:  Filtering  Observables   26  

Slide 27

Slide 27 text

©2015  Couchbase  Inc.   27   RxJava:  Filtering  Observables   27  

Slide 28

Slide 28 text

©2015  Couchbase  Inc.   28   RxJava:  Filtering  Observables   28  

Slide 29

Slide 29 text

©2015  Couchbase  Inc.   29   RxJava:  Filtering  Observables   29  

Slide 30

Slide 30 text

Reactive  N1QL   Cranking  Queries  up  to  Eleven!  

Slide 31

Slide 31 text

©2015  Couchbase  Inc.   31   § Simple   §  Executes  a  single  N1QL  statement,  no  options  available.   § Parameterized   §  Executes  a  parameterized  query  with  positional  or  named  params   § Prepared   §  Executed  a  previously  prepared  statement   Query  Types   31  

Slide 32

Slide 32 text

©2015  Couchbase  Inc.   32   § (Async)  QueryResult   §  Observable  rows()   §  Observable  errors()   §  Observable  info()   §  Observable  signature()   §  Observable  finalSuccess()   §  boolean  parseSuccess()   §  String  requestId()   §  String  clientContextId()   Query  Response   32  

Slide 33

Slide 33 text

©2015  Couchbase  Inc.   33   Simple  Query   33  

Slide 34

Slide 34 text

©2015  Couchbase  Inc.   34   Parameterized  Query   34   § Named  Params  

Slide 35

Slide 35 text

©2015  Couchbase  Inc.   35   Parametrized  Query   35   § Positional  Params  

Slide 36

Slide 36 text

©2015  Couchbase  Inc.   36   Prepared  Query   36  

Slide 37

Slide 37 text

©2015  Couchbase  Inc.   37   Index  Handling   37  

Slide 38

Slide 38 text

©2015  Couchbase  Inc.   38   Index  Handling   38  

Slide 39

Slide 39 text

©2015  Couchbase  Inc.   39   Conditional  Index  Creation   39  

Slide 40

Slide 40 text

Error  Handling   with  RxJava  

Slide 41

Slide 41 text

©2015  Couchbase  Inc.   41   § 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   41  

Slide 42

Slide 42 text

©2015  Couchbase  Inc.   42   Timeouts   42   § 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 43

Slide 43 text

©2015  Couchbase  Inc.   43   Timeouts:  Simple   43  

Slide 44

Slide 44 text

©2015  Couchbase  Inc.   44   Timeouts:  Synchronous  API   44  

Slide 45

Slide 45 text

©2015  Couchbase  Inc.   45   Timeouts:  Complex  Example   45  

Slide 46

Slide 46 text

©2015  Couchbase  Inc.   46   Coordinated  Retry   46   §  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 47

Slide 47 text

©2015  Couchbase  Inc.   47   Coordinated  Fallback   47  

Slide 48

Slide 48 text

©2015  Couchbase  Inc.   48   Coordinated  Retry:  Builder   48   §  Declarative  API  instead  of  complicated  retryWhen  

Slide 49

Slide 49 text

Thank  you.