Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Full Steam Ahead, R2DBC!

Mark Paluch
September 03, 2020

Full Steam Ahead, R2DBC!

It’s almost three years since the inception of the open standard Reactive Relational Database Connectivity specification (R2DBC). R2DBC is driven by the community. It started as a research project to explore how reactive applications running on the JVM can integrate with SQL databases using an end-to-end reactive approach. Learn in this session how R2DBC started and what you can do today. We’ll have a look at various database drivers and client projects. If you’re new to R2DBC, then this session will introduce R2DBC key concepts to you and will give you an overview of the specification.

Mark Paluch

September 03, 2020
Tweet

More Decks by Mark Paluch

Other Decks in Programming

Transcript

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

    View full-size slide

  2. Reactive Relational Database Connectivity

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

  5. 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

    View full-size slide

  6. 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

    View full-size slide

  7. 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

    View full-size slide

  8. 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

    View full-size slide

  9. 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

    View full-size slide

  10. 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

    View full-size slide

  11. 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

    View full-size slide

  12. 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

    View full-size slide

  13. 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

    View full-size slide

  14. 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

    View full-size slide

  15. 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))))

    View full-size slide

  16. 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

    View full-size slide

  17. 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

    View full-size slide

  18. 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

    View full-size slide

  19. 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

    View full-size slide

  20. 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

    View full-size slide

  21. 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

    View full-size slide

  22. 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

    View full-size slide

  23. 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

    View full-size slide

  24. 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

    View full-size slide

  25. 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

    View full-size slide

  26. 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

    View full-size slide

  27. 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

    View full-size slide

  28. 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

    View full-size slide

  29. 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"))

    View full-size slide

  30. 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

    View full-size slide

  31. 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

    View full-size slide

  32. 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

    View full-size slide

  33. 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

    View full-size slide

  34. 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

    View full-size slide

  35. Thank you.
    r2dbc.io
    @mp911de

    View full-size slide

  36. 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

    View full-size slide