Build a Redis Cluster with Docker Compose and Use It in SpringBoot
This guide walks you through setting up a six-node Redis cluster using Docker Compose, configuring the necessary redis.conf settings, deploying the containers, initializing the cluster, and then integrating the cluster into a SpringBoot application via appropriate application.yml configurations and connection commands.
Redis Cluster Setup
To improve Redis storage capacity and response speed, we build a Redis cluster using Docker Compose. The cluster consists of six nodes: three masters and three slaves, each listening on ports 6391‑6396.
First, download and edit redis.conf to enable clustering and set the ports (6391‑6396) and other cluster parameters:
# Enable cluster mode
cluster-enabled yes
# Set port
port 6391
# Node timeout (ms)
cluster-node-timeout 15000
# Cluster config file
cluster-config-file "nodes-6391.conf"Next, create a docker-compose.yml file that defines six Redis containers. Each container maps its /config directory to the host directory /mydata/redis-cluster/config and uses a redis.sh entrypoint script to start Redis with the appropriate configuration file based on the PORT environment variable.
version: "3"
services:
redis-master1:
image: redis:5.0
container_name: redis-master1
working_dir: /config
environment:
- PORT=6391
ports:
- 6391:6391
- 16391:16391
stdin_open: true
tty: true
network_mode: host
privileged: true
volumes:
- /mydata/redis-cluster/config:/config
entrypoint:
- /bin/bash
- redis.sh
# ... similar definitions for redis-master2, redis-master3, redis-slave1, redis-slave2, redis-slave3Upload the configuration files and redis.sh script to the host directory, then start all containers: docker-compose up -d During startup the containers output logs (shown in the images below).
Enter one of the master containers and initialize the cluster:
# Enter container
docker exec -it redis-master1 /bin/bash
# Create cluster
redis-cli --cluster create \
192.168.6.139:6391 192.168.6.139:6392 192.168.6.139:6393 \
192.168.6.139:6394 192.168.6.139:6395 192.168.6.139:6396 \
--cluster-replicas 1Confirm the creation by typing yes. After the cluster is created, you can connect with redis-cli in either single‑node or cluster mode and view node information with cluster nodes.
Using Redis Cluster in SpringBoot
Modify the SpringBoot application.yml to add the cluster nodes and connection settings:
spring:
redis:
password:
timeout: 3000ms
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
cluster:
nodes:
- 192.168.6.139:6391
- 192.168.6.139:6392
- 192.168.6.139:6393
- 192.168.6.139:6394
- 192.168.6.139:6395
- 192.168.6.139:6396After the configuration, calls to the brand‑detail API will cache data in the Redis cluster. Because each master and its corresponding slave share the same data, the brand information is stored redundantly across the master‑slave pairs.
Configuration file and project source code are available on GitHub:
Configuration: https://github.com/macrozheng/mall-learning/tree/master/document/redis-cluster
Source code: https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-redis
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
