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

Reactive Data Access with RxJava ... and N1QL!

Reactive Data Access with RxJava ... and N1QL!

I gave this talk at Couchbase Connect 2015 in Santa Clara. Check out the couchbase website for the recording!

Michael Nitschinger

June 03, 2015
Tweet

More Decks by Michael Nitschinger

Other Decks in Programming

Transcript

  1. ©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  
  2. ©2015  Couchbase  Inc.   5     Detour:  Smart  Batching

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

      Source:  http://mechanical-­‐sympathy.blogspot.co.at/2011/10/smart-­‐batching.html   6  
  4. ©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<T>   async   Future<T>   Observable<T>   8  
  5. ©2015  Couchbase  Inc.   9   §  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()   9  
  6. ©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  
  7. ©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  
  8. ©2015  Couchbase  Inc.   32   § (Async)  QueryResult   § 

    Observable<AsyncQueryRow>  rows()   §  Observable<JsonObject>  errors()   §  Observable<QueryMetrics>  info()   §  Observable<Object>  signature()   §  Observable<Boolean>  finalSuccess()   §  boolean  parseSuccess()   §  String  requestId()   §  String  clientContextId()   Query  Response   32  
  9. ©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  
  10. ©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.  
  11. ©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!)    
  12. ©2015  Couchbase  Inc.   48   Coordinated  Retry:  Builder  

    48   §  Declarative  API  instead  of  complicated  retryWhen