R2DBC Revealed: Reactive Relational Database Connectivity for Java and JVM Programmers
()
About this ebook
Examples in the book are worked using the freely available MariaDB database along with MariaDB’s vendor-implementation of the R2DBC service-provider interface (SPI). Following along with the examples and the provided example code helps prepare you to work with any of the growing number of R2DBC implementations for popular enterprise databases such as Oracle Database and SQL Server. You’ll be well prepared for what is becoming the future of database access from Java and other languages built on the JVM.
What You Will Learn
- Understand why R2DBC was created and how it utilizes the Reactive Streams API
- Understand the components of the R2DBC service-provider interface
- Create and manage reactive database connections and connection pools using an R2DBC client
- Programmatically execute queries on a relational database using an R2DBC client
- Effectively utilize transactions using an R2DBC client
- Build relational database-driven applications that are event-driven and non-blocking
Who This Book Is For
Software developers building solutions using JVM languages and the JVM ecosystem, and developers who need an introduction to the R2DBC specification and reactive programming with relational databases and want to understand what Reactive Relational Database Connectivity is and why it came about. This book includes practical examples of using the R2DBC specification with Java and MariaDB that will provide developers with the knowledge they need to create their own solutions.
Related to R2DBC Revealed
Related ebooks
Building Production-ready Web Apps with Node.js: A Practitioner’s Approach to produce Scalable, High-performant, and Flexible Web Components Rating: 0 out of 5 stars0 ratingsBuilding large scale web apps Rating: 0 out of 5 stars0 ratingsAmazon SimpleDB: LITE Rating: 0 out of 5 stars0 ratingsDataflow and Reactive Programming Systems Rating: 0 out of 5 stars0 ratingsRESTful Java Web Services Interview Questions You'll Most Likely Be Asked: Second Edition Rating: 0 out of 5 stars0 ratingsBeginning jOOQ: Learn to Write Efficient and Effective Java-Based SQL Database Operations Rating: 0 out of 5 stars0 ratingsSuccessful Management of Cloud Computing and DevOps Rating: 0 out of 5 stars0 ratingsJava / J2EE Interview Questions You'll Most Likely Be Asked Rating: 0 out of 5 stars0 ratingsEdge Cloud Operations: A Systems Approach Rating: 0 out of 5 stars0 ratingsBeginning Jakarta EE: Enterprise Edition for Java: From Novice to Professional Rating: 0 out of 5 stars0 ratingsMastering React.js: Modern Web Development Rating: 0 out of 5 stars0 ratingsMulti-Tier Application Programming with PHP: Practical Guide for Architects and Programmers Rating: 0 out of 5 stars0 ratingsBuilding React Apps with Server-Side Rendering: Use React, Redux, and Next to Build Full Server-Side Rendering Applications Rating: 0 out of 5 stars0 ratingsResponsible JavaScript Rating: 0 out of 5 stars0 ratingsJava Web Services Architecture Rating: 4 out of 5 stars4/5Exploring Hadoop Ecosystem (Volume 2): Stream Processing Rating: 0 out of 5 stars0 ratingsReact and React Native Rating: 0 out of 5 stars0 ratingsLearn SQL with MySQL: Retrieve and Manipulate Data Using SQL Commands with Ease Rating: 0 out of 5 stars0 ratingsLearn T-SQL Querying: A guide to developing efficient and elegant T-SQL code Rating: 0 out of 5 stars0 ratingsInter-Service Communication with Go: Mastering protocols, queues, and event-driven architectures in Go (English Edition) Rating: 0 out of 5 stars0 ratingsGetting Started with React Rating: 0 out of 5 stars0 ratingsMongoDB Performance Tuning: Optimizing MongoDB Databases and their Applications Rating: 0 out of 5 stars0 ratingsBuilding Progressive Web Applications with Vue.js: Reliable, Fast, and Engaging Apps with Vue.js Rating: 0 out of 5 stars0 ratingsMulti-Hosting Application & Balloon Services: A Novel Cloud Native Architecture & Its Ecosystem Rating: 0 out of 5 stars0 ratingsMigrating to the Cloud: Oracle Client/Server Modernization Rating: 0 out of 5 stars0 ratingsJavaScript: Optimizing Native JavaScript: Designing, Programming, and Debugging Native JavaScript Applications Rating: 0 out of 5 stars0 ratingsAdvanced Web Development with React: SSR and PWA with Next.js using React with advanced concepts Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming: Crafting Databases, APIs, and Web Servers for High-Performance Backend Rating: 0 out of 5 stars0 ratingsPractical C++ Backend Programming Rating: 0 out of 5 stars0 ratings
Programming For You
Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Game Development with Unreal Engine 5: Learn the Basics of Game Development in Unreal Engine 5 (English Edition) Rating: 0 out of 5 stars0 ratingsExcel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5C# Programming from Zero to Proficiency (Beginner): C# from Zero to Proficiency, #2 Rating: 0 out of 5 stars0 ratingsPython Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Problem Solving in C and Python: Programming Exercises and Solutions, Part 1 Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPython GUI Programming Cookbook - Second Edition Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for R2DBC Revealed
0 ratings0 reviews
Book preview
R2DBC Revealed - Robert Hedgpeth
Part IThe Reactive Movement and R2DBC
© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
R. HedgpethR2DBC Revealedhttps://doi.org/10.1007/978-1-4842-6989-3_1
1. The Case for Reactive Programming
Robert Hedgpeth¹
(1)
Chicago, IL, USA
Buzzword technologies are all too familiar within the software engineering world. For years, some of the most creative minds have given us development innovations that have completely changed the landscape of our solutions. On the other hand, some of those trending technologies have caused us much more pain in the long run. All the hype can be difficult to assess.
In this first chapter, I’d like to address the paradigm of reactive programming , which has been gaining popularity for several years, and help to lay the groundwork for our journey into the idea of Reactive Relational Database Connectivity (R2DBC).
Since you’re reading this book, I’m going to assume that you have likely at least heard or read the words reactive
and programming
paired together before this point. Heck, I’ll even go out on a limb and guess that you might have even heard or read that in order to create a truly reactive solution, you need to think reactively. But what does all of that even mean? Well, let’s find out!
A Traditional Approach
Introducing new technologies can be challenging, but I’ve found that it can help to identify a common, real-world use case that we, as developers, have encountered and see how it fits within that context.
Imagine a basic solution that contains web request workflow between a client application and a server application, and let’s start at the point where there’s an asynchronous request initiated from the client to the server.
Note
When something is occurring asynchronously, it means that there is a relationship between two or more events/objects that interact within the same system but don’t occur at predetermined intervals and don’t necessarily rely on each other's existence to function. Put more simply, they are events that are not coordinated with each other and happen simultaneously.
Upon receiving the request, the server spins up a new thread for processing (Figure 1-1).
../images/504354_1_En_1_Chapter/504354_1_En_1_Fig1_HTML.pngFigure 1-1
Executing a simple, synchronous web request from a client to server
Simple enough, and it runs, so ship it, right? Not so fast! Rarely is it the case that requests are that simple. It could be the case, like in Figure 1-2, that the server thread needs to access a database to complete the work requested by the client. However, while the database is being accessed, the server thread waits, blocking more work from being performed until a response is returned by the database.
../images/504354_1_En_1_Chapter/504354_1_En_1_Fig2_HTML.pngFigure 1-2
The server thread is blocked from performing work while the database returns a response
Unfortunately, that’s probably not an optimal solution as there’s no way to know how long the database call will take. That’s not likely to scale well. So, in an effort to optimize the client’s time and keep it working, you could add more threads to process multiple requests in parallel, like in Figure 1-3.
../images/504354_1_En_1_Chapter/504354_1_En_1_Fig3_HTML.pngFigure 1-3
Subsequent incoming requests are processed using additional threads
Now we’re cooking! You can just continue to add threads to handle additional processing, right? Not so fast. As with most things in life, if something seems too good to be true, it probably is. The trade-off for adding additional threads is pesky problems like higher resource consumption, possibly resulting in decreased throughput, and, in many cases, increased complexity for developers due to thread context management.
Note
For many applications, using multiple threads will work as a viable solution. Reactive programming is not a silver bullet. However, reactive solutions can help utilize resources more efficiently. Read on to learn how!
Imperative vs. Declarative Programming
Using multiple threads to prevent blocking operations is a common approach within imperative programming paradigms and languages. That’s because the imperative approach is a process that describes, step-by-step, the state a program should be in to accomplish a specific goal. Ultimately, imperative processes hinge on controlling the flow of information, which can be very useful in certain situations, but can also create quite a headache, as I alluded to before, with memory and thread management.
Declarative programming, on the other hand, does not focus on how to accomplish a specific goal but rather the goal itself. But that’s fairly vague, so let’s back up a bit.
Consider the following analogies:
Imperative programming is like attending an art class and listening to the teacher give you step-by-step instructions on how to paint a landscape.
Declarative programming is like attending an art class and being told to paint a landscape. The teacher doesn’t care how you do it, just that it gets done.
Tip
Both imperative and declarative programming paradigms have strengths and weaknesses. Like with any task, make sure you choose the right tool for the job!
For this book, I’m going to be using the Java programming languages for all the examples. Now, it’s no secret that Java is an imperative language, and as such its focus is on how to achieve the final result. That said, it’s easy for us to imagine how to write imperative instructions using Java, but what you may not know is that you can also write declarative flows. For instance, consider the following.
Here’s an imperative approach to summing a range of numbers, step-by-step, from one to ten:
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println(sum); // 55
Alternatively, a declarative approach to summing a range of numbers from one to ten involves working with data streams and receiving a result as some unknown, or rather unspecified, time in the future:
int sumByStream = IntStream.rangeClosed(0,10).sum();
System.out.println(sumByStream); // 55
Note
In Java 8, streams were introduced as part of an effort to increase the declarative programming capabilities within the language. IntStream rangeClosed(int startInclusive, int endInclusive) returns an IntStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.
However, for the time being, it’s not important that you understand a Stream object or the underlying IntStream specialization. No, for now, set the idea of a stream
to the side; we’ll get back to it.
The real takeaway here is that both approaches yield the same results, but the declarative approach is merely setting an expectation for the result of an operation, not dictating the underlying implementation steps. This is fundamentally, from a high level, how reactive programming works. Still a little hazy? Let’s dive deeper.
Thinking Reactively
As I mentioned previously, reactive programming, at its core, is declarative. It aims to circumvent blocking states by eliminating many of the issues caused by having to maintain numerous threads. Ultimately, this is accomplished by managing expectations between the client and server.
In fact, pursuing a more reactive approach , upon receiving a request from the client, the server thread calls on the database for processing, but does not wait for a response. This frees up the server thread to continue processing incoming requests. Then, at an undetermined amount of time later, a response, in the form of an event, is received and reacted to by the server thread from the database.