Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Programming • High-efficiency applications • Fundamentally non-blocking • No opinion on async • Key differentiators: (pull-push) back pressure, flow control 2
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Subscriber Publisher Subscribe Data Demand
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Subscriber Publisher Subscribe Data request(n)
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Roadblocks • Barriers to using Reactive everywhere • Data Access • MongoDB, Apache Cassandra, Redis • No Relational Database Access 6
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 7 A specification designed from the ground up for reactive programming https://r2dbc.io
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Dependencies • Reactive Streams • Java 8 8
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Design Principles • Embrace Reactive Types and Patterns • Non-blocking, all the way to the database • Documented specification • Shrink the driver SPI • Enable multiple "humane" APIs 9
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Driver SPI • JDBC: same API for humane API and inhumane SPI for alternative clients like JPA, jOOQ, Jdbi, etc. • API that users didn't like using and driver authors didn't like implementing • Duplicating effort implementing the same "humane" affordances like ? binding 10
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Driver SPI: ConnectionFactory 11 Publisher<Connection> create() ConnectionFactoryMetadata getMetadata()
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Great! But a Bit Verbose. • Minimal set of implementation specific operations • Definitely usable, but very verbose and prone to errors • Explicit transaction management is analogous to try-catch- finally-try-catch in JDBC • We need a "humane" client API. In fact we need many humane client APIs! 17
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC Client 18 Flux<String> values = r2dbc.withHandle(handle -> handle.select("SELECT value FROM test") .mapRow(row -> row.get("value", String.class)))
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Parametrized Statement 19 Flux<Integer> updatedRows = r2dbc.withHandle(handle -> handle.createUpdate("INSERT INTO test VALUES($1, $2)") .bind("$1", 100).bind("$2", 200).add() .bind("$1", 300).bind("$2", 400).execute())
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Transactional Update 20 Flux<Integer> updatedRows = r2dbc.inTransaction(handle -> handle.createUpdate("INSERT INTO test VALUES($1, $2)") .bind("$1", 100).bind("$2", 200).add() .bind("$1", 300).bind("$2", 400).execute())
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data R2DBC 22 DatabaseClient client = DatabaseClient.create(connectionFactory); Flux<Person> rows = client .execute("SELECT * FROM person WHERE name = :name") .bind("name", "John Doe") .as(Person.class) .fetch() .all();
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data R2DBC • Functional-reactive declaration of data access • Fluent API • Support for Transactions • Named parameter support (Dialect-aware) • Repositories • Kotlin Coroutines extensions 24
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Atomic Operations • Application and Database transactions • Classic implementations Thread-bound 26
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Transactions • Application and Database transactions • Pattern is still correct 27
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Reactive Transactions • Application and Database transactions • Pattern is still correct • Bind transactional state to Subscription • Reactor Context 28
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Getting Started with R2DBC • Available from Maven Central • start.spring.io 31
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC Connection URL 34 r2dbc:pool:postgresql://localhost:5432/database?key=value ConnectionFactory connectionFactory = ConnectionFactories.get("r2dbc:postgresql://myhost/database? driver=foo");
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Boot Starter for R2DBC • ConnectionFactory configuration • R2DBC TransactionManager • Embedded H2 support • Actuator integration • Schema.sql and Data.sql support • @DataR2dbcTest 35
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC Ecosystem • Specification document • R2DBC SPI • R2DBC Proxy • Connection Pooling • Client Implementations • Spring Data R2DBC • r2dbc-client • Kotysa • Drivers • Google Cloud Spanner • H2 • Microsoft SQL Server • MySQL Driver (r2dbc-mysql, jasync-sql) • MariaDB • PostgreSQL • SAP Hana 38
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Planned R2DBC Drivers • MariaDB (alpha-state by MariaDB Inc.) • Firebird (Jaybird) investigating • Oracle investigating • DB2 foundation in the works (started with a vert.x driver first) • Ask your vendor! 40
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What Can You Do Today? • End-to-end reactive non-blocking database communication • Batching • BLOB/CLOB • Extensive Type Conversion • Savepoints • Transactions • Leveraging Database-specific features • ServiceLoader-based Driver discovery • Connection URLs • Categorized exceptions (Bad grammar, Data integrity violation, …) 43
Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC Roadmap • Started as experiment in early 2018 • Available as 0.8.0 SR2 • 0.9.0: Stored Procedures, Extended transaction spec, EventProvider 44