Under the Hood of Reactive Data Access

Under the Hood of Reactive Data Access

Slides to the presentation I gave at Spring I/O 2018

Conference page: https://2018.springio.net/sessions/under-the-hood-of-reactive-data-access
GitHub repository: https://github.com/mp911de/under-the-hood-reactive

C5f6e8dffbb19acf405198c8fb917337?s=128

Mark Paluch

May 24, 2018
Tweet

Transcript

  1. Mark Paluch @mp911de UNDER THE HOOD OF REACTIVE DATA ACCESS

  2. Spring Data Modules JPA

  3. Reactive Spring Data Modules JPA

  4. What about… JDBC? JPA?

  5. Relational Databases •Not a good fit for reactive access •Limited

    scalability •Resource synchronization •ACID Transactions
  6. 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
  7. Query Behavior

  8. Query by Time Transport

  9. Synchronous Data Access Data store App Data store App Data

    store
  10. Reactive Data Access Data store App Data store Data store

    Data store App
  11. Differences to imperative Data Access •Drivers require threading infrastructure •Timeout

    handling different
  12. Synchronous Timeouts Socket.setSoTimeout(…) ⏱

  13. Aynchronous Timeouts •Active timeout tracking required

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

    connect
  15. Application Driver Application Request Server selection Queueing Write Response decoding

    Response buffering Request completion Response
  16. Reactive NoSQL drivers

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

  18. Query by Time Request Response (chunk) Response (complete) Prefetch (cont.d)

    Query Emit elements Result processing
  19. MongoDB Driver Fetching Operator MongoDB Subscriber request(Long.MAX_VALUE) request(Long.MAX_VALUE)

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

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

  22. Specific Features •Cursor size derived from demand •Tailable Cursors (@Tailable)

    •MongoDB 3.6: Change Streams
  23. Redis •Reactive driver (Lettuce 5) •Based on Netty and Project

    Reactor •Singleton connection •Optionally: Pooled connections
  24. Command by Time Request Command Command (subscription) Response (chunk) Response

    (complete)
  25. Specifics •Use ByteBuffer instead byte[] •null handling: Emission of empty

    buffers •Suspend reading if no demand
  26. Apache Cassandra •Reactive bridge over async driver •Netty-based driver (Datastax)

    •Pooled connections
  27. Query by Time Request Response (chunk) Response (complete) Query Emit

    elements Result processing Next page
  28. 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
  29. Couchbase •Reactive driver driver •Based on Netty and RxJava 1

    •Multiple connection schemes
  30. Key-Value, DCP •Request •Response •Emit results
 Search, Query, View •Request

    •Response streaming
  31. Command by Time Request Command Command (subscription) Response (chunk) Response

    (complete)
  32. Outlook •Couchbase driver likely to move to RxJava 2 •No

    tickets/decisions yet
  33. Imperative and Reactive Together

  34. Blocking with Imperative Programming

  35. Blocking with Reactive Programming

  36. Imperative and Reactive @Component public class ApplicationInitializer { ReactiveMongoOperations ops;

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

    @PostConstruct public void postConstruct() { ops.remove(Person.class).all().block(); } }
  38. Event Loop Event Loop (single Thread)

  39. Event Loop Event Loop (single Thread)

  40. Event Loop Process next item Callback Register Callback Return Work

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

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

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

    item Work Callback Work Wait for Request completion Return Request Request Callback
  44. Thou shalt not block

  45. Resources •Slides – mp911.de/uhd-springio18 •Example Code – mp911.de/uhd-code •Spring Data

    Examples – Repository @ Github •Find us on Gitter!
  46. Mark Paluch @mp911de THANKS!