SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Going Reactive
with Relational
Databases
Ivaylo Pashov
20 Feb 2020
Reactive 101
What you might have missed so far
Architectural Shift
The Reactive Manifesto
Architectural Shift
Processor ProcessorPublisher
Internal Synchronous Processing
Processor
Internal ImplementationInternal Asynchronous Processing
Processor
2
1
3
5
4
Asynchronous
Programming
is Hard
Reactive Programming
Flux.interval(Duration.ofSeconds(1)).take(5)
.filter(i -> i % 3 != 0)
.map( i -> i * i )
.subscribe(System.out::println);
Subset of Asynchronous programming where availability of new
information (events) drives the logic forward
Declarative
Subscription
demand/cancel
Reactive Streams
SubscriberPublisher
subscribe
0 … * events
Error
Complete
publish
Transform Filter
?
Aggregate Combine
Reactive Streams - Backpressure
Slow
Operator
Operator
Slow
Operator
Operator
BackpressureBackpressure
Normal Scenario
High Load Scenario
Reactive Streams Landscape
Reactive
Streams
ClientsImpl
Blocking Web Stack
HTTP Server
Servlet API
Web Framework
Application
Persistence
Database
Blocking I/O
Blocking I/O
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
Blocking I/O
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
Backpressure on Insert
@PostMapping(path="/cars", consumes = "application/stream+json")
public Mono<Void> loadCars(@RequestBody Flux<Car> cars) {
return repository.insert(cars).then();
}
Backpressure on Get
@GetMapping(path = "/cars", produces = "application/stream+json")
public Flux<Car> getCars() {
return this.repository.findCars();
}
R2DBC
Mind the Gap between
Reactive and Relational
Bridge
Overview
JDBC R2DBC
• Non-humane API
• Humane API
• Non-blocking
• Back-pressure
• Declarative
• Usually asynchronous
✓
✓
✓
✓
✓
✓
❌
❌
❌
❌
❌
✓
Stack
JDBC R2DBC
JDBC API
Driver
Database
Wire Protocol
Blocking I/O
Connection Pool
Client
R2DBC SPI
Reactive Driver
Database
Client
Wire Protocol
Non-Blocking I/O
R2DBC Pool
R2DBC – Connections
String url = "r2dbc:pool:postgresql://localhost:5432/test";
ConnectionFactory connectionFactory =
ConnectionFactories.get(url);
ConnectionFactory Connection
R2DBC – Query
Flux<String> mails = Mono.from(connectionFactory.create())
.flatMapMany(con ->
con.createStatement("SELECT email FROM account")
.execute()
)
.flatMap(result -> result
.map((row, metadata) -> row.get(0, String.class))
);
Statement
Connection
Result
R2DBC – Transactional Batch Insert
Mono<Void> inserted = Mono.from(connectionFactory.create())
.flatMap(con ->
Mono.from(con.beginTransaction())
.thenMany(
con.createStatement("INSERT INTO TEST VALUES($1)")
.bind("$1", "1").add()
.bind("$1", "2").add()
.bind("$1", "3").execute()
).then()
.delayUntil(r -> con.commitTransaction())
.onErrorResume(e -> Mono.from(con.rollbackTransaction())
.then(Mono.error(e))
)
);
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
Alternatives
• Vert.x Reactive SQL Client
• jasync SQL
• ADBA (discontinued)
• Hibernate RX (experimental, limited features)
Upcoming Features
• Stored Procedures
• ExtendTransaction Support
• Event Provider
Spring Data
R2DBC
The Humane API
Persistence Stacks – Spring Perspective
JDBC API (Blocking)
Hibernate/EclipseLink
R2DBC SPI (Reactive)
Spring Data R2DBC
Spring Data JPA
Dialect
JPA
Dialect
JPA Provider Features
• Advanced Object Relational Mapping
• Schema Generation andValidation
• 2 Levels of Caching
• Lazy Loading
• Deferred Flushing and Dirty Checks
• Batching
What Queries
Would Be
Executed?
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
Spring Data RDBC Models
@Data
@Table("movies")
public class Movie {
@Id
private Long id;
private String title;
private String summary;
private LocalDate releasedAt;
}
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);
}
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();
}
How About
Relations?
Relations and Entity Graph
Relations – Tables vs. Entities
1 N M 1
∞1
1
∞
N M
1
N
1
1
M
1
M
1
N
1
11
1
K
L
1
1
∞
1
∞
1
K
L1
1
1
Microservices and Bounded Context
Aggregates
Entities Lifecycle
1 N M 1
∞1
1
∞
N M
1
N
1
1
M
1
M
1
N
1
11
1
K
L
1
1
∞
1
∞
1
K
L1
1
1
Entities Lifecycle
N M
1
M
1
N
1
11
1
K
L
1
1
∞
1
∞
Microservices and Bounded Context
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
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
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);
}
Limitations
• Derived Query Methods
• Embedded Objects
• Collections of arbitrary Objects
• Versioning and Optimistic Locking
• Paging and Sorting Repositories
• Batching
N + 1 Query Problem
/
1 1 1 1 1
1 1 1 1 2
1 1 1 1 3
1 1 1 1 4
1 1 1 1 5
1 1 1 2 6
1 1 1 2 7
1 1 1 2 8
1 1 1 2 9
Single Query with a Fetch Join:
10 (roots) * 5 * 5 * 5 * 5 = 6250 rows
Join in Parallel:
10 + 10 * (5 * 5 * 5 * 5) = 210 rows
Query in Parallel
Fetch Actors
<<R2DBC>>
Fetch Movie
<<R2DBC>>
Get Rating
<<HTTP>>
Get Biography
<<HTTP>>
Q/A
Thank You for Your Attention!

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Reactive Java (33rd Degree)
Reactive Java (33rd Degree)Reactive Java (33rd Degree)
Reactive Java (33rd Degree)
 
