Home » Redis with SpringBoot3 Native

Redis with SpringBoot3 Native

Last Updated on July 8, 2023 by KnownSense

Objective:
To create an aot executable/ native-image of a java based Microservice in SpringBoot3 and integration with Redis for cache.

Understanding the Basics

What is Redis

Redis (REmote DIctionary Server) is an open source, in-memory, NoSQL key/value store that is used primarily as an application cache or quick-response database.
It is also known for its fast read and write operations, rich data types, and advanced memory structure. It is ideal for developing high-performance, scalable web applications.
It is also being used by major industry players and wide scale organizations.

There is also Redis stack that extends Redis with modern data models and processing engines to provide a complete developer experience.
Some of the use cases of Redis stack are:
1. Document database
2. Telemetry
3. Identity and Resource Management
4. Fraud Detection etc.

Implementation:
To implement Redis in SpringBoot3 for native-image development, we need to follow the below steps:
A. Add these in pom.xml
for redis:

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
   


for native:

              <plugin>
	              <groupId>org.graalvm.buildtools</groupId>
	              <artifactId>native-maven-plugin</artifactId>
              </plugin>

B. Create a Class named AppRuntimeHints which implements RuntimeHintsRegistrar. These runtime hints are added so that reflections and proxy classes can be specified upfront to the native compiler at the build time, else the application will fail.

public class AppRuntimeHints implements RuntimeHintsRegistrar {
    @Override
    public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
        hints.serialization().registerType(ContractModel.class).registerType(ContractInfo.class).registerType(Date.class).registerType(Number.class).registerType(Long.class);
   }
}

C. Once the AppRuntimeHints class is ready (point C), we need to let Spring context aware that this class is present and has to be included during the build phase.
For the same below import is done on the SpringBoot main application class.


@ImportRuntimeHints(AppRuntimeHints.class)

Authored by codingknownsense.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top