#1
When building web services with Dropwizard, integrating Redis can really boost performance—whether it's for caching, sessions, or messaging. In this guide, we'll walk through configuring Redis in Dropwizard:
  • Local development using Docker and config-dev.yml
  • Production deployment on Kubernetes using config-prod.yml and environment-based settings
  • A minimal Maven setup with Dropwizard, Redis, and Kubernetes support
Ready? Let’s dive in!

1. Add Dependencies in pom.xml

Ensure your Maven project includes the necessary libraries:
<dependencies>
  <!-- Dropwizard core -->
  <dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-core</artifactId>
    <version>${dropwizard.version}</version>
  </dependency>

  <!-- Redis client (Jedis) -->
  <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>4.4.3</version>
  </dependency>
  
  <!-- Kubernetes client (optional for advanced integration) -->
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-client</artifactId>
    <version>${fabric8.version}</version>
  </dependency>
</dependencies>
This setup gives you a Redis-capable Dropwizard application, with optional Kubernetes support.

2. Run Redis with Docker (Dev Mode)

Start a Redis server locally using Docker Compose. Create docker-compose.yml:
version: '3'
services:
  redis:
    image: redis:7.0-alpine
    ports:
      - "6379:6379"
Run it with:
docker-compose up -d
Redis will now be available at localhost:6379—ideal for development and testing.

3. Dropwizard Dev Configuration: config-dev.yml

Define Redis settings in a development config file:
server:
  applicationConnectors:
    - type: http
      port: 8080

redis:
  host: localhost
  port: 6379
  timeoutMs: 2000
Create a matching config class:
public class RedisConfig {
  @JsonProperty public String host;
  @JsonProperty public int port;
  @JsonProperty public int timeoutMs;
}
Add it to your application config:
public class AppConfig extends Configuration {
  @JsonProperty("redis")
  public RedisConfig redis = new RedisConfig();
}
Initialize your Redis client in the Application:
JedisPool pool = new JedisPool(new JedisPoolConfig(),
  config.redis.host,
  config.redis.port,
  config.redis.timeoutMs
);
Run with:
java -jar target/myapp.jar server config-dev.yml
You’re now connected to local Redis.

4. Prepare for Kubernetes Production

In production, configurations should come from Kubernetes Secrets and ConfigMaps.
Run these commands:
kubectl create configmap redis-config \
  --from-literal=REDIS_HOST=redis-service \
  --from-literal=REDIS_PORT=6379

kubectl create secret generic redis-secret \
  --from-literal=REDIS_TIMEOUT_MS=2000
This stores your Redis endpoint and timeout for later injection.

5. Production Config: config-prod.yml

Create a config file that reads environment variables:
server:
  applicationConnectors:
    - type: http
      port: 8080

redis:
  host: ${REDIS_HOST}
  port: ${REDIS_PORT}
  timeoutMs: ${REDIS_TIMEOUT_MS}
Dropwizard will handle variable substitution automatically.

6. Kubernetes Deployment Snippet

Inject the configuration into your app’s container spec:
env:
  - name: REDIS_HOST
    valueFrom:
      configMapKeyRef:
        name: redis-config
        key: REDIS_HOST
  - name: REDIS_PORT
    valueFrom:
      configMapKeyRef:
        name: redis-config
        key: REDIS_PORT
  - name: REDIS_TIMEOUT_MS
    valueFrom:
      secretKeyRef:
        name: redis-secret
        key: REDIS_TIMEOUT_MS
Launch with:
java -jar myapp.jar server config-prod.yml
Now your app will use the production-grade Redis setup in Kubernetes.

7. Quick Redis Usage in Code

Example health check endpoint using Redis:
@GET
@Path("/cache/{key}")
public String getCache(@PathParam("key") String key) {
  try (Jedis jedis = pool.getResource()) {
    return Key + "=" + jedis.get(key);
  }
}
This simple method shows how easy it is to work with Redis once your connection is configured.

Why This Setup Works

  • Fast iteration locally with Docker and simple YAML configs
  • Production safety—no hardcoded values, all secrets managed through Kubernetes
  • Separation of concerns—dev config stays in config-dev.yml, prod config in config-prod.yml
  • Scalability and security—environment variables make the application ready for container orchestration
By combining Docker, Kubernetes, and Dropwizard's YAML config support, you get a scalable, secure Redis configuration that’s easy to manage across environments. Whether you're caching sessions or building real-time features, this setup ensures reliability and maintainability from local development to live deployment.

image quote pre code