Redis Integration with Quarkus
One of the greatest strengths of the Java programming language is the tremendous diversity of different Java libraries and frameworks for developers. From Google Guava for adding new data types and functionalities, to the lightweight, modular Spring framework, developers have no shortage of options when building new Java applications.
Since its release in 2019, the Quarkus framework has been gaining popularity for developers using Kubernetes and containerized applications. Of course, large, robust Java software also requires a database to store enterprise data. These databases are implemented using technologies such as Redis. The 2020 Stack Overflow developer survey has found that Redis remains the “most loved” database technology among users for several years in a row.
But is it possible to get Quarkus and Redis to work together? What is Quarkus, exactly, and how can you integrate Redis with Quarkus?
What Is Quarkus?
Quarkus is a “Kubernetes-native Java stack tailored for OpenJDK HotSpot and GraalVM.”
More specifically, Quarkus is a full-stack Java framework to help developers build Kubernetes-native Java applications. These applications can then run either as natively compiled executables or on Java virtual machines (JVMs). The two JVM options when using Quarkus are GraalVM and OpenJDK HotSpot.
- HotSpot is the virtual machine solution used by the open-source OpenJDK implementation of Java.
- GraalVM is developed by Oracle and based on OpenJDK HotSpot, but includes multiple features and functionality intended to improve on HotSpot.
Quarkus adopts a “container-first” philosophy and focuses on very fast boot times and low memory consumption. To improve startup times, processing is offloaded to build time whenever possible. In addition, Quarkus has been designed with developers’ productivity and efficiency in mind, helping organizations build and deploy new applications more quickly when responding to evolving conditions.
Tasks such as compilation, processing annotations, building framework metamodels, and constructing the final JAR file are all done by Quarkus when building the application, instead of at runtime. As a result, Quarkus applications can start up in tens of milliseconds, allowing for rapid automatic scaling of containerized microservices.
How to Integrate Redis with Quarkus
Redis is an open-source in-memory data structure store that is used to implement NoSQL key-value databases, application caches, and message brokers. There are many good reasons to use Redis for your next project, including:
- Flexibility: Redis has a rich set of pre-built data structures to work with, including strings, lists, sets, hashes, and bit arrays.
- High availability: Redis uses a primary-replica architecture, and the size of your Redis cluster can easily be scaled up or down when necessary.
- In-memory: Redis data lives in main memory, unlike with other database alternatives, which results in very fast read and write operations.
More specifically, many Java developers choose to work with a third-party Redis Java client such as Redisson. Redisson is a third-party, open-source Redis Java client that provides many different implementations of Java distributed objects, collections, and constructs. By offering this familiar structure and underpinning, Redisson makes it much easier for developers to use Redis with Java.
The good news is that Redisson comes with support for Quarkus and other Java frameworks. Below is the simple three-step process for integrating Redis with Quarkus in Redisson.
1. Add the redisson-quarkus dependency to your project
If you’re using Maven for build automation, add the following text to your project file:
<dependency> <groupId>org.redisson</groupId> <!-- for Quarkus v1.6.x - v1.13.x --> <artifactId>redisson-quarkus-16</artifactId> <!-- for Quarkus v2.0.x --> <artifactId>redisson-quarkus-20</artifactId> <version>3.16.1</version> </dependency>
If you’re using Gradle, add the following text:
// for Quarkus v1.6.x - v1.13.x compile 'org.redisson:redisson-quarkus-16:3.16.1' // for Quarkus v2.0.x compile 'org.redisson:redisson-quarkus-20:3.16.1'
Note that the text varies slightly, depending on the version of Quarkus that you want to use.
2. Add settings to the application.properties file
The application.properties file is a configuration file that is structured in YAML format. Redisson offers multiple configuration possibilities depending on your needs, including single mode, replicated mode,cluster mode,sentinel mode, and proxy mode. Below is an example of how to configure Redisson for working with Quarkus in single mode:
quarkus.redisson.single-server-config.address=redis://localhost:6379 quarkus.redisson.single-server-config.password=null quarkus.redisson.threads=16 quarkus.redisson.netty-threads=32
Note that names in camel case are joined using the hyphen character instead.
3. Start using Redisson
Last but not least, to integrate Quarkus with Redis, add an @Inject annotation (which denotes a dependency injection) to your Java code. For example:
@Inject RedissonClient redisson;
And that’s it! You’re now ready to start using Redis with Quarkus.
Redisson dramatically simplifies the process of integrating Redis with Quarkus and other Java frameworks. Need the best performance and guaranteed support? Consider upgrading to Redisson PRO, which offers additional features, blazing-fast speed, and 24/7 technical support with a service-level agreement (SLA).