Intro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich AndroidIntro to RxJava/RxAndroid - GDG Munich Android
Intro to RxJava/RxAndroid - GDG Munich Android
 
rx-java-presentation
rx-java-presentationrx-java-presentation
rx-java-presentation
 
Buiilding reactive distributed systems with Akka
Buiilding reactive distributed systems with AkkaBuiilding reactive distributed systems with Akka
Buiilding reactive distributed systems with Akka
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
Flink Forward SF 2017: Stefan Richter - Improvements for large state and reco...
 
Processing large-scale graphs with Google(TM) Pregel
Processing large-scale graphs with Google(TM) PregelProcessing large-scale graphs with Google(TM) Pregel
Processing large-scale graphs with Google(TM) Pregel
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 
BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit BMC Keynote @ 2016 Hadoop Summit
BMC Keynote @ 2016 Hadoop Summit
 
React lecture
React lectureReact lecture
React lecture
 
Reason and GraphQL
Reason and GraphQLReason and GraphQL
Reason and GraphQL
 
A dive into akka streams: from the basics to a real-world scenario
A dive into akka streams: from the basics to a real-world scenarioA dive into akka streams: from the basics to a real-world scenario
A dive into akka streams: from the basics to a real-world scenario
 
Next generation message driven systems with Akka
Next generation message driven systems with AkkaNext generation message driven systems with Akka
Next generation message driven systems with Akka
 
Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJava
 
Reactive Fault Tolerant Programming with Hystrix and RxJava
Reactive Fault Tolerant Programming with Hystrix and RxJavaReactive Fault Tolerant Programming with Hystrix and RxJava
Reactive Fault Tolerant Programming with Hystrix and RxJava
 
Reactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJavaReactive Programming on Android - RxAndroid - RxJava
Reactive Programming on Android - RxAndroid - RxJava
 
Reactive streams processing using Akka Streams
Reactive streams processing using Akka StreamsReactive streams processing using Akka Streams
Reactive streams processing using Akka Streams
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016
 

Ähnlich wie Going Reactive with Relational Databases

Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
Mobicents JSLEE progress and roadmap - Mobicents Summit 2011Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
telestax
 

Ähnlich wie Going Reactive with Relational Databases (20)

Declarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive ProgrammingDeclarative Concurrency with Reactive Programming
Declarative Concurrency with Reactive Programming
 
Reactive Relational Database Connectivity
Reactive Relational Database ConnectivityReactive Relational Database Connectivity
Reactive Relational Database Connectivity
 
Streaming Dataflow with Apache Flink
Streaming Dataflow with Apache Flink Streaming Dataflow with Apache Flink
Streaming Dataflow with Apache Flink
 
Reactive programming for java developers
Reactive programming for java developersReactive programming for java developers
Reactive programming for java developers
 
apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...
apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...
apidays LIVE Australia 2020 - Strangling the monolith with a reactive GraphQL...
 
Android RenderScript on LLVM
Android RenderScript on LLVMAndroid RenderScript on LLVM
Android RenderScript on LLVM
 
GDG DevFest 2015 - Reactive approach for slowpokes
GDG DevFest 2015 - Reactive approach for slowpokesGDG DevFest 2015 - Reactive approach for slowpokes
GDG DevFest 2015 - Reactive approach for slowpokes
 
Reactive java programming for the impatient
Reactive java programming for the impatientReactive java programming for the impatient
Reactive java programming for the impatient
 
NGRX Apps in Depth
NGRX Apps in DepthNGRX Apps in Depth
NGRX Apps in Depth
 
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in ClojurescriptProgscon 2017: Taming the wild fronteer - Adventures in Clojurescript
Progscon 2017: Taming the wild fronteer - Adventures in Clojurescript
 
Apache Spark Streaming: Architecture and Fault Tolerance
Apache Spark Streaming: Architecture and Fault ToleranceApache Spark Streaming: Architecture and Fault Tolerance
Apache Spark Streaming: Architecture and Fault Tolerance
 
Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
Mobicents JSLEE progress and roadmap - Mobicents Summit 2011Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
Mobicents JSLEE progress and roadmap - Mobicents Summit 2011
 
A look at Flink 1.2
A look at Flink 1.2A look at Flink 1.2
A look at Flink 1.2
 
Stefan Richter - A look at Flink 1.2 and beyond @ Berlin Meetup
Stefan Richter - A look at Flink 1.2 and beyond @ Berlin Meetup Stefan Richter - A look at Flink 1.2 and beyond @ Berlin Meetup
Stefan Richter - A look at Flink 1.2 and beyond @ Berlin Meetup
 
Native REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11gNative REST Web Services with Oracle 11g
Native REST Web Services with Oracle 11g
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
From User Action to Framework Reaction
From User Action to Framework ReactionFrom User Action to Framework Reaction
From User Action to Framework Reaction
 
Why scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with thisWhy scala is not my ideal language and what I can do with this
Why scala is not my ideal language and what I can do with this
 
Reactive Spring 5
Reactive Spring 5Reactive Spring 5
Reactive Spring 5
 
Building Web APIs that Scale
Building Web APIs that ScaleBuilding Web APIs that Scale
Building Web APIs that Scale
 

Kürzlich hochgeladen

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Kürzlich hochgeladen (20)

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Going Reactive with Relational Databases

Hinweis der Redaktion

  1. 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
  2. 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
  3. Vert.x with Reactive Postgres is ranking top in TechEmpower
  4. Drupal