couchbase live europe 2015: reactive programming with rxjava for big data querying with n1ql

39
Reactive Programming with RxJava for Faster Big Data Michael Nitschinger

Upload: couchbase

Post on 16-Jul-2015

132 views

Category:

Software


0 download

TRANSCRIPT

Reactive Programming with RxJavafor Faster Big Data

Michael Nitschinger

©2015 Couchbase Inc.

New challenges need new solutions

Modern applications need to React to user load

React to failure

Be responsive under load and failure

Decoupled, event-driven architectures are the foundation.

Resources need to be utilized as best as possible. Waiting for IO is bad.

Why Reactive?

2

©2015 Couchbase Inc.

Detour: Java Client Architecture

3

©2015 Couchbase Inc.

Detour: Smart Batching

Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html4

©2015 Couchbase Inc.

Detour: Smart Batching

Source: http://mechanical-sympathy.blogspot.co.at/2011/10/smart-batching.html5

RxJava

A Gentle Introduction

©2015 Couchbase Inc.

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>

7

©2015 Couchbase Inc.

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()

8

©2015 Couchbase Inc.

Consuming Observables

9

The Observer subscribes and receives events.

A cold Observablestarts when subscribed.

onNext can be called0..N times

©2015 Couchbase Inc.

RxJava: Creating Observables

just

10

©2015 Couchbase Inc.

RxJava: Creating Observables

11

©2015 Couchbase Inc.

RxJava: Creating Observables

12

©2015 Couchbase Inc.

RxJava: Creating Observables

13

©2015 Couchbase Inc.

RxJava: Creating Observables

14

©2015 Couchbase Inc.

RxJava: Creating Observables

15

©2015 Couchbase Inc.

RxJava: Transforming Observables

16

©2015 Couchbase Inc.

RxJava: Transforming Observables

17

©2015 Couchbase Inc.

RxJava: Transforming Observables

18

©2015 Couchbase Inc.

RxJava: Transforming Observables

19

©2015 Couchbase Inc.

RxJava: Transforming Observables

20

©2015 Couchbase Inc.

RxJava: Transforming Observables

21

©2015 Couchbase Inc.

RxJava: Transforming Observables

22

©2015 Couchbase Inc.

RxJava: Transforming Observables

23

©2015 Couchbase Inc.

RxJava: Transforming Observables

24

©2015 Couchbase Inc.

RxJava: Filtering Observables

25

©2015 Couchbase Inc.

RxJava: Filtering Observables

26

©2015 Couchbase Inc.

RxJava: Filtering Observables

27

©2015 Couchbase Inc.

RxJava: Filtering Observables

28

Error Handling

with RxJava

©2015 Couchbase Inc.

Resiliency is key

Things will go wrong, so better plan for it

Do not aim for QA, aim for production

Do not trust integration points

Treat the Database (SDK) as an integration point

Reacting to Failure

30

©2015 Couchbase Inc.

Timeouts

31

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.

©2015 Couchbase Inc.

Timeouts: Simple

32

©2015 Couchbase Inc.

Timeouts: Synchronous API

33

©2015 Couchbase Inc.

Timeouts: Complex Example

34

©2015 Couchbase Inc.

Coordinated Retry

35

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!)

©2015 Couchbase Inc.

Coordinated Retry: Fallback

36

©2015 Couchbase Inc.

Coordinated Retry with Delay

37

©2015 Couchbase Inc.

More Patterns

38

Circuit Breaker (https://github.com/Netflix/Hystrix)

Closed if everything okay

Opens once the integration point breaks

Fail fast if open

Rechecking with half-open

Bulkheads Contain errors into manageable compartments

Don’t let your ship sink!

Thank you.