#1
Redis is a lightning-fast in-memory data store commonly used for caching, session management, pub/sub messaging, and more. Quarkus, with its reactive core and lightweight nature, pairs well with Redis to build modern, scalable microservices. Whether you're prototyping locally or deploying to production on Kubernetes, setting up Redis in Quarkus is simple—especially when using profile-specific configuration files like application-dev.properties and application-prod.properties.
In this article, we’ll walk through how to configure Redis in a Quarkus project using Docker for development and Kubernetes for production. We'll manage settings via application.properties based on the active profile (dev or prod), using clean, maintainable practices.

Step 1: Add Required Dependencies

To get started, make sure your project’s pom.xml includes the following:
<dependencies>
  <!-- Redis Client for Quarkus -->
  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-redis-client</artifactId>
  </dependency>

  <!-- Kubernetes Config Extension -->
  <dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-kubernetes-config</artifactId>
  </dependency>
</dependencies>
These dependencies enable Redis support in Quarkus and allow the application to read from Kubernetes ConfigMaps and Secrets.

Step 2: Run Redis Using Docker for Development

Before diving into the code, you’ll need a Redis instance. For development, running Redis via Docker is the easiest approach:
docker run --name redis-dev -p 6379:6379 -d redis:latest
This launches Redis on your local machine, accessible at localhost:6379.

Step 3: Create application-dev.properties

In your src/main/resources directory, create a file named application-dev.properties with the following content:
quarkus.redis.hosts=redis://localhost:6379
quarkus.redis.timeout=10s
This tells Quarkus to connect to the local Redis instance. The timeout setting ensures that Redis operations don’t hang indefinitely in case of connection issues. To run your app using this profile:
./mvnw quarkus:dev -Dquarkus.profile=dev
Quarkus will automatically load application-dev.properties.

Step 4: Production Setup with Kubernetes

In production, configuration should be externalized. Kubernetes provides ConfigMaps and Secrets to securely inject configuration values at runtime.

Step 4.1: Create a Redis ConfigMap

Here's a sample ConfigMap containing Redis configuration:
apiVersion: v1
kind: ConfigMap
metadata:
  name: redis-config
  namespace: your-namespace
data:
  redis.hosts: redis://redis-service:6379
Replace redis-service with the internal hostname of your Redis service in Kubernetes.

Step 4.2: Create a Redis Secret (Optional)

If your Redis setup requires authentication, store the password in a Kubernetes Secret:
apiVersion: v1
kind: Secret
metadata:
  name: redis-secret
  namespace: your-namespace
type: Opaque
data:
  redis.password: cmVkaXNwYXNz  # base64-encoded version of 'redispass'

Step 5: Create application-prod.properties

Create another configuration file named application-prod.properties:
quarkus.redis.hosts=${redis.hosts}
quarkus.redis.password=${redis.password}
quarkus.redis.timeout=10s

quarkus.kubernetes-config.enabled=true
quarkus.kubernetes-config.config-maps=redis-config
quarkus.kubernetes-config.secrets=redis-secret
This setup makes use of placeholders that will be resolved from the environment at runtime—perfect for Kubernetes. Quarkus will fetch values from the referenced ConfigMap and Secret automatically when the prod profile is active.

Step 6: Activate Profiles in Kubernetes

In your Kubernetes deployment manifest, specify the profile and ensure the necessary ConfigMaps and Secrets are mounted:
env:
  - name: QUARKUS_PROFILE
    value: prod
This tells Quarkus to activate the prod profile and load the corresponding application-prod.properties file.

Step 7: Testing Redis Integration

To test whether your Quarkus application can connect to Redis, you can inject the Redis client into a REST endpoint:
import io.quarkus.redis.client.RedisClient;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/ping")
public class RedisResource {

    @Inject
    RedisClient redisClient;

    @GET
    public String pingRedis() {
        return redisClient.ping();
    }
}
When you access /ping, the service will return a "PONG" response from Redis, indicating that the connection is active.

image quote pre code