Slide 1

Slide 1 text

Full Steam Ahead! Mark Paluch • Spring Data Project Lead, R2DBC Contributor • @mp911de

Slide 2

Slide 2 text

Reactive Relational Database Connectivity

Slide 3

Slide 3 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Mark Paluch ● Spring Data, R2DBC, Lettuce Redis Driver ● All things non-blocking ● @mp911de (Twitter, GitHub) 3

Slide 4

Slide 4 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 4 2009 Microsoft to release .NET 4.0 2013 Reactive Manifesto Initial Akka release 2014 RxJava 1.0 2015 Reactive Streams 1.0 Project Reactor 2.0 2016 RxJava 2.0 Akka Streams 1.0 Ratpack Spring WebFlux Micronaut Today RSocket Reactive Mongo Lettuce Cassandra vert.x Lagom Apple Servicetalk Helidon RxJava 3.0 Project Reactor 3.0

Slide 5

Slide 5 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Programming Models Have Changed ● Reactive applications typically stream-processing ● Push-based models ● Notifications, Events ● Is SQL the right choice for reactive applications? ● Depends on the Use-Case 5

Slide 6

Slide 6 text

Unless otherwise indicated, these slides are © 2013-2020 vmware 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 6

Slide 7

Slide 7 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Persistence ● ✅ Kafka ● ✅ MongoDB ● ✅ Cassandra ● ✅ Couchbase ● ✅ Redis ● ❌ Oracle, DB2, MySQL, MariaDB, Postgres, SQL Server* 7

Slide 8

Slide 8 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Persistence ● ✅ Kafka ● ✅ MongoDB ● ✅ Cassandra ● ✅ Couchbase ● ✅ Redis ● ❌ Oracle, DB2, MySQL, MariaDB, Postgres, SQL Server* 8

Slide 9

Slide 9 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Non-blocking SQL Persistence (1/3) ● MySQL: X DevAPI (Document Store API): CompletableFuture ● vert.x Postgres Driver: Callback API ● jasync-sql: CompletableFuture ● Common API 9

Slide 10

Slide 10 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Non-blocking SQL Persistence (2/3) ● JDBC on ThreadPool ● RxJava JDBC ● Blocks still threads ● Project Loom: Production readiness unknown ● Requires proper queuing and capacity controls 10

Slide 11

Slide 11 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Non-blocking SQL Persistence (3/3) ● ADBA (Asynchronous Database API) ● Standard-API ● Based heavily on CompletableFuture ● Discontinued in favor of Project Loom 11

Slide 12

Slide 12 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 12 Standard API for reactive programming with SQL databases on the JVM

Slide 13

Slide 13 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Project of

Slide 14

Slide 14 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Design Principles ● Embrace Reactive Types and Patterns ● Low barrier to entry (depends on Reactive Streams and Java 8 only) ● Non-blocking, all the way to the database ● De-duplicate driver efforts ● Documented specification ● Shrink the driver SPI ● Enable multiple "humane" APIs 14

Slide 15

Slide 15 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Simple Select 15 Publisher values = connectionFactory.create() .flatMapMany(conn -> conn.createStatement("SELECT value FROM test") .execute() .flatMap(result -> result.map((row, metadata) -> row.get("value", String.class))))

Slide 16

Slide 16 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC 0.8 ● Stable release ● 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, …) 16

Slide 17

Slide 17 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC 0.9 / 1.0 ● Evolution of the specification ● Extended transaction spec ● Improved In/Out Parameter Bindings ● Investigation on stored procedures ● No real demand so far ● Scheduled for 2021 17

Slide 18

Slide 18 text

Unless otherwise indicated, these slides are © 2013-2020 vmware 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 ● Querydsl ● R2DBC Migrate ● Kotysa ● Testcontainers ● Driver Community ● Google Cloud Spanner ● H2 ● Microsoft SQL Server ● MySQL Driver (r2dbc-mysql, jasync-sql) ● MariaDB ● PostgreSQL ● SAP Hana ● Oracle (work in progress) ● DB2 (work in progress) 18

Slide 19

Slide 19 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Community Projects ● Specification document ● R2DBC SPI ● R2DBC Proxy ● Connection Pooling ● Client Implementations ● Spring ● Querydsl ● R2DBC Migrate ● Kotysa ● Testcontainers ● Driver Community ● Google Cloud Spanner ● H2 ● Microsoft SQL Server ● MySQL Driver (r2dbc-mysql, jasync-sql) ● MariaDB ● PostgreSQL ● SAP Hana ● Oracle (work in progress) ● DB2 (work in progress) 19

Slide 20

Slide 20 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2DBC Clients: Work in Progress ● MyBatis (#1444) ● JDBI (#1454) ● jOOQ (#6298) ● Querydsl (#2468) ● Liquibase (CORE-3419) ● Flyway (#2502) ● Exposed (#456) ● Micronaut (#416) ● Go vote for a client you want to see happen. 20

Slide 21

Slide 21 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What R2DBC gives you ● Move Thread congestion out of JVM ● Achieve more with less Threads ● Doesn’t change law of physics ● Follow well-known SQL database interaction patterns ● Database laws still apply ● Obey wire protocol rules ● ACID rules 21

Slide 22

Slide 22 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring ❤ R2DBC

Slide 23

Slide 23 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring and R2DBC ● 2018/2019 ● First steps with Spring Data R2DBC 1.0 ● 2020 ● Migration of core R2DBC support from Spring Data to Spring Framework 5.3 ● Deprecations in Spring Data R2DBC 1.2 23

Slide 24

Slide 24 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring R2DBC ● Central class: DatabaseClient ● BindMarker SPI ● Reactive Transaction Manager ● Schema initialization support ● ConnectionFactory implementations (e.g. AbstractRoutingConnectionFactory) 24

Slide 25

Slide 25 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ DatabaseClient DatabaseClient client = DatabaseClient.create(connectionFactory); Flux people = client .sql("SELECT firstname, lastname FROM person WHERE age > :age") .bind("age", 42) .filter(statement -> statement.fetchSize(20)) .map(row -> new Person(row.get("firstname", String.class), row.get("lastname", String.class))) .all(); 25

Slide 26

Slide 26 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ ConnectionFactoryInitializer @Bean ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) { ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); initializer.setConnectionFactory(connectionFactory); initializer.setDatabasePopulator(new ResourceDatabasePopulator( new ClassPathResource("schema.sql"))); return initializer; } 26

Slide 27

Slide 27 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data R2DBC ● Mapped entity support through R2dbcEntityOperations ● Query and Update objects ● Dialect-awareness ● Repository abstraction ● Sorry, no mapped relations yet 27

Slide 28

Slide 28 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2dbcEntityTemplate R2dbcEntityTemplate entityTemplate = …; Flux people = entityTemplate.select(Person.class) .from("people") .matching(query(where("age").greaterThan(42)) .limit(10) .offset(20) .sort(by("firstname"))) .all(); 28

Slide 29

Slide 29 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ R2dbcEntityTemplate R2dbcEntityTemplate entityTemplate = …; Flux people = entityTemplate.select(Person.class) .from("people") .matching(query(where("age").greaterThan(42)) .limit(10) .offset(20) .sort(by("firstname"))) .all(); 29 query(where("age").greaterThan(42)) .limit(10) .offset(20) .sort(by("firstname"))

Slide 30

Slide 30 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Repositories interface PersonRepository extends ReactiveSortingRepository { Flux findByFirstnameContains(String name); Flux findFirst10By(); Flux findAllByOrderByLastname(Pageable pageable); @Modifying @Query("DELETE FROM person WHERE age = :age") Mono deleteAllByAge(int age); @Modifying @Query("DELETE FROM person") Mono deleteAllAndReturnCount(); } 30

Slide 31

Slide 31 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data R2DBC 1.2: New and Noteworthy ● Entity Callbacks ● Before/After Convert, Before/After Save ● Auditing ● SpEL Context Extensions ● Enum value revision ● Repository metrics SPI 31

Slide 32

Slide 32 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Entity Callbacks @FunctionalInterface interface BeforeConvertCallback extends EntityCallback { Publisher onBeforeConvert(T entity, SqlIdentifier table); } 32

Slide 33

Slide 33 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Entity Callback in Action @Bean BeforeConvertCallback autogeneratedId(DatabaseClient client) { return (entity, table) -> { if (entity.getId() == null) { return client.sql("SELECT nextval('person_seq')") .map(row -> row.get(0, Integer.class)) .first() .map(entity::withId); } return Mono.just(entity); }; } 33

Slide 34

Slide 34 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Outlook: Spring Data R2DBC 2.0 ● Closer alignment of Spring Data JDBC and Spring Data R2DBC ● Evolution of Spring Data Relational 34

Slide 35

Slide 35 text

Thank you. r2dbc.io @mp911de

Slide 36

Slide 36 text

Unless otherwise indicated, these slides are © 2013-2020 vmware and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Resources ● Website https://r2dbc.io ● Twitter @r2dbc ● GitHub https://github.com/r2dbc ● Mailing List https://groups.google.com/forum/ #!forum/r2dbc ● Monthly Call 1st Friday of Month 0630 PT/0930 ET/1530 CET 36