Advanced Redis Integration With Micronaut
The Java programming language has a rich array of libraries and frameworks, giving developers tremendous choice and flexibility. One of these solutions is Micronaut, which describes itself as “a modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications.”
Unfortunately, it can sometimes be challenging to use Micronaut and other Java frameworks in combination with other parts of your technology stack—such as Redis, an open-source project for building fast in-memory databases. So is it possible to integrate Redis with Micronaut? Keep reading for the answers.
What Is Micronaut?
Micronaut is an open-source Java virtual machine (JVM) framework for building software with service-based architectures in the Java, Kotlin, or Groovy programming languages. Micronaut offers advantages such as quick startup times and low memory consumption, which it achieves by performing dependency injection at build time instead of at runtime.
In addition to microservices applications, Micronaut is also highly useful for serverless computing and building Internet of Things (IoT) applications and Android mobile apps. Any Java developer looking to build software with a lightweight, modular architecture, high scalability, and high performance would do well to consider Micronaut.
How to Integrate Redis With Micronaut
Redisson: a third-party, open-source Redis Java client that reimplements many familiar Java distributed objects, collections, and constructs. Using Redisson can dramatically simplify the Redis development process for Java programmers.
The even better news is that Redisson includes built-in support for Micronaut and other common Java frameworks. More specifically, Redisson supports implementations of SessionStore and Cache in Micronaut. Below, we’ll go over the three simple steps for integrating Redis with Micronaut in Redisson.
1. Add the redisson-micronaut dependency to your project
As of writing, Redisson includes support for Micronaut versions 2.0.x through 2.5.x. If you’re using Maven for build automation, add the following text to your project file:
<dependency> <groupId>org.redisson</groupId> <!-- for Micronaut v2.x.x --> <artifactId>redisson-micronaut-20</artifactId> <!-- for Micronaut v3.x.x --> <artifactId>redisson-micronaut-30</artifactId> <!-- for Micronaut v4.x.x --> <artifactId>redisson-micronaut-40</artifactId> <version>3.32.0</version> </dependency>
If you’re using Gradle, add the following text:
// for Micronaut v2.x.x compile 'org.redisson:redisson-micronaut-20:3.32.0' // for Micronaut v3.x.x compile 'org.redisson:redisson-micronaut-30:3.32.0' // for Micronaut v4.x.x compile 'org.redisson:redisson-micronaut-40:3.32.0'
2. Add settings to the application.yml file
Next, you need to add the appropriate Redisson settings to your application.yml file. This is a configuration file structured in YAML format. Below is an example of how to configure Redisson for working with Micronaut:
redisson: single-server-config: address: "redis://127.0.0.1:6379" threads: 16 netty-threads: 32
Note that names in camel case are joined using the hyphen character instead.
In addition to the Redisson settings, you’ll also need to configure the cache or session store settings, depending on how you want to use Micronaut. Redisson supports the following caching options:
- redisson.caches.* (open-source or Redisson PRO version)
- redisson.local-caches.* (Redisson PRO only)
- redisson.clustered-caches.* (Redisson PRO only)
- redisson.clustered-local-caches.* (Redisson PRO only)
When implementing Micronaut’s SessionStore in Redisson, you can adjust the following options:
- enabled: A Boolean variable that enables the session store.
- key-prefix: An integer string prefix applied to all objects stored in Redis.
- codec: Specifies the Redis data codec applied to cache entries (the default is MarshallingCodec).
- update-mode: Defines the update mode for session attributes (the default is AFTER_REQUEST). If WRITE_BEHIND, session changes are stored asynchronously. If AFTER_REQUEST, session changes are stored only when invoking the save() method.
- broadcastSessionUpdates: A Boolean variable that determines whether session updates are broadcasted across all Micronaut services.
Below is an example configuration for Redisson’s implementation of SessionStore in Micronaut:
micronaut: session: http: redisson: enabled: true update-mode: "WRITE_BEHIND" broadcast-session-updates: false
For the full details about configuring Micronaut settings, check out our GitHub page on Redis integration with Micronaut.
3. Start using Redisson
Finally, to integrate Micronaut with Redis, simply add the @Inject annotation (which denotes a dependency injection) to your Java code. For example:
@Inject RedissonClient redisson;
That’s it! You’re now ready to start using Redis with Micronaut. The following code is an example of how to use a Micronaut cache with Redis and Redisson:
@Singleton @CacheConfig("my-cache1") public class CarsService { @Cacheable public List<String> listAll() { // ... } @CachePut(parameters = {"type"}) public List<String> addCar(String type, String description) { // ... } @CacheInvalidate(parameters = {"type"}) public void removeCar(String type, String description) { // ... } }
Redisson makes it dramatically easier to use Redis with Micronaut and other Java frameworks. If you’re looking for top-tier performance, blazing-fast speeds, and guaranteed support, consider upgrading to Redisson PRO. With Redisson PRO, you get access to additional features and functionality (such as the local-caches, clustered-caches, and local-clustered-caches options mentioned above), as well as 24/7 technical support.