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!

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.

Michael Nitschinger

September 14, 2016
Tweet

More Decks by Michael Nitschinger

Other Decks in Programming

Transcript

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

    View full-size slide

  2. ©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

    View full-size slide

  3. ©2016 Couchbase Inc. 3
    3

    View full-size slide

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

    View full-size slide

  5. ©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

    View full-size slide

  6. ©2016 Couchbase Inc. 6
    Detour: N1QL
    6

    View full-size slide

  7. ©2016 Couchbase Inc. 7
    Detour: N1QL
    7

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  11. RxJava 101
    A Gentle Introduction

    View full-size slide

  12. ©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

    View full-size slide

  13. ©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

    View full-size slide

  14. ©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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. ©2016 Couchbase Inc. 18
    RxJava: Creating Observables
    18

    View full-size slide

  19. ©2016 Couchbase Inc. 19
    RxJava: Creating Observables
    19

    View full-size slide

  20. ©2016 Couchbase Inc. 20
    RxJava: Creating Observables
    20

    View full-size slide

  21. ©2016 Couchbase Inc. 21
    RxJava: Filtering Observables
    21

    View full-size slide

  22. ©2016 Couchbase Inc. 22
    RxJava: Filtering Observables
    22

    View full-size slide

  23. ©2016 Couchbase Inc. 23
    RxJava: Filtering Observables
    23

    View full-size slide

  24. ©2016 Couchbase Inc. 24
    RxJava: Filtering Observables
    24

    View full-size slide

  25. ©2016 Couchbase Inc. 25
    RxJava: Transforming Observables
    25

    View full-size slide

  26. ©2016 Couchbase Inc. 26
    RxJava: Transforming Observables
    26

    View full-size slide

  27. ©2016 Couchbase Inc. 27
    RxJava: Transforming Observables
    27

    View full-size slide

  28. ©2016 Couchbase Inc. 28
    RxJava: Transforming Observables
    28

    View full-size slide

  29. ©2016 Couchbase Inc. 29
    RxJava: Transforming Observables
    29

    View full-size slide

  30. ©2016 Couchbase Inc. 30
    RxJava: Transforming Observables
    30

    View full-size slide

  31. ©2016 Couchbase Inc. 31
    RxJava: Transforming Observables
    31

    View full-size slide

  32. Reactive N1QL
    Cranking Queries up to Eleven!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. ©2016 Couchbase Inc. 35
    Simple Query
    35

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  38. ©2016 Couchbase Inc. 38
    Index Handling
    38

    View full-size slide

  39. ©2016 Couchbase Inc. 39
    Index Handling
    39

    View full-size slide

  40. ©2016 Couchbase Inc. 40
    Conditional Index Creation
    40

    View full-size slide

  41. ©2016 Couchbase Inc. 41
    Transparent Query Caching
    41

    View full-size slide

  42. Error Handling
    with RxJava

    View full-size slide

  43. ©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

    View full-size slide

  44. ©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.

    View full-size slide

  45. ©2016 Couchbase Inc. 45
    Timeouts: Simple
    45

    View full-size slide

  46. ©2016 Couchbase Inc. 46
    Timeouts: Synchronous API
    46

    View full-size slide

  47. ©2016 Couchbase Inc. 47
    Timeouts: Complex Example
    47

    View full-size slide

  48. ©2016 Couchbase Inc. 48
    Timeouts: Complex Example
    48

    View full-size slide

  49. ©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!)

    View full-size slide

  50. ©2016 Couchbase Inc. 50
    Coordinated Fallback
    50

    View full-size slide

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

    View full-size slide