R2DBC Logo

The Reactive Relational Database Connectivity (R2DBC) project brings reactive programming APIs to relational databases.

In a Nutshell

Based on the Reactive Streams specification. R2DBC is founded on the Reactive Streams specification, which provides a fully-reactive non-blocking API.

Works with relational databases. In contrast to the blocking nature of JDBC, R2DBC allows you to work with SQL databases using a reactive API.

Supports scalable solutions. With Reactive Streams, R2DBC enables you to move from the classic “one thread per connection” model to a more powerful and scalable approach.

Provides an open specification. R2DBC is an open specification and establishes a Service Provider Interface (SPI) for driver vendors to implement and clients to consume.

Example Query

ConnectionFactory connectionFactory = ConnectionFactories
  .get("r2dbc:h2:mem:///testdb");

Mono.from(connectionFactory.create())
  .flatMapMany(connection -> connection
    .createStatement("SELECT firstname FROM PERSON WHERE age > $1")
    .bind("$1", 42)
    .execute())
  .flatMap(result -> result
    .map((row, rowMetadata) -> row.get("firstname", String.class)))
  .doOnNext(System.out::println)
  .subscribe();
ConnectionFactory connectionFactory = ConnectionFactories
  .get("r2dbc:h2:mem:///testdb");

Single.fromPublisher(connectionFactory.create()).toFlowable()
  .flatMap(connection -> connection
    .createStatement("SELECT firstname FROM PERSON WHERE age > $1")
    .bind("$1", 42)
    .execute())
  .flatMap(result -> result
    .map((row, rowMetadata) -> row.get("firstname", String.class)))
  .doOnNext(System.out::println)
  .subscribe();
ConnectionFactory connectionFactory = ConnectionFactories
  .get("r2dbc:h2:mem:///testdb");

Uni.createFrom().publisher(connectionFactory.create())
  .onItem().transformToMulti(connection -> connection
    .createStatement("SELECT firstname FROM PERSON WHERE age > $1")
    .bind("$1", 42)
    .execute())
  .onItem().transform(result -> result
    .map((row, rowMetadata) -> row.get("firstname", String.class)))
  .subscribe().with(System.out::println);

Features

Relational Meets Reactive

Existing standards, based on blocking I/O, cut off reactive programming from relational database users. R2DBC specifies a new API to allow reactive code that works efficiently with relational databases.

R2DBC is a specification designed from the ground up for reactive programming with SQL databases. It defines a non-blocking SPI for database driver implementors and client library authors. R2DBC drivers fully implement the database wire protocol on top of a non-blocking I/O layer.

Design Principles

R2DBC aims for a minimal SPI surface, specifying only parts that differ across databases, and is fully reactive and backpressure-aware all the way down to the database. It is intended primarily as a driver SPI to be consumed by client libraries and not intended to be used directly in application code.

Cloud Ready

R2DBC supports cloud-native applications using relational databases such as PostgreSQL, MySQL, and others. Application developers are free to pick the right database for the job without being confined by APIs.

Community

Join the R2DBC Community Forum to learn more about R2DBC, get your R2DBC questions answered, and interact with other R2DBC developers.