Slide 1

Slide 1 text

Mark Paluch @mp911de UNDER THE HOOD OF REACTIVE DATA ACCESS

Slide 2

Slide 2 text

Spring Data Modules JPA

Slide 3

Slide 3 text

Reactive Spring Data Modules JPA

Slide 4

Slide 4 text

What about… JDBC? JPA?

Slide 5

Slide 5 text

Relational Databases •Not a good fit for reactive access •Limited scalability •Resource synchronization •ACID Transactions

Slide 6

Slide 6 text

JDBC •Fully blocking API •Non-blocking API announced: ADBA 
 (JDK-8188051) http://mail.openjdk.java.net/pipermail/jdbc-spec-discuss/ • JDK-8188051: http://hg.openjdk.java.net/jdk/sandbox/shortlog/JDK-8188051-branch

Slide 7

Slide 7 text

Query Behavior

Slide 8

Slide 8 text

Query by Time Transport

Slide 9

Slide 9 text

Synchronous Data Access Data store App Data store App Data store

Slide 10

Slide 10 text

Reactive Data Access Data store App Data store Data store Data store App

Slide 11

Slide 11 text

Differences to imperative Data Access •Drivers require threading infrastructure •Timeout handling different

Slide 12

Slide 12 text

Synchronous Timeouts Socket.setSoTimeout(…) ⏱

Slide 13

Slide 13 text

Aynchronous Timeouts •Active timeout tracking required

Slide 14

Slide 14 text

Reactive Drivers •End-to-end nonblocking •Asynchronous request-response processing •Non-blocking pooling •Non-blocking connect

Slide 15

Slide 15 text

Application Driver Application Request Server selection Queueing Write Response decoding Response buffering Request completion Response

Slide 16

Slide 16 text

Reactive NoSQL drivers

Slide 17

Slide 17 text

MongoDB •Reactive-over-async driver •Cursor lifecycle •Pluggable I/O •Pooled connections Java.NIO

Slide 18

Slide 18 text

Query by Time Request Response (chunk) Response (complete) Prefetch (cont.d) Query Emit elements Result processing

Slide 19

Slide 19 text

MongoDB Driver Fetching Operator MongoDB Subscriber request(Long.MAX_VALUE) request(Long.MAX_VALUE)

Slide 20

Slide 20 text

MongoDB Driver Fetching Filter MongoDB Subscriber request(Long.MAX_VALUE) request(Long.MAX_VALUE)

Slide 21

Slide 21 text

MongoDB Driver Fetching Filter MongoDB Subscriber request(Long.MAX_VALUE) request(1) Dropped element

Slide 22

Slide 22 text

Specific Features •Cursor size derived from demand •Tailable Cursors (@Tailable) •MongoDB 3.6: Change Streams

Slide 23

Slide 23 text

Redis •Reactive driver (Lettuce 5) •Based on Netty and Project Reactor •Singleton connection •Optionally: Pooled connections

Slide 24

Slide 24 text

Command by Time Request Command Command (subscription) Response (chunk) Response (complete)

Slide 25

Slide 25 text

Specifics •Use ByteBuffer instead byte[] •null handling: Emission of empty buffers •Suspend reading if no demand

Slide 26

Slide 26 text

Apache Cassandra •Reactive bridge over async driver •Netty-based driver (Datastax) •Pooled connections

Slide 27

Slide 27 text

Query by Time Request Response (chunk) Response (complete) Query Emit elements Result processing Next page

Slide 28

Slide 28 text

Outlook •Cassandra driver 4.0 in the works •Likely support for reactive execution model •Emit items as they are received https://github.com/datastax/java-driver/tree/4.x • https://datastax-oss.atlassian.net/browse/JAVA-1505

Slide 29

Slide 29 text

Couchbase •Reactive driver driver •Based on Netty and RxJava 1 •Multiple connection schemes

Slide 30

Slide 30 text

Key-Value, DCP •Request •Response •Emit results
 Search, Query, View •Request •Response streaming

Slide 31

Slide 31 text

Command by Time Request Command Command (subscription) Response (chunk) Response (complete)

Slide 32

Slide 32 text

Outlook •Couchbase driver likely to move to RxJava 2 •No tickets/decisions yet

Slide 33

Slide 33 text

Imperative and Reactive Together

Slide 34

Slide 34 text

Blocking with Imperative Programming

Slide 35

Slide 35 text

Blocking with Reactive Programming

Slide 36

Slide 36 text

Imperative and Reactive @Component public class ApplicationInitializer { ReactiveMongoOperations ops; @PostConstruct public void postConstruct() { ops.remove(Person.class).all().block(); } }

Slide 37

Slide 37 text

Imperative and Reactive @Component public class ApplicationInitializer { ReactiveMongoOperations ops; @PostConstruct public void postConstruct() { ops.remove(Person.class).all().block(); } }

Slide 38

Slide 38 text

Event Loop Event Loop (single Thread)

Slide 39

Slide 39 text

Event Loop Event Loop (single Thread)

Slide 40

Slide 40 text

Event Loop Process next item Callback Register Callback Return Work Database Computation File I/O Event Loop (single Thread) Work Queue Work Request Callback

Slide 41

Slide 41 text

Event Loop Process next item Callback Work Event Loop (single Thread) Work Queue Request Callback

Slide 42

Slide 42 text

Event Loop Event Loop (single Thread) Work Queue Process next item Work Callback Work Wait for Request completion Return Request Request Callback

Slide 43

Slide 43 text

Event Loop Event Loop (single Thread) Work Queue Process next item Work Callback Work Wait for Request completion Return Request Request Callback

Slide 44

Slide 44 text

Thou shalt not block

Slide 45

Slide 45 text

Resources •Slides – mp911.de/uhd-springio18 •Example Code – mp911.de/uhd-code •Spring Data Examples – Repository @ Github •Find us on Gitter!

Slide 46

Slide 46 text

Mark Paluch @mp911de THANKS!