Redis is a powerful in-memory data store often used for caching, real-time analytics, and session management. If you’re working with Spring Boot, integrating Redis can enhance your application's performance. But setting it up manually—especially across different environments—can become cumbersome.
This guide shows how to set up Redis using Docker and configure it in a Spring Boot application using
application.properties
. We’ll cover both development and production environments, with production assuming a Kubernetes deployment.
Why Docker for Redis?
Docker makes running services like Redis incredibly simple. You don’t have to install anything directly on your machine or server. Everything runs inside a container, which can be torn down or replicated easily.
For development, Docker gives you quick access to a Redis instance. For production (especially in Kubernetes), Docker containers let you scale and maintain Redis reliably.
Step 1: Run Redis in Docker (Development)
For local development, you can run a Redis container with a single command:
docker run -d --name redis-dev -p 6379:6379 redis
This will start a Redis container with the default port exposed on your localhost. No passwords or configs are needed for development use.
Step 2: Add Redis Dependency to Spring Boot
Add the following dependency to your
pom.xml
to enable Redis support in your Spring Boot app:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
This pulls in all the necessary components for Spring Data Redis to function.
Step 3: Configure application-dev.properties
Create a
application-dev.properties
file and add your Redis configuration:
spring.redis.host=localhost
spring.redis.port=6379
This points your application to the Redis container running locally on your machine.
If you want to use a Redis database index (optional), you can also add:
spring.redis.database=0
To enable this profile when running your app locally, use:
SPRING_PROFILES_ACTIVE=dev
Step 4: Production Setup in Kubernetes
In production, you'll want Redis to run in a more controlled and scalable environment. Typically, Redis would be deployed as a Kubernetes pod, possibly using Helm or a managed Redis service. Regardless of the setup, your app will connect to it using environment variables injected into the container.
Step 4.1: Create a ConfigMap for Redis Settings
Define a ConfigMap in Kubernetes to store your Redis host and port:
apiVersion: v1
kind: ConfigMap
metadata:
name: redis-config
namespace: your-namespace
data:
redis_host: redis-service
redis_port: "6379"
If your Redis deployment requires a password, you should store that in a Secret.
Step 4.2: Inject Environment Variables into Deployment
Update your Kubernetes deployment YAML to pass these values into your Spring Boot app:
containers:
- name: your-app
image: your-app-image
env:
- name: REDIS_HOST
valueFrom:
configMapKeyRef:
name: redis-config
key: redis_host
- name: REDIS_PORT
valueFrom:
configMapKeyRef:
name: redis-config
key: redis_port
- name: SPRING_PROFILES_ACTIVE
value: prod
This way, your application can remain environment-agnostic while still being configurable.
Step 5: Configure application-prod.properties
In your
application-prod.properties
file, use environment variable placeholders:
spring.redis.host=${REDIS_HOST}
spring.redis.port=${REDIS_PORT}
Spring Boot will automatically replace these values with the environment variables injected by Kubernetes.
You can also secure your Redis instance by enabling SSL or requiring a password. For example:
spring.redis.password=${REDIS_PASSWORD}
In that case, make sure to pass
REDIS_PASSWORD
from a Kubernetes Secret.
Step 6: Using Spring Profiles for Clean Separation
To keep your configuration neat and manageable, define separate profiles for dev and prod:
application-dev.properties
: used during local development
application-prod.properties
: used in production
Switch between them using the
SPRING_PROFILES_ACTIVE
environment variable. This way, your dev setup can be quick and loose, while your prod setup stays secure and robust.
image quote pre code