Databases 8 min read

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.

macrozheng
macrozheng
macrozheng
Build a Redis Cluster with Docker Compose and Use It in SpringBoot

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:

<code># 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"
</code>

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.

<code>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-slave3
</code>

Upload the configuration files and

redis.sh

script to the host directory, then start all containers:

<code>docker-compose up -d</code>

During startup the containers output logs (shown in the images below).

Enter one of the master containers and initialize the cluster:

<code># 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 1
</code>

Confirm 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:

<code>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:6396
</code>

After 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

DockerDatabaseRedisSpringBootDocker-ComposeRedis Cluster
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.