Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is R2DBC and how does Spring Data R2DBC differ from Spring Data JPA?

R2DBC is a reactive, non-blocking SPI (a standard interface that database drivers implement) for relational databases, with drivers for PostgreSQL, MySQL, SQL Server, Oracle, H2 and others. Spring Data R2DBC builds on it to map rows to simple objects and offers ReactiveCrudRepository methods that return Mono and Flux, but unlike Spring Data JPA it has no persistence context, no lazy loading, no dirty checking and no relationship mapping. For hand-written queries it offers DatabaseClient and R2dbcEntityTemplate, and for transactions R2dbcTransactionManager with @Transactional or TransactionalOperator.

Why this question is asked

When a team moves to WebFlux, the database is usually the first wall it hits. JDBC is a blocking API, so a JPA repository called from a reactive pipeline blocks the event loop. R2DBC is the answer the ecosystem gives for relational databases, but it is not JPA with a reactive return type, and developers who expect it to be are surprised by what is missing. Interviewers ask this question to see whether a candidate knows both what R2DBC is and what we give up by choosing it. In this answer, we will recap why JDBC does not fit reactive code, see what R2DBC is, write an entity and a repository, compare it point by point with JPA, and look at queries and transactions.

1A Quick Recap: Why JDBC Does Not Fit Reactive Code

JDBC is the classic Java API for relational databases. Every call, such as executeQuery, holds the calling thread until the database answers. In Spring MVC that is fine, because each request has its own thread. In WebFlux, the request runs on one of a few event-loop threads that must never wait, so a JDBC call there stalls every request on that thread. We can move JDBC calls onto boundedElastic, but then we are back to one thread per waiting query, which removes the benefit of going reactive in the first place.

What reactive code needs is a database API where a query returns at once with a publisher, a Mono or Flux, and the rows arrive later as signals, with backpressure so that a slow consumer is not flooded with rows.

2What R2DBC Is

R2DBC, short for Reactive Relational Database Connectivity, is exactly that. It is an SPI, a service provider interface: a set of Java interfaces such as ConnectionFactory, Connection, Statement and Result, built on Reactive Streams types, that each database driver implements. There are drivers for PostgreSQL, MySQL and MariaDB, Microsoft SQL Server, Oracle, H2 and others. A driver talks to the database's network protocol without blocking, using Netty or a similar non-blocking network layer.

The SPI itself is deliberately small and low level, much like plain JDBC. Most applications do not use it directly. On top of it sit r2dbc-pool, a reactive connection pool, and Spring Data R2DBC, which adds mapping, repositories and Spring's transaction support. In Spring Boot, adding spring-boot-starter-data-r2dbc and a driver, and setting spring.r2dbc.url to something like r2dbc:postgresql://localhost:5432/shop, gives us a pooled ConnectionFactory, a DatabaseClient, an R2dbcEntityTemplate and a transaction manager.

Step throughThe reactive data access stack
  1. Our codereturns Mono and Flux
  2. Spring Data R2DBCrepositories, R2dbcEntityTemplate, mapping
  3. DatabaseClientfluent SQL with named parameters
  4. r2dbc-poolreactive connection pool
  5. R2DBC driverfor example r2dbc-postgresql, non-blocking protocol
  6. DatabasePostgreSQL, MySQL, SQL Server, Oracle

6 layers, from the top down. Step through them one at a time.

3An Entity and a Repository

Entities in Spring Data R2DBC are plain objects, often records. We mark the table and the id, and the mapping turns each row into one object.

Java
@Table("orders")
public record Order(@Id Long id, String customer, String status, BigDecimal total) {}
public interface OrderRepository extends ReactiveCrudRepository<Order, Long> {
Flux<Order> findByStatus(String status); // derived query
@Query("SELECT * FROM orders WHERE total > :min")
Flux<Order> findLarge(BigDecimal min);
}
orders.save(new Order(null, "ana", "NEW", new BigDecimal("40.00"))) // Mono<Order>, id 1 after insert
.then(orders.findByStatus("NEW").count()) // Mono<Long>
.subscribe(System.out::println); // 1

The repository looks like a JPA one, but every method returns Mono or Flux. save returns a Mono that, when subscribed, performs an INSERT because the id is null, and emits the saved object with its generated id. When the id is set, save performs an UPDATE of every column. Nothing happens until something subscribes, which in a WebFlux controller is the framework itself.

4What Spring Data R2DBC Does Not Do

This is the heart of the question. Spring Data JPA sits on top of Hibernate, an object-relational mapper with a persistence context: a per-transaction cache of loaded entities that tracks every change. That context is what gives JPA lazy loading (a related collection is fetched when first touched), dirty checking (changed fields are written at commit without calling save) and cascading relationships (@OneToMany saves and deletes children with the parent).

Spring Data R2DBC has none of these. There is no persistence context, so an object we load is just an object: changing a field does nothing until we call save. There is no lazy loading, because touching a field cannot block to run a query in a reactive world. And there is no relationship mapping: an Order cannot have a List<OrderLine> field that the framework fills and saves for us. If we need the lines, we query them ourselves and combine the results. The reason for this design is honesty: every database round trip is visible as a Mono or Flux in our code, instead of hidden behind a getter.

Spring Data JPA compared with Spring Data R2DBC
FeatureSpring Data JPASpring Data R2DBC
I/O modelblocking JDBCnon-blocking R2DBC
Return typesentities, List, OptionalMono and Flux
Persistence contextyes, per transactionnone
Dirty checkingchanges saved at commitonly on explicit save
Lazy loadingyesno
Relationships and cascade@OneToMany, @ManyToOnenone, query and combine yourself
Hand-written queriesJPQL, native SQL, CriteriaSQL, DatabaseClient, Criteria
TransactionsJpaTransactionManagerR2dbcTransactionManager

5Queries With DatabaseClient and R2dbcEntityTemplate

For anything beyond simple repository methods we have two tools. DatabaseClient runs SQL with named parameters and lets us map rows ourselves. R2dbcEntityTemplate offers a fluent, criteria-based API that uses the entity mapping.

Java
Flux<String> names = client.sql("SELECT customer FROM orders WHERE status = :status")
.bind("status", "NEW")
.map((row, meta) -> row.get("customer", String.class))
.all(); // Flux of customer names
Flux<Order> big = template.select(Order.class)
.matching(Query.query(Criteria.where("total").greaterThan(100)))
.all(); // Flux<Order>

Both return publishers and both stream rows as the driver reads them, so a large result does not have to fit in memory at once if the consumer processes rows as they arrive.

6Transactions

Spring Boot configures an R2dbcTransactionManager, a ReactiveTransactionManager. With it, @Transactional works on methods that return Mono or Flux: the transaction begins on subscription and commits when the publisher completes, or rolls back on error. Where we want explicit control, TransactionalOperator wraps a pipeline.

Java
Mono<Void> transfer = debit(from, amount)
.then(credit(to, amount))
.as(transactionalOperator::transactional); // both updates commit together or not at all

Because there are no thread-bound transactions in reactive code, the transaction travels in the Reactor Context of the subscription, not in a ThreadLocal. That is also why mixing JPA and R2DBC in one transaction does not work: they use different transaction managers and different connections.

7Follow-up questions

Can we use R2DBC with Hibernate?

Hibernate Reactive exists, but it is a separate project built on the Vert.x SQL clients rather than on R2DBC, and it is not the same as Spring Data R2DBC. Spring Data R2DBC deliberately stays a simple mapper. If a team needs full ORM features in a reactive service, Hibernate Reactive is the option to evaluate, with its own learning curve.

Is R2DBC faster than JDBC?

Not per query. A single query takes about the same time, because the database does the same work. The benefit is in concurrency: waiting for the database does not hold a thread, so a small number of threads can keep many queries in flight. With virtual threads, JDBC can now get a similar benefit, which changes the trade-off for many services.

Does save() on an existing entity update only changed columns?

No. Without dirty checking, Spring Data R2DBC does not know which fields changed, so save writes all mapped columns of the row. For a partial update, we write an UPDATE statement with DatabaseClient or use R2dbcEntityTemplate.update with an Update object naming the columns.

8Conclusion

In this answer, we saw that R2DBC is a reactive SPI for relational databases with non-blocking drivers for the main databases, and that Spring Data R2DBC adds object mapping, ReactiveCrudRepository methods returning Mono and Flux, DatabaseClient for SQL and R2dbcEntityTemplate for criteria queries, and transactions through R2dbcTransactionManager with @Transactional or TransactionalOperator. The big difference from Spring Data JPA is what is missing: no persistence context, no lazy loading, no dirty checking and no relationship mapping, so every query is explicit in our code.

What to say in the interviewAbout 80 seconds aloud

Open with

R2DBC is Reactive Relational Database Connectivity, a non-blocking SPI that database drivers implement, so there are R2DBC drivers for PostgreSQL, MySQL, SQL Server, Oracle, H2 and others. Queries return publishers, and rows arrive as signals with backpressure, so waiting on the database does not hold a thread.

Then, if they want more

That is what we need in WebFlux, where JDBC would block the event loop. Spring Data R2DBC sits on top: it maps rows to simple objects, often records with @Table and @Id, and gives us ReactiveCrudRepository, where save, findById and derived queries return Mono or Flux. For custom SQL there is DatabaseClient with named parameters, and R2dbcEntityTemplate for criteria-based queries.

Transactions go through R2dbcTransactionManager, either with @Transactional on methods returning Mono or Flux, or with TransactionalOperator. The key difference from Spring Data JPA is that it is not an ORM. There is no persistence context, so no dirty checking, and save writes the whole row.

There is no lazy loading and no relationship mapping, so an order does not automatically load or cascade its lines; I query and combine them myself. That makes it simpler and very explicit, but it means more hand-written code for aggregates.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is R2DBC and how does Spring Data R2DBC differ from Spring Data JPA?": https://scrivio-interview-library.netlify.app/spring-boot/webflux/f07654c37d/