Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reactive Data Acces with RxJava, Including N1QL: Couchbase Connect 2015

1,366 views

Published on

This talk shows how to build scalable, reactive, and fault tolerant applications by making use of RxJava and the brand new fully reactive Couchbase Java SDK 2.x. We will also cover stability patterns and how our brand new query language, "N1QL" fits into the picture. This subject is important, as applications that exclusively rely on synchronous data access often hit a scalability wall when responses slow down and thread pools are exhausted. New paradigms, like reactive programming, alleviate the wasting of resources by dispatching them where they can do useful work and provide extensive toolsets to deal with the ever growing demands of web applications.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Reactive Data Acces with RxJava, Including N1QL: Couchbase Connect 2015

  1. 1. REACTIVE DATA ACCESS WITH RXJAVA … INCLUDING N1QL! Michael Nitschinger, Couchbase @daschl
  2. 2. ©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
  3. 3. ©2015 Couchbase Inc. 3 Why Reactive? 3
  4. 4. ©2015 Couchbase Inc. 4 Detour: Async Java Client Stack 4
  5. 5. ©2015 Couchbase Inc. 5 Detour: Smart Batching Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 5
  6. 6. ©2015 Couchbase Inc. 6 Detour: Smart Batching Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html 6
  7. 7. RxJava 101 A Gentle Introduction
  8. 8. ©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
  9. 9. ©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
  10. 10. ©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
  11. 11. ©2015 Couchbase Inc. 11 RxJava: Creating Observables just 11
  12. 12. ©2015 Couchbase Inc. 12 RxJava: Creating Observables 12
  13. 13. ©2015 Couchbase Inc. 13 RxJava: Creating Observables 13
  14. 14. ©2015 Couchbase Inc. 14 RxJava: Creating Observables 14
  15. 15. ©2015 Couchbase Inc. 15 RxJava: Creating Observables 15
  16. 16. ©2015 Couchbase Inc. 16 RxJava: Creating Observables 16
  17. 17. ©2015 Couchbase Inc. 17 RxJava:Transforming Observables 17
  18. 18. ©2015 Couchbase Inc. 18 RxJava:Transforming Observables 18
  19. 19. ©2015 Couchbase Inc. 19 RxJava:Transforming Observables 19
  20. 20. ©2015 Couchbase Inc. 20 RxJava:Transforming Observables 20
  21. 21. ©2015 Couchbase Inc. 21 RxJava:Transforming Observables 21
  22. 22. ©2015 Couchbase Inc. 22 RxJava:Transforming Observables 22
  23. 23. ©2015 Couchbase Inc. 23 RxJava:Transforming Observables 23
  24. 24. ©2015 Couchbase Inc. 24 RxJava:Transforming Observables 24
  25. 25. ©2015 Couchbase Inc. 25 RxJava:Transforming Observables 25
  26. 26. ©2015 Couchbase Inc. 26 RxJava: Filtering Observables 26
  27. 27. ©2015 Couchbase Inc. 27 RxJava: Filtering Observables 27
  28. 28. ©2015 Couchbase Inc. 28 RxJava: Filtering Observables 28
  29. 29. ©2015 Couchbase Inc. 29 RxJava: Filtering Observables 29
  30. 30. Reactive N1QL CrankingQueries up to Eleven!
  31. 31. ©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 QueryTypes 31
  32. 32. ©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
  33. 33. ©2015 Couchbase Inc. 33 Simple Query 33
  34. 34. ©2015 Couchbase Inc. 34 Parameterized Query 34  Named Params
  35. 35. ©2015 Couchbase Inc. 35 Parametrized Query 35  Positional Params
  36. 36. ©2015 Couchbase Inc. 36 Prepared Query 36
  37. 37. ©2015 Couchbase Inc. 37 Index Handling 37
  38. 38. ©2015 Couchbase Inc. 38 Index Handling 38
  39. 39. ©2015 Couchbase Inc. 39 Conditional Index Creation 39
  40. 40. Error Handling with RxJava
  41. 41. ©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
  42. 42. ©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.
  43. 43. ©2015 Couchbase Inc. 43 Timeouts: Simple 43
  44. 44. ©2015 Couchbase Inc. 44 Timeouts: Synchronous API 44
  45. 45. ©2015 Couchbase Inc. 45 Timeouts: Complex Example 45
  46. 46. ©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!)
  47. 47. ©2015 Couchbase Inc. 47 Coordinated Fallback 47
  48. 48. ©2015 Couchbase Inc. 48 Coordinated Retry: Builder 48  Declarative API instead of complicated retryWhen
  49. 49. Thank you.

×