Advanced Quarkus Redis Cache

Fast, efficient Java applications depend on a robust caching strategy. Quarkus is a Java framework that provides powerful caching capabilities. By default, Quarkus uses Caffeine as its backend. While the Caffeine library is familiar to many Java developers and offers fast performance, it can only function as a local cache. Therefore, distributed applications such as those set up as clusters can't use Caffeine. On the other hand, in-memory data stores offer exceptional caching performance and can be configured to serve distributed applications.

The blazing-fast and powerful Redis is one such in-memory data store that can act as the backend for Quarkus caching. Here's how Java programmers can set up a Quarkus cache with Redis for their high-performance and distributed applications.

What is Quarkus caching?

Quarkus is a full-stack Java framework developed for Java virtual machines (JVMs) and native compilation. As a Kubernetes-native framework, Quarkus is a popular choice for cloud, serverless, and container-based application developers. Quarkus caching stores the results of database queries or complex calculations directly in memory, speeding up subsequent requests for the same data.

How does Quarkus caching work?

As with other enterprise-level cache solutions, Quarkus caching results in faster application response times while reducing the load on expensive server resources such as memory and processing power. However, Quarkus features a unique approach to caching that sets it apart from its peers. By leveraging annotations and interceptors, Quarkus seamlessly integrates caching into Java code. Thus, developers can simply annotate the classes or methods they want to cache.

For these reasons, Quarkus has earned a reputation as a developer-first caching solution. It's also versatile, supporting multiple cache stores, including in-memory data stores, distributed implementations, and custom implementations.

Quarkus caching with Redis

The most demanding applications benefit from a cache with the speed and scalability of an in-memory data store on the backend. Redis is one of the most popular options thanks to its lightning-fast performance, sub-millisecond response times, and persistence options, allowing a cache to survive application restarts.

In addition, Redis supports a wide array of data structures, publish-subscribe messaging, and automatic expiration for cached data. These features bring a fine-grained level of control to Quarkus caching. However, while the default Redis implementation bundled with Quarkus doesn't support local cache and data partitioning in cluster mode for the same cache instance, Redisson offers this and other options. 

Redisson configuration for Quarkus caching with Redis

With Redisson and its Quarkus extension for Redis, Java developers can quickly implement Quarkus caching with a Redis backend for clusters and other setups. This simple dependency integrates Redisson with the Quarkus framework, enabling developers to take advantage of a wide array of Quarkus caching options with Redis on the backend.

The redisson-quarkus-cache dependency allows developers to seamlessly integrate Redisson objects, like RMapCache, into Java code and use the objects for caching. This approach to Quarkus with Redis on the backend also gives developers access to Redisson's advanced features, like object-based caching, providing fine-grained control over caching.

To get started, add the redisson-quarkus-cache dependency to a Java project, like this code for Maven:

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-quarkus-30-cache</artifactId>
    <version>3.33.0</version>
</dependency>

For Gradle, use this code instead:

// for Quarkus v3.x.x
compile 'org.redisson:redisson-quarkus-30-cache:3.33.0'

Next, add settings to the project's application.properties file. Here's an example configuration with an explanation of the settings below:

quarkus.cache.type=redisson
quarkus.cache.redisson.implementation=standard

# Default configuration for all caches
quarkus.cache.redisson.expire-after-write=5s
quarkus.cache.redisson.expire-after-access=1s

# Configuration for `sampleCache` cache
quarkus.cache.redisson.sampleCache.expire-after-write=100s
quarkus.cache.redisson.sampleCache.expire-after-access=10s

Redisson supports multiple cache implementations, including standard and native, as noted by the implementation string above. Redisson PRO additionally supports other cache implementations: v2, localcache, localcache_v2, clustered, and clustered_localcache.

The expire-after-write setting defines the time to live of the item stored in the cache. The default value is 0.

The expire-after-access setting defines the time to live added to the item after a read operation. The default value is 0.

As noted above, the implementation setting defines the type of cache used. The default value is standard.

Among the advanced features of Redisson's various cache implementations are:

  • Local cache: Using the so-called near cache to speed up read operations and avoid network roundtrips, this feature caches map entries on the Redisson side, resulting in read operations up to 45 times faster in comparison to other Quakrus cache implementations
  • Data partitioning: Although the Map object is compatible with clusters, its content isn't scaled or partitioned across multiple Redis master nodes in a cluster. Redisson's data partitioning allows developers to scale available memory, read/write operations, and the entry eviction process for an individual Map instance in a Redis cluster.
  • Scripted entry eviction: The Redis hash structure doesn't support eviction, but Redisson provides this feature through a custom scheduled task that removes expired entries using a Lua script.

For the full list of features and information about Redisson usage, read the Redisson Quarkus extension for Redis documentation.

To learn more about how Java developers can use Redisson, Quarkus, and Redis to achieve a robust and high-performance caching solution, visit the Redisson PRO website today.

Similar articles