The concept of reactive programming enables more responsive and scalable programmes by handling asynchronous data streams.
It emphasises on representing data flows as ongoing streams of events so that systems may respond and adjust in real time to shifting circumstances. Reactive programming provides a potent tool for handling complicated interactions and upholding fluid user experiences by utilising the ideas of event-driven and declarative programming.
Its method is particularly useful in situations where event-driven structures and effective handling of asynchronous activities are critical, such as in contemporary web applications and real-time data processing systems.
Project Reactor by Spring is a completely non-blocking foundation with built-in back-pressure support.
It serves as the structural core of the Spring ecosystem’s reactive stack and is included into initiatives like Spring WebFlux, Spring Data, and Spring Cloud Gateway.
While attempting to maintain functional simplicity, this demonstration will serve as a proof-of-concept (POC) for creating reactive REST endpoints supported by a PostgreSQL database and using Redis as a cache, all with the support of Docker containers.
In this app we will be using:
And the following dependencies:
Continuing with the movie theme from previous articles, we start by developing our data model.
Note: Although I’m using records, you are still welcome to utilise classes if you’d like.
Next, let’s build our reactive repository:
Now, let’s create our service:
To better understand how to use the Mono and Flux publishers, I suggest reading this insightful article.
It’s time to create our movie rest controller:
Then, we need a starting script to persist a set of movies in our database. You can use the script in the main class or any configuration class.
Note: The save operation won’t populate our cache, only the findAll will.
As the next part of this demo, some configurations need to take place:
we need a reactive Redis template to persist our movies:
In the resource folder, we create a schema.sql file.
The init script is used at the startup in order to create a table for our movies.
In our compose.yaml file, we define our databases’ services.
Now that we’ve finished building our reactive cached endpoint, let’s put it to use:
Our cache is empty, let’s run the findAll op to fill it up with movies from the PostgreSQL database:
The findAll is successfully returning the list of persisted movies. Let’s check our cache:
Now that our cache is filled, the next findAll will be based on it.
The Spring’s Docker support is the reason why the databases services are running at the same time with the movie service.
Once the app is terminated, all the related services will be terminated as well.
This proof of concept acts as a stepping stone, giving an idea of the enormous potential that may be realised by fully using these cutting-edge technologies. Although the present POC has a constrained scope, its goal is to illustrate the fundamental ideas and skills that may be expanded upon and enhanced.
Blossoming Intelligence: How to Run Spring AI Locally with Ollama
In this short article, we'll look at how easy it is to create a chat bot backend powered by Spring and Olama using the llama 3 model.
Sat, 11th May 2024
Read MoreGetting started with native java apps with GraalVM
Native Image is a technology to ahead-of-time compile Java code to a standalone executable, called a native image. This executable includes the application classes, classes from its dependencies, runtime library classes, and statically linked native code from JDK.
Wed, 10th April 2024
Read MoreLeveraging Spring Reactive, Functional Endpoints, Docker, and MongoDB
Blocking is a feature of classic servlet-based web frameworks like Spring MVC. Introduced in Spring 5, Spring WebFlux is a reactive framework that operates on servers like Netty and is completely non-blocking. Two programming paradigms are supported by Spring WebFlux. Annotations (Aspect Oriented Programming) and WebFlux.fn (Functional Programming).
Thu, 29th February 2024
Read MoreNextJs meets Redux: A simple user PoC
Redux is a powerful state management library primarily used in JavaScript applications, particularly those built with frameworks like React. At its core, Redux provides a predictable state container for managing the state of an application in a more organised and centralised manner. It operates on a unidirectional data flow model, which helps in maintaining the consistency of application state and facilitates easier debugging and testing.
Thu, 15th February 2024
Read MoreMonitor Spring reactive microservices with Prometheus and Grafana: a how-to guide
Micro-services monitoring is a crucial aspect of managing modern, complex software architectures. Unlike traditional monolithic applications, micro-services break down functionality into smaller, independent services that can be developed, deployed, and scaled independently.
Fri, 27th October 2023
Read More