Relational Databases have been a blocker for systems incorporating a fully reactive stack since the inception of the Reactive Streams specification.
The new Reactive Relational Database Connectivity (R2DBC) can be considered as a game-changer towards achieving efficiency and resiliency on persistence level.
Being one of the early adopters, incorporating R2DBC on an ETL that is processing 5 billion flight tickets per year, I will compare it with the existing alternatives in the same space.
We will go through the existing APIs, the current limitations, the new Spring 5.2 reactive transactions, and some of the most useful constructs and patterns that emerge from using R2DBC.
13. Blocking Web Stack
HTTP Server
Servlet API
Web Framework
Application
Persistence
Database
Blocking I/O
Blocking I/O
14. Blocking Web Stack
HTTP Server
Persistence
T
1
T
2
T
3
T
4
T
5
T
6
T
7
T
8
T
9
T
N
…
Thread Lifetime
1000 RPS 1 RPMS
Database
Blocking I/O
35 ms Blocking I/O
(queries)
5 ms Server Processing
~45 Threads
16. Non-Blocking Web Stack
HTTP Server
Servlet API 3.1
Web Framework
Application
Persistence
Tomcat, Jetty, Netty, Undertow
Database
Non-Blocking I/O
Non-Blocking I/O
Spring Web Flux, Vert.x, Micronaut, Quarkus
Spring Data + Elastic Search, Mongo DB,
Cassandra
25. R2DBC Adoption
Clients
• Spring Data R2DBC
• R2DBC Client
• jOOQ (initial support)
Drivers
• Google Cloud Spanner
• H2
• PostgreSQL
• Microsoft SQL Server
• MySQL
• SAP Hana
• Oracle coming soon -> OJDBC with
Reactive Extensions
29. Persistence Stacks – Spring Perspective
JDBC API (Blocking)
Hibernate/EclipseLink
R2DBC SPI (Reactive)
Spring Data R2DBC
Spring Data JPA
Dialect
JPA
Dialect
30. JPA Provider Features
• Advanced Object Relational Mapping
• Schema Generation andValidation
• 2 Levels of Caching
• Lazy Loading
• Deferred Flushing and Dirty Checks
• Batching
32. Spring Data JDBC
Persistence Stacks – Spring Perspective
JDBC API (Blocking) R2DBC SPI (Reactive)
Spring Data R2DBC
Spring Data Relational Spring Data Relational
JdbcAggregateTemplate
Repositories
DatabaseClient
Reactive Repositories
33. Spring Data RDBC Models
@Data
@Table("movies")
public class Movie {
@Id
private Long id;
private String title;
private String summary;
private LocalDate releasedAt;
}
34. Spring Data RDBC Repositories
@Repository
public interface MovieRepository
extends ReactiveCrudRepository<Movie, Long> {
@Query("SELECT * FROM movies WHERE title = :title")
Mono<Movie> findByTitle(String title);
@Modifying
@Query("DELETE FROM movies WHERE title = :title")
Mono<Integer> deleteByTitle(String title);
}
35. Spring Data RDBC DatabaseClient
private Mono<Movie> findMovie(long movieId) {
return databaseClient.execute(
"SELECT * FROM movies WHERE id = :movieId")
.bind("movieId", movieId)
.as(Movie.class)
.fetch()
.one();
}
44. Aggregates in Spring Data (JDBC) RDBC
@Table("movies")
public class Movie {
@Id
private Long id;
…
private Plot plot;
private Set<Role> cast;
}
One-to-One Embedded Object
Bounded
One-to-Many Embedded Collection
STILL NOT SUPPORTED
EXISTING WORKAROUNDS
45. Aggregates in Spring Data (JDBC) RDBC
@Table("movies")
public class Movie {
@Id
private Long id;
…
private Director director;
private Set<Actor> cast;
}
Many-to-One De-normalize
OR
Many-to-Many Soft reference
NOT ALLOWED
46. Reactive Transactions Spring 5.2 Way
@Transactional
public Mono<Movie> insert(Movie movie) {
return repository.save(movie);
}
private final TransactionalOperator transactionalOperator;
public Mono<Movie> insert(Movie movie) {
return repository.save(movie)
.as(transactionalOperator::transactional);
}
47. Limitations
• Derived Query Methods
• Embedded Objects
• Collections of arbitrary Objects
• Versioning and Optimistic Locking
• Paging and Sorting Repositories
• Batching
Messaging:
ensures loose coupling, isolation and location transparency
Resiliency – Each component needs to be designed operates on its own (containment and isolation); delegate failures as messages
Responsiveness -> Employ pipelining and introduce concurrency; Streaming allows new information to be received as soon as it becomes available
Elastic - consume resources only while active
Location Transparency - embrace the network and all its constraints—like partial failure, network splits, dropped messages, and its asynchronous and message-based nature
by making them first class in the programming model
Vert.x with Reactive Postgres is ranking top in TechEmpower