Build Redis Cluster with Docker Compose: GitHub Example

Build Redis Cluster with Docker Compose: GitHub Example
docker-compose redis cluster github

In the intricate landscape of modern web applications and microservices, data persistence and retrieval at lightning speeds are not merely desirable features; they are foundational pillars for success. As applications scale, the demand for high-performance, resilient data stores becomes paramount. Redis, an in-memory data structure store, renowned for its speed, versatility, and efficiency, stands out as a preferred choice for caching, session management, real-time analytics, and much more. However, a single Redis instance, while powerful, presents a single point of failure and limitations in terms of scalability and data capacity. This is where Redis Cluster steps in, offering automatic sharding, high availability, and the ability to scale horizontally.

While deploying a production-grade Redis Cluster often involves orchestrators like Kubernetes, the journey of development, testing, and even demonstrating such a setup can be significantly simplified using Docker Compose. Docker Compose empowers developers to define and run multi-container Docker applications, making it an ideal tool for quickly spinning up complex distributed systems on a local machine or a CI/CD environment. This article delves into the meticulous process of constructing a fully functional Redis Cluster using Docker Compose, providing a detailed, step-by-step guide akin to a well-documented GitHub repository example. We will explore the underlying concepts, design principles, and practical implementation, ensuring that you gain a profound understanding that extends beyond mere execution. Furthermore, we will contextualize this within the broader ecosystem of scalable applications, touching upon how such a resilient data layer underpins robust APIs, efficient gateways, and truly empowering Open Platforms.

The objective is not just to provide a working configuration but to equip you with the knowledge to understand why each component is configured in a particular way. This approach is crucial for troubleshooting, optimizing, and adapting the setup to various requirements, fostering a deeper engagement with the technology rather than just consuming a ready-made solution. By the end of this extensive guide, you will have a comprehensive understanding of how to leverage Docker Compose to manage a Redis Cluster, paving the way for developing high-performance, resilient applications that can confidently interact with distributed data stores.

The Indispensable Role of Redis in Modern Architectures

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. Its exceptional speed, attributed to its in-memory nature, coupled with its support for various data structures like strings, hashes, lists, sets, sorted sets, streams, and more, makes it an incredibly versatile tool for developers. From accelerating database queries by caching frequently accessed data to managing user sessions in real-time, handling message queues for asynchronous processing, and powering real-time analytics dashboards, Redis has become a cornerstone technology in countless high-performance applications.

In microservices architectures, where services communicate over networks and data consistency and availability are paramount, Redis often serves critical roles. It can act as a distributed cache to reduce the load on databases, a shared state store for services, or a messaging bus for inter-service communication. The ability to perform operations on complex data structures with atomic guarantees also makes it suitable for implementing distributed locks, rate limiters, and leaderboards. However, as application traffic grows and data volumes swell, a single Redis instance inevitably becomes a bottleneck. It possesses finite memory capacity and, being a single process, represents a single point of failure. If that instance goes down, your application could face significant downtime or data loss, depending on how it's configured for persistence. This limitation highlights the necessity for a more robust, scalable, and fault-tolerant solution: Redis Cluster.

Why Redis Cluster? Overcoming the Limitations of a Single Instance

Redis Cluster is Redis's official solution for achieving automatic sharding across multiple Redis nodes, providing high availability and ensuring that data is partitioned across several instances. This addresses the fundamental limitations of a standalone Redis server in several critical ways:

  1. Scalability: A single Redis instance is bound by the memory and CPU resources of the machine it runs on. Redis Cluster allows you to distribute your dataset across multiple nodes, effectively breaking this barrier. As your data grows, you can add more nodes to the cluster, horizontally scaling your storage capacity and read/write throughput. This horizontal scalability is crucial for applications that experience rapid growth in user base or data volume.
  2. High Availability: In a standalone setup, if the single Redis server crashes, your application's access to that data is completely interrupted. Redis Cluster mitigates this risk by employing a master-replica architecture for each shard. Each master node can have one or more replica nodes. If a master node fails, one of its replicas is automatically promoted to become the new master, ensuring continuous operation with minimal downtime. This automatic failover mechanism is a cornerstone of building resilient systems.
  3. Automatic Sharding (Data Partitioning): Redis Cluster automatically partitions your data across its master nodes. The keys are mapped to 16384 hash slots, and each master node is responsible for a subset of these slots. When a client wants to interact with a specific key, it computes the hash slot for that key and then connects to the master node responsible for that slot. This transparent sharding simplifies application development as developers don't need to manually manage data distribution.
  4. Performance: By distributing the dataset and read/write operations across multiple nodes, Redis Cluster can significantly increase the aggregate throughput compared to a single instance. While individual operations on a specific key still target a single node, the cluster as a whole can handle a much larger volume of concurrent requests.

In essence, Redis Cluster transforms Redis from a potent single-point solution into a robust, distributed data store capable of powering mission-critical applications that demand high performance, scalability, and resilience. For scenarios where a single point of failure is unacceptable and data needs to be accessible even during node outages, Redis Cluster is the go-to architecture. It enables applications to maintain their responsiveness and functionality under heavy loads and adverse conditions, which is crucial for delivering a seamless user experience in today's demanding digital landscape.

Understanding Redis Cluster Architecture: The Core Mechanics

To effectively build and manage a Redis Cluster, it's vital to grasp its underlying architecture and how its components interact. A Redis Cluster is not a monolithic entity but a collection of independent Redis instances working collaboratively.

Key Components and Concepts:

  1. Master Nodes and Replica Nodes:
    • Master Nodes: These are the primary nodes that hold a portion of the cluster's data and are responsible for serving read and write requests for the hash slots they manage. To function correctly, a Redis Cluster requires a minimum of three master nodes, though more can be added for increased capacity and fault tolerance.
    • Replica Nodes (Slaves): Each master node can have one or more replica nodes associated with it. Replicas are exact copies of their master's data. Their primary role is to provide high availability: if a master node fails, one of its replicas is automatically promoted to take over its responsibilities, ensuring that the data segment remains accessible. Replicas can also serve read requests, offloading some of the read burden from the masters, though this is primarily for HA and not explicit read scaling as a primary goal of Redis Cluster.
  2. Hash Slots (16384): The entire key space of a Redis Cluster is divided into 16384 fixed hash slots. When a client stores a key, Redis calculates a hash value for that key and maps it to one of these 16384 slots. Each master node in the cluster is assigned a specific contiguous range of these hash slots. For example, Master A might handle slots 0-5460, Master B slots 5461-10922, and Master C slots 10923-16383. This distribution ensures that data is sharded across the master nodes.
  3. Gossip Protocol and Cluster Bus: Redis Cluster nodes communicate with each other using a special TCP port, which is the normal client communication port plus 10000 (e.g., if a node listens on 6379 for client connections, it listens on 16379 for cluster bus communication). This "cluster bus" is used for:
    • Heartbeats: Nodes send periodic pings to check the liveness of other nodes.
    • Failure Detection: If a sufficient number of master nodes (a majority) agree that another master node is unreachable (PFAIL state), it's marked as FAIL, triggering a failover process.
    • Configuration Updates: Nodes exchange information about the cluster topology, such as which node owns which hash slots, new nodes joining, or node promotions.
    • Client Redirection: When a client sends a request for a key to the wrong node (i.e., a node that doesn't own the key's hash slot), the receiving node responds with a MOVED redirection error, indicating the correct node and port. Clients are then expected to re-send the request to the correct node and update their internal routing table.
  4. Cluster Configuration: Each Redis Cluster node stores the entire cluster configuration (nodes, their roles, hash slot assignments, etc.) in a configuration file (e.g., nodes.conf). This file is automatically updated by Redis as the cluster topology changes and is crucial for nodes to join and maintain the cluster state.

How a Redis Cluster Operates: A Simplified Flow

  1. Client Connects: A client can connect to any node in the cluster.
  2. Key Request: The client sends a command for a specific key (e.g., SET mykey "value").
  3. Slot Calculation: The connected node calculates the hash slot for mykey.
  4. Redirection (if necessary):
    • If the connected node is the master responsible for that slot, it processes the request.
    • If not, it sends a MOVED redirection to the client, telling it which node (IP and port) is responsible for that slot. The client then redirects its request to the correct node and updates its internal mapping of slots to nodes. Modern Redis clients often handle this redirection automatically, making the sharding transparent to the application.
  5. Replication: All write operations on a master node are asynchronously replicated to its associated replica nodes.
  6. Failover: If a master node becomes unreachable, a majority of the remaining master nodes will declare it FAIL. One of its replicas will then be elected and promoted to become the new master for its hash slots. Other replicas of the failed master will start replicating from the newly promoted master.

Redis Cluster vs. Redis Sentinel: A Brief Comparison

While both Redis Cluster and Redis Sentinel provide high availability for Redis, they achieve it through different mechanisms and address different scaling needs.

Feature Redis Cluster Redis Sentinel
Primary Goal Horizontal scalability (sharding) & High Availability High Availability (failover)
Data Partitioning Automatic sharding across master nodes No sharding; all nodes store the full dataset
Scalability Scales horizontally by adding more master nodes Scales vertically or by adding more read replicas for reads
Data Capacity Limited only by the aggregate memory of all masters Limited by the memory of a single master node
Complexity More complex setup and management Simpler setup and management
Master-Replica Setup Each shard has a master and optional replicas One master, multiple replicas
Failover Mechanism Nodes communicate directly (gossip protocol) Sentinels monitor nodes and orchestrate failover
Client Interaction Clients connect to any node; redirected to correct shard Clients query Sentinels to find the current master
Use Cases Large datasets, high throughput, distributed cache Moderate datasets, high availability for a single logical DB

For this article, our focus is on Redis Cluster because of its ability to scale both data capacity and throughput horizontally, making it suitable for larger, more demanding applications and as a robust backend for sophisticated APIs and an Open Platform environment.

Docker and Docker Compose Fundamentals: Your Local Orchestra

Before diving into the specifics of building a Redis Cluster, a solid understanding of Docker and Docker Compose is essential. These tools will be our primary orchestrators for defining, deploying, and managing the multiple Redis instances that constitute our cluster.

What is Docker?

Docker is a platform that uses OS-level virtualization to deliver software in packages called containers. These containers are isolated, lightweight, and portable units that bundle an application and all its dependencies (libraries, system tools, code, runtime) together.

  • Containers vs. Virtual Machines (VMs):
    • VMs virtualize the hardware, requiring a full guest operating system for each application, which makes them heavy and slow to start.
    • Containers virtualize the operating system, sharing the host OS kernel. This makes them significantly lighter, faster to start, and consume fewer resources. They provide process isolation and resource governance, ensuring that applications run consistently across different environments, from a developer's laptop to a production server.

The power of Docker lies in its ability to ensure consistency: "if it runs on my machine, it will run on yours." This dramatically simplifies development, testing, and deployment workflows, especially for complex distributed systems like a Redis Cluster where multiple identical instances need to be provisioned.

What is Docker Compose?

While Docker allows you to run individual containers, most real-world applications consist of multiple services (e.g., a web application, a database, a cache like Redis). Manually starting, linking, and managing these interdependent containers can quickly become cumbersome. This is where Docker Compose comes to the rescue.

Docker Compose is a tool for defining and running multi-container Docker applications. You use a YAML file (typically docker-compose.yml) to configure your application's services. With a single command, you can then create and start all the services from your configuration.

Key Docker Compose Concepts:

  1. Services: In a docker-compose.yml file, a "service" defines a container that will be run. Each service specifies the Docker image to use, environment variables, exposed ports, linked volumes, and other configuration parameters specific to that container. For our Redis Cluster, each Redis node (master or replica) will be defined as a separate service.
  2. Networks: Docker Compose creates a default network for your application, allowing all services within that docker-compose.yml to communicate with each other using their service names as hostnames. This simplifies inter-container communication. You can also define custom networks for more granular control over network isolation and topology.
  3. Volumes: Containers are inherently ephemeral; any data written inside a container is lost when the container is removed. Volumes provide a way to persist data generated by Docker containers. By mounting a host directory or a named volume into a container, you ensure that data (like Redis's appendonly.aof or dump.rdb files, or the nodes.conf file for the cluster configuration) persists across container restarts and even removals. This is absolutely critical for a stateful application like a Redis Cluster.

Why Docker Compose for Redis Cluster?

  • Reproducibility: A single docker-compose.yml file encapsulates the entire cluster setup, making it easy for anyone to spin up an identical cluster environment. This is perfect for the "GitHub Example" approach.
  • Simplicity: Instead of running multiple docker run commands with complex networking and volume flags, a simple docker-compose up -d command brings the entire cluster to life.
  • Isolation: Each Redis node runs in its own isolated container, preventing conflicts and ensuring a clean environment.
  • Networking: Docker Compose handles the internal networking between Redis nodes, allowing them to discover and communicate with each other using service names.
  • Persistence: With Docker volumes, the cluster's configuration and data can be persisted, so you don't lose your cluster state if you stop and restart the containers.

By mastering Docker and Docker Compose, we lay a strong foundation for building, experimenting with, and sharing complex distributed systems setups, making the task of building a Redis Cluster significantly more manageable and enjoyable.

Designing the Redis Cluster Configuration for Docker Compose

Before writing any docker-compose.yml code, a thoughtful design phase is essential. We need to decide on the cluster topology, networking strategy, and persistence mechanisms.

1. Node Count and Topology: The Minimal Viable Cluster

A Redis Cluster requires at least three master nodes for proper functioning and majority consensus for failover. For a robust development or testing environment that simulates production, it's highly recommended to have at least one replica for each master. This configuration ensures that if any single master node fails, there's a replica ready to take over, maintaining high availability.

Therefore, our chosen topology will be: * 3 Master Nodes * 3 Replica Nodes (one replica for each master)

This gives us a total of 6 Redis instances. Each instance will run in its own Docker container.

2. Networking Considerations: Enabling Inter-Node Communication

Docker Compose automatically creates a default bridge network for all services defined in a docker-compose.yml file. Services on this network can reach each other by their service names. This is incredibly convenient for a Redis Cluster, as nodes need to communicate using their internal Docker network IPs.

However, Redis Cluster nodes also need to advertise an IP address that other nodes can use to connect to them via the cluster bus port (default client port + 10000). By default, Redis tries to auto-detect its IP, which might not always be the internal Docker IP if there are multiple network interfaces or complex networking. To ensure proper communication within the Docker network, we will explicitly configure each Redis node to bind to its service name (which resolves to its internal Docker IP) and to announce this IP to the other nodes for cluster communication.

We will use a custom bridge network for our Redis Cluster, giving it a descriptive name (e.g., redis-cluster-network). This practice offers better isolation and organization compared to relying solely on the default network.

3. Persistent Storage: Ensuring Data Durability

Redis is an in-memory database, but it supports various persistence options to save data to disk, preventing data loss upon restart. More importantly for a cluster, the cluster configuration itself (the nodes.conf file) needs to be persistent. Without it, the cluster state would be lost, and you'd have to re-create the cluster every time containers restart.

We will use Docker named volumes for persistence: * Each Redis instance will have its own dedicated named volume. This volume will be mounted into the container at the Redis data directory (e.g., /data). * This ensures that the nodes.conf file, dump.rdb (if RDB persistence is enabled), and appendonly.aof (if AOF persistence is enabled) are preserved across container lifecycles.

4. Configuration Files for Individual Redis Nodes: Customizing Behavior

While we could pass all Redis configurations as command-line arguments or environment variables to the redis-server command, it's generally cleaner and more manageable to use individual configuration files for each Redis node, especially for a cluster setup.

Each Redis container will mount a specific redis.conf file tailored for cluster mode: * cluster-enabled yes: Enables Redis Cluster mode. * cluster-config-file nodes.conf: Specifies the name of the cluster configuration file. This file is managed by Redis itself and should not be manually edited. * cluster-node-timeout 5000: Sets the node timeout in milliseconds. If a node is unreachable for longer than this timeout, it's considered to be in a PFAIL state. * bind 0.0.0.0: Binds Redis to all network interfaces within the container, allowing it to accept connections from other containers on the Docker network. * port 6379: Standard Redis client port. * protected-mode no: Disable protected mode to allow connections from any IP within the Docker network. Caution: For production, ensure your Docker network is secure. * appendonly yes: Enables AOF persistence for data durability. This is critical for preventing data loss. * dir /data: Specifies the directory where Redis persistence files and the nodes.conf file will be stored. This path will be mounted to our Docker volume. * cluster-announce-ip: This is a crucial setting for Docker Compose. We will dynamically set this via an environment variable in docker-compose.yml for each service, making Redis advertise its internal Docker service name as its IP. This is more reliable than letting Redis try to guess its IP in a multi-container Docker environment.

By carefully considering these design aspects, we can construct a robust and maintainable Redis Cluster environment using Docker Compose, forming the backbone for highly available and scalable applications.

Implementing the Docker Compose Configuration: A Detailed GitHub Example

Now, let's translate our design into a concrete docker-compose.yml file and a supporting setup script. This section will walk you through the creation of these files, explaining each part in detail.

Our example will create a 6-node Redis Cluster: 3 masters and 3 replicas. Each node will run in its own Docker container, persist its data, and communicate over a dedicated Docker network.

1. Project Structure

First, let's establish a clear directory structure for our project, mimicking a typical GitHub repository:

redis-cluster-docker/
├── docker-compose.yml
├── redis.conf
└── cluster-setup.sh

2. redis.conf - The Universal Redis Configuration

We'll use a single redis.conf file that will be shared by all Redis containers. The critical cluster-specific settings are included here.

# redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no
bind 0.0.0.0
dir /data
# Important: cluster-announce-ip will be set via environment variable in docker-compose.yml
# cluster-announce-port and cluster-announce-bus-port will be derived from port 6379

Explanation of redis.conf: * port 6379: The standard Redis port for client connections. * cluster-enabled yes: This directive is essential; it tells Redis to start in cluster mode. * cluster-config-file nodes.conf: Specifies the file where Redis will save its cluster configuration (node IDs, IPs, ports, slots, etc.). Redis automatically manages this file. * cluster-node-timeout 5000: Sets the maximum time in milliseconds a node can be unreachable before it's considered to be failed by other nodes. * appendonly yes: Enables AOF (Append Only File) persistence. This log every write operation, providing excellent data durability by replaying commands upon restart. While RDB (snapshotting) is also available, AOF typically offers stronger guarantees against data loss. * protected-mode no: Disables protected mode. In a Docker network, containers might connect from non-localhost IPs. Disabling this allows inter-container communication. Warning: In a production environment, ensure your Docker network is properly secured, or use other authentication mechanisms. * bind 0.0.0.0: Makes Redis listen on all available network interfaces within the container, allowing other Docker containers to connect to it. * dir /data: This is the directory inside the container where Redis will store its persistence files (AOF, RDB) and the nodes.conf file. This directory will be mapped to a Docker volume to ensure persistence. * cluster-announce-ip: This is commented out here because we will set it dynamically per service in docker-compose.yml using an environment variable. This is a critical point for Docker Compose as it ensures Redis advertises its correct internal Docker network IP (which is usually its service name for other containers).

3. docker-compose.yml - Defining Our Cluster Services

This is the heart of our setup. We'll define six services, corresponding to our six Redis nodes, along with shared volumes and a custom network.

# docker-compose.yml
version: '3.8'

services:
  redis-node-1:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-1
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-1:/data
    ports:
      - "6379:6379" # Exposing for external client access (optional, but useful for testing)
    networks:
      - redis-cluster-network

  redis-node-2:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-2
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-2:/data
    networks:
      - redis-cluster-network

  redis-node-3:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-3
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-3:/data
    networks:
      - redis-cluster-network

  redis-node-4:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-4
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-4:/data
    networks:
      - redis-cluster-network

  redis-node-5:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-5
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-5:/data
    networks:
      - redis-cluster-network

  redis-node-6:
    image: redis:7.2.4-alpine
    command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-6
    volumes:
      - ./redis.conf:/usr/local/etc/redis/redis.conf
      - redis-data-6:/data
    networks:
      - redis-cluster-network

volumes:
  redis-data-1:
  redis-data-2:
  redis-data-3:
  redis-data-4:
  redis-data-5:
  redis-data-6:

networks:
  redis-cluster-network:
    driver: bridge

Explanation of docker-compose.yml:

  • version: '3.8': Specifies the Docker Compose file format version. Using a recent version like 3.8 ensures access to the latest features and best practices.
  • services:: This top-level key defines all the containers that make up our application.
    • redis-node-1 to redis-node-6: Each of these defines a single Redis instance (container).
      • image: redis:7.2.4-alpine: We use the official Redis Docker image, specifically version 7.2.4, and the alpine variant for a smaller image size. Using a specific version is important for reproducibility.
      • command: redis-server /usr/local/etc/redis/redis.conf --cluster-announce-ip redis-node-X: This is the command executed when the container starts.
        • redis-server /usr/local/etc/redis/redis.conf: Starts the Redis server using our custom redis.conf file, which is mounted into the container.
        • --cluster-announce-ip redis-node-X: This is critical for Docker Compose. This flag tells Redis to advertise its own service name (e.g., redis-node-1) as its IP address to other cluster nodes. Within the redis-cluster-network, redis-node-1 resolves to the container's IP address, allowing other nodes to connect. Without this, Redis might try to announce a different IP that's not reachable by other containers.
      • volumes::
        • - ./redis.conf:/usr/local/etc/redis/redis.conf: Mounts our local redis.conf file into each container at the expected location for Redis configuration. This ensures all nodes use the same base configuration.
        • - redis-data-X:/data: Mounts a named Docker volume (redis-data-1, redis-data-2, etc.) to the /data directory inside each container. This makes the nodes.conf file and any persistence data (AOF/RDB) durable, surviving container restarts and removals.
      • ports: (Only for redis-node-1 in this example):
        • - "6379:6379": This line publishes the container's port 6379 to the host machine's port 6379. This is primarily for external client access (e.g., using redis-cli from your host machine to connect to redis-node-1). We only expose one port here for simplicity in a development setup; for production or more advanced testing, you might expose other nodes on different host ports (e.g., 6380, 6381) or use a jump host/VPN to access internal ports.
      • networks::
        • - redis-cluster-network: Assigns each service to our custom redis-cluster-network. This allows them to communicate with each other using their service names.
  • volumes:: This section defines the named volumes used by our services. Docker automatically creates these volumes if they don't exist. Each Redis node gets its own dedicated volume.
  • networks:: This section defines our custom network.
    • redis-cluster-network:: The name of our network.
    • driver: bridge: Specifies a standard bridge network, which is suitable for multi-container applications on a single host.

4. cluster-setup.sh - Initializing the Redis Cluster

After all Redis nodes are up and running, they are still independent instances. They need to be told to form a cluster. We use redis-cli --cluster create for this. This script also includes a sleep command to give the Redis containers enough time to fully initialize before attempting to create the cluster.

#!/bin/bash
# cluster-setup.sh

echo "Waiting for Redis nodes to start..."
sleep 20 # Give containers time to fully initialize

echo "Creating Redis Cluster..."
# The --cluster-replicas 1 means each master will have 1 replica.
# We list all 6 nodes that we want to be part of the cluster.
# redis-cli will automatically assign masters and replicas.
redis-cli --cluster create \
  redis-node-1:6379 \
  redis-node-2:6379 \
  redis-node-3:6379 \
  redis-node-4:6379 \
  redis-node-5:6379 \
  redis-node-6:6379 \
  --cluster-replicas 1 \
  --cluster-yes # Automatically accept the proposed configuration

echo "Redis Cluster creation initiated. Check status after a moment."
echo "You can check the cluster status with: redis-cli -c -p 6379 cluster info"
echo "Or: redis-cli -c -p 6379 cluster nodes"

Explanation of cluster-setup.sh:

  • #!/bin/bash: Shebang line, indicating the script should be run with bash.
  • sleep 20: This is a crucial step. Starting Docker containers doesn't mean the Redis server inside is immediately ready to accept connections or form a cluster. A delay ensures that all Redis processes are fully booted up and listening. The exact duration might vary slightly based on system performance.
  • redis-cli --cluster create ...: This is the command that orchestrates the cluster creation.
    • redis-cli: The Redis command-line interface.
    • --cluster create: The subcommand to create a new cluster.
    • redis-node-1:6379 ... redis-node-6:6379: A list of all the Redis instances that should be part of the cluster. Docker Compose's internal DNS allows us to use service names directly.
    • --cluster-replicas 1: This tells redis-cli to assign one replica to each master node it creates. Since we provided 6 nodes, it will designate 3 as masters and 3 as their respective replicas.
    • --cluster-yes: Automatically confirms the cluster configuration proposed by redis-cli, bypassing the interactive prompt. This is useful for scripting.

With these three files in place, you have a complete and well-defined setup for a Redis Cluster using Docker Compose, ready for deployment and testing. This setup can be easily shared via a GitHub repository, enabling rapid environment setup for any team member.

APIPark is a high-performance AI gateway that allows you to securely access the most comprehensive LLM APIs globally on the APIPark platform, including OpenAI, Anthropic, Mistral, Llama2, Google Gemini, and more.Try APIPark now! 👇👇👇

Hands-on Cluster Deployment and Verification

With the docker-compose.yml, redis.conf, and cluster-setup.sh files prepared, we are now ready to bring our Redis Cluster to life. This section details the steps to deploy the cluster, verify its health, and perform basic interactions.

1. Spinning Up the Cluster

Navigate to the redis-cluster-docker directory in your terminal where all the files are located.

First, start all the Redis containers in detached mode:

docker-compose up -d

Expected Output: You'll see Docker pulling the redis:7.2.4-alpine image (if not already present) and then creating and starting the six Redis services, along with the custom network and named volumes.

Creating network "redis-cluster-docker_redis-cluster-network" with driver "bridge"
Creating volume "redis-cluster-docker_redis-data-1" with default driver
...
Creating redis-node-1 ... done
Creating redis-node-2 ... done
...

Now, execute the cluster-setup.sh script to form the cluster:

bash cluster-setup.sh

Expected Output (after the sleep 20): The redis-cli --cluster create command will run. It will output information about the proposed cluster layout (which nodes become masters, which become replicas, and which hash slots are assigned to each master). You'll see lines like:

>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica redis-node-4:6379 to redis-node-1:6379
Adding replica redis-node-5:6379 to redis-node-2:6379
Adding replica redis-node-6:6379 to redis-node-3:6379
M: d7f... redis-node-1:6379
   slots:[0-5460] (5461 slots) master
M: 8a5... redis-node-2:6379
   slots:[5461-10922] (5462 slots) master
M: 5f3... redis-node-3:6379
   slots:[10923-16383] (5461 slots) master
S: 0b1... redis-node-4:6379
   replicates d7f...
S: 7c4... redis-node-5:6379
   replicates 8a5...
S: b2e... redis-node-6:6379
   replicates 5f3...
[OK] All 16384 slots covered.

This output confirms that the cluster has been successfully formed, with hash slots distributed among three masters, and each master having one replica.

2. Verifying Cluster Health

You can verify the cluster's health and configuration using redis-cli. Since we exposed port 6379 of redis-node-1 to the host, we can connect to it directly. The -c flag is crucial; it enables cluster mode for redis-cli, allowing it to handle redirections automatically.

Check overall cluster information:

redis-cli -c -p 6379 cluster info

Expected Output:

cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:867
cluster_stats_messages_pong_sent:866
cluster_stats_messages_sent:1733
cluster_stats_messages_ping_received:866
cluster_stats_messages_pong_received:867
cluster_stats_messages_received:1733

The key piece of information here is cluster_state:ok, indicating that the cluster is healthy and fully operational. cluster_known_nodes:6 confirms all nodes are registered, and cluster_size:3 indicates three master nodes.

Inspect individual node configurations and relationships:

redis-cli -c -p 6379 cluster nodes

Expected Output (will vary slightly for node IDs):

d7f... redis-node-1:6379@16379 master - 0 1687893922000 1 connected 0-5460
8a5... redis-node-2:6379@16379 master - 0 1687893922123 2 connected 5461-10922
5f3... redis-node-3:6379@16379 master - 0 1687893922300 3 connected 10923-16383
0b1... redis-node-4:6379@16379 slave d7f... 0 1687893922000 1 connected
7c4... redis-node-5:6379@16379 slave 8a5... 0 1687893922123 2 connected
b2e... redis-node-6:6379@16379 slave 5f3... 0 1687893922300 3 connected

This output provides a detailed view of each node: its ID, announced IP:port (which is redis-node-X:6379 in our case, accessible via Docker's internal DNS), role (master/slave), its master's ID (for slaves), connection status, and the hash slots it's responsible for (for masters).

3. Connecting Clients and Testing Data Operations

Now, let's perform some basic read and write operations to demonstrate how the cluster handles data distribution.

Connect to the cluster via any exposed port (we'll use redis-node-1:6379). Remember the -c flag for cluster mode.

redis-cli -c -p 6379

Once inside the redis-cli prompt:

Set a key:

127.0.0.1:6379> SET mykey "Hello Redis Cluster"
-> Redirected to host redis-node-1:6379
OK

Notice the -> Redirected to host redis-node-1:6379. redis-cli automatically redirected the command to the correct node responsible for mykey's hash slot. In this example, mykey's slot happened to be on redis-node-1.

Set another key, which might go to a different node:

127.0.0.1:6379> SET anotherkey "Distributed Data"
-> Redirected to host redis-node-2:6379
OK

Here, anotherkey's hash slot was assigned to redis-node-2, and redis-cli transparently redirected the command.

Get the keys:

127.0.0.1:6379> GET mykey
"Hello Redis Cluster"
127.0.0.1:6379> GET anotherkey
-> Redirected to host redis-node-2:6379
"Distributed Data"

Again, redirections occur transparently as redis-cli finds the correct node holding the data.

Try to set a key with a hash tag: Hash tags (curly braces {}) allow you to force multiple keys to reside in the same hash slot, even if their full names would hash to different slots. This is useful for multi-key operations that require all keys to be on the same node.

127.0.0.1:6379> SET user:{123}:name "Alice"
-> Redirected to host redis-node-3:6379
OK
127.0.0.1:6379> SET user:{123}:email "alice@example.com"
OK

In this example, both user:{123}:name and user:{123}:email keys were directed to redis-node-3 because the hash slot calculation is based only on the string inside the curly braces (123). This is essential for operations like MGET or transactions involving multiple related keys.

Exit redis-cli by typing exit.

4. Testing Failover Scenarios

To demonstrate the high availability of the cluster, let's simulate a master node failure.

First, identify one of the master nodes and its corresponding replica from the cluster nodes output. For instance, redis-node-1 is a master, and redis-node-4 is its replica.

Now, stop the master node container:

docker-compose stop redis-node-1

Wait a few moments (the cluster-node-timeout of 5 seconds will come into play). Then, check the cluster status again:

redis-cli -c -p 6379 cluster info
redis-cli -c -p 6379 cluster nodes

Expected Changes: * cluster info might momentarily show cluster_state:fail but should quickly recover to ok as the failover completes. * cluster nodes will show redis-node-1 as fail or handshake. Crucially, redis-node-4 (the former replica) should now be listed as master and responsible for redis-node-1's original hash slots (0-5460).

Example cluster nodes output after redis-node-1 fails and redis-node-4 promotes:

# ... other nodes ...
8a5... redis-node-2:6379@16379 master - 0 1687893922123 2 connected 5461-10922
5f3... redis-node-3:6379@16379 master - 0 1687893922300 3 connected 10923-16383
0b1... redis-node-4:6379@16379 master - 0 1687894000000 7 connected 0-5460  <-- redis-node-4 is now a master
7c4... redis-node-5:6379@16379 slave 8a5... 0 1687893922123 2 connected
b2e... redis-node-6:6379@16379 slave 5f3... 0 1687893922300 3 connected
d7f... redis-node-1:6379@16379 master,fail - 1687893922000 1687894000000 1 disconnected  <-- redis-node-1 is failed

This demonstrates the automatic failover capability, a cornerstone of Redis Cluster's high availability. Once you restart redis-node-1, it will rejoin the cluster as a replica of the newly promoted master redis-node-4.

docker-compose start redis-node-1

After a short while, check cluster nodes again; redis-node-1 should now be a replica of redis-node-4.

This hands-on experience solidifies the understanding of how Redis Cluster functions in a distributed environment and how Docker Compose facilitates its setup and management.

5. Cleaning Up

When you are done experimenting, you can stop and remove all containers, networks, and volumes:

docker-compose down -v

The -v flag is important as it also removes the named volumes, ensuring a clean slate for the next deployment. If you omit -v, the data and cluster configuration (nodes.conf) will persist, which can be useful if you only want to stop and restart the cluster without re-creating it from scratch.

This detailed walk-through of deployment and verification should provide a robust understanding of operating a Redis Cluster with Docker Compose, making it a truly valuable GitHub example.

Advanced Topics and Best Practices for Redis Cluster

Building a basic Redis Cluster with Docker Compose is a great start, but real-world scenarios often demand more. Let's delve into some advanced topics and best practices that will enhance the robustness, security, and performance of your Redis Cluster.

Monitoring Redis Clusters

Monitoring is non-negotiable for any production system, and Redis Cluster is no exception. Key metrics to track include:

  1. Node Status: Are all nodes (masters and replicas) healthy and connected? Is any node in a PFAIL or FAIL state?
  2. Memory Usage: Redis is an in-memory store. High memory usage can lead to swapping (which severely degrades performance) or out-of-memory errors. Monitor used_memory_rss, used_memory_peak, and mem_fragmentation_ratio.
  3. CPU Usage: High CPU usage can indicate bottlenecks or inefficient queries.
  4. Client Connections: Track connected_clients to ensure you're not hitting connection limits and to understand client behavior.
  5. Latency: Monitor latency_samples_duration_us to detect slowdowns in command processing.
  6. Persistence Status: Verify that AOF rewrites and RDB snapshots are completing successfully and within acceptable timeframes.
  7. Replication Lag: For replicas, monitor master_repl_offset and slave_repl_offset to detect if replicas are falling behind their masters.

Tools for Monitoring: * Redis-cli INFO and CLUSTER INFO: The basic commands provide a wealth of information directly from Redis. * Prometheus and Grafana: A popular combination for metrics collection and visualization. Redis Exporter can expose Redis metrics to Prometheus. * Cloud Provider Monitoring: If running on managed Redis services (e.g., AWS ElastiCache, Azure Cache for Redis), they provide built-in monitoring dashboards. * APM Tools: Application Performance Monitoring tools can integrate with Redis clients to provide insights into application-level Redis interactions.

Security Considerations

While our Docker Compose setup is suitable for development, securing a Redis Cluster in a production environment requires careful attention:

  1. Network Isolation: Never expose all Redis cluster ports directly to the public internet. Use firewalls, VPCs, and private networks to restrict access only to authorized application servers. Within Docker, custom bridge networks provide good isolation for inter-container communication, but external access points need protection.
  2. Authentication: Use Redis's requirepass directive to set a password for client authentication. All redis-cli and client library connections will then need to provide this password. For cluster mode, the password should be consistent across all nodes.
  3. Encryption (TLS/SSL): For sensitive data, encrypt client-server communication using TLS/SSL. Redis 6.0 and later versions support native TLS.
  4. Rename/Disable Dangerous Commands: Commands like FLUSHALL, KEYS, CONFIG can be dangerous in production. Consider renaming or disabling them in your redis.conf file using rename-command or rename-command <original_command> "".
  5. Regular Backups: Implement a robust backup strategy for your Redis persistence files (AOF/RDB). While Redis Cluster offers high availability, it doesn't replace the need for off-site backups in case of catastrophic data corruption or cluster-wide failure.

Performance Tuning

Optimizing Redis Cluster performance involves several layers:

  1. Hardware: Redis is CPU-bound for some operations and memory-bound for others. Ensure adequate CPU cores, sufficient RAM, and fast storage (SSDs) for persistence.
  2. Network: High-speed, low-latency network interfaces are crucial for inter-node communication and client-server interactions.
  3. redis.conf Optimizations:
    • Persistence: AOF fsync strategy (e.g., appendfsync everysec for a good balance of durability and performance).
    • Memory Management: maxmemory and maxmemory-policy are critical for caching use cases.
    • Lazy Freeing: lazyfree-lazy-eviction yes, lazyfree-lazy-expire yes, lazyfree-lazy-server-del yes can improve responsiveness by performing memory freeing in the background.
  4. Client-Side Optimizations:
    • Pipelining: Group multiple commands into a single request to reduce network round trips.
    • Connection Pooling: Reuse connections to avoid the overhead of establishing new TCP connections for every command.
    • Bulk Operations: Use commands like MSET, MGET, HMSET when operating on multiple keys or fields.
  5. Data Modeling: Efficient data structures and key naming conventions can significantly impact performance. Avoid very large hash keys or lists that require significant memory transfers.
  6. Read Replicas: While Redis Cluster's primary replicas are for failover, they can be used to offload some read traffic, especially for operations that don't require the absolute latest data. However, be mindful of potential replication lag.

Integrating with Application Backends (Client Libraries)

Most programming languages have robust Redis client libraries that natively support Redis Cluster. These libraries are "cluster-aware," meaning they: * Can connect to any node in the cluster and automatically discover the cluster topology. * Handle MOVED and ASK redirections transparently, ensuring commands are sent to the correct hash slot owner. * Implement intelligent connection management, often maintaining a pool of connections to various cluster nodes.

Example (Conceptual Python with redis-py-cluster):

from redis.cluster import RedisCluster

# Define the startup nodes (any subset of your cluster nodes)
startup_nodes = [{"host": "your_redis_node_ip", "port": "6379"}] # For Docker, this would be redis-node-1:6379 etc.

# Create a RedisCluster client instance
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)

# Perform operations
rc.set("my_app_key", "some_value")
value = rc.get("my_app_key")
print(f"Retrieved: {value}")

It's crucial to use cluster-aware clients to leverage the full power and transparency of Redis Cluster.

Beyond Development: Production Considerations

While Docker Compose is an excellent tool for local development, testing, and CI/CD pipelines, deploying a Redis Cluster to production often warrants a more robust orchestration platform like Kubernetes.

  • Docker Compose for Development vs. Kubernetes for Production:
    • Docker Compose: Ideal for single-host, multi-container applications. Simple to set up and manage locally. Lacks built-in features for self-healing, scaling, and rolling updates across multiple machines.
    • Kubernetes: A powerful container orchestration system designed for deploying, scaling, and managing containerized applications across a cluster of machines. Provides native support for stateful applications (like Redis Cluster) via StatefulSets, automatic load balancing, service discovery, persistent storage management, and advanced deployment strategies.
  • Cloud Provider Managed Redis Services: For many organizations, especially those without deep expertise in operating distributed databases, leveraging managed Redis services from cloud providers is often the most cost-effective and reliable option.
    • AWS ElastiCache for Redis: Offers fully managed, highly available, and scalable Redis clusters. Handles patching, backups, and monitoring.
    • Azure Cache for Redis: Similar to AWS, providing a managed Redis service with enterprise-grade features.
    • Google Cloud Memorystore for Redis: Google's managed Redis offering.

These managed services abstract away the operational complexities of running a Redis Cluster, allowing development teams to focus on application logic rather than infrastructure management.

The Broader Ecosystem: APIs, Gateways, and Open Platforms

A high-performance, scalable data store like a Redis Cluster is not an isolated component; it's a foundational element within a larger application ecosystem. Modern software architectures, especially those built on microservices, heavily rely on APIs for communication and gateways for managing external access. When designing an Open Platform, the efficiency and reliability of every underlying component, including the data layer, directly impact the platform's overall success and user experience.

How Redis Clusters Power High-Performance APIs

Many high-traffic APIs demand incredibly fast response times. Redis Cluster plays a pivotal role here:

  • Caching Layer: The most common use case. APIs often fetch data from slower, more expensive data stores (like relational databases). Redis can cache the results of frequently requested API calls or database queries, serving them directly from memory, dramatically reducing latency and database load. This allows APIs to handle a much higher volume of requests.
  • Session Management: For authenticated APIs, user session data (tokens, user preferences) can be stored in Redis. This provides a fast, centralized, and scalable store for session information across multiple API instances.
  • Rate Limiting: Redis's atomic operations and TTL (Time To Live) features make it perfect for implementing robust rate-limiting mechanisms for APIs. This protects your services from abuse and ensures fair usage.
  • Leaderboards and Real-time Analytics: APIs that expose real-time data, such as gaming leaderboards, trending topics, or live dashboards, can leverage Redis's sorted sets and other data structures for efficient updates and queries.
  • Message Queues: For asynchronous API operations (e.g., submitting a long-running job), Redis can act as a lightweight message broker, allowing the API to quickly acknowledge the request and offload the actual processing to background workers.

By offloading expensive operations and providing ultra-low-latency data access, a Redis Cluster enables APIs to deliver exceptional performance and scalability, handling millions of requests per second.

The Role of an API Gateway

An API Gateway acts as the single entry point for all API calls from clients to your backend services. It handles a multitude of cross-cutting concerns that would otherwise clutter your individual microservices.

  • Caching at the Gateway Level: An API Gateway can implement its own caching layer, further reducing the load on backend services, including Redis. If a response can be served directly from the gateway's cache, the request might not even reach your Redis Cluster.
  • Rate Limiting and Throttling: While Redis can be used by individual services for rate limiting, an API Gateway provides a centralized point to enforce global rate limits across all APIs, protecting the entire backend from overwhelming traffic.
  • Authentication and Authorization: The gateway can authenticate incoming requests and authorize them before forwarding to the appropriate backend service.
  • Traffic Management: Load balancing, routing requests to different versions of services (e.g., A/B testing, canary deployments), and circuit breaking are all functions an API Gateway can perform.
  • API Composition: For complex requests, the gateway can aggregate calls to multiple backend services and compose a single response.

In a system leveraging a Redis Cluster, the API Gateway often sits in front of the services that interact with Redis. It ensures that only valid, authorized, and non-rate-limited requests reach the backend, effectively shielding and optimizing the entire application stack.

Redis as a Pillar of an Open Platform

An Open Platform is an architecture designed for extensibility, interoperability, and often allows third-party developers to build applications and integrations on top of it. Such platforms are inherently complex, involving numerous services, vast amounts of data, and diverse integration points.

Redis Cluster contributes significantly to the foundations of an Open Platform:

  • Scalable Data Backbone: An Open Platform must support an ever-growing user base and data volume. A sharded and highly available Redis Cluster provides the necessary scalable data backbone for caching, real-time data, and session management across all platform services.
  • Shared State for Microservices: In an Open Platform built with microservices, Redis can act as a shared, fast, and consistent state store, allowing different services to coordinate or share transient data efficiently.
  • Event-Driven Architectures: Redis's Pub/Sub capabilities can be used for building event-driven systems, enabling loose coupling between services—a hallmark of resilient Open Platforms.
  • Foundation for AI Services: With the rise of AI integration, an Open Platform often needs to manage interactions with various AI models. High-speed caches like Redis can store model outputs, feature vectors, or intermediate AI processing results, accelerating AI-powered features within the platform.

When building a robust Open Platform that exposes various services via APIs, the efficient management and security of these APIs become paramount. Platforms like APIPark, an open-source AI gateway and API management platform, offer comprehensive solutions for integrating, managing, and securing your APIs, including those backed by high-performance data stores like Redis clusters. APIPark helps developers and enterprises manage, integrate, and deploy AI and REST services with ease, ensuring that the performance and reliability delivered by a Redis Cluster are effectively channeled and protected at the gateway level. By providing features such as quick integration of 100+ AI models, unified API format for AI invocation, prompt encapsulation into REST API, and end-to-end API lifecycle management, APIPark ensures that your scalable backend, powered by Redis Cluster, is accessible and manageable in a secure and efficient manner across your Open Platform. It facilitates API service sharing within teams, ensures independent API and access permissions for each tenant, and offers powerful data analysis and detailed API call logging, all contributing to a well-governed and high-performing platform. The combination of a robust Redis Cluster and an intelligent API Gateway like APIPark creates a formidable architecture capable of handling the demands of any sophisticated Open Platform.

In conclusion, a well-architected Redis Cluster is more than just a cache; it's an enabler for high-performance APIs, a critical component within the ecosystem managed by an API Gateway, and a fundamental building block for any truly scalable and resilient Open Platform. Understanding its deployment and operational nuances, as demonstrated in this GitHub example with Docker Compose, empowers developers to build the next generation of fast, reliable, and distributed applications.

Conclusion

The journey of constructing a Redis Cluster with Docker Compose, as meticulously detailed in this GitHub example, provides an invaluable blueprint for anyone seeking to master distributed system deployments in a contained environment. We began by understanding the foundational limitations of a single Redis instance and recognized the compelling advantages of Redis Cluster—its inherent scalability, high availability, and automatic sharding capabilities. Through a deep dive into its architecture, including the intricate interplay of master and replica nodes, hash slots, and the gossip protocol, we established a solid theoretical understanding.

Leveraging Docker Compose, we moved from theory to practical implementation, crafting a robust docker-compose.yml file and an accompanying cluster-setup.sh script. This hands-on approach allowed us to define six Redis instances, each running within its isolated container, communicating over a dedicated network, and persisting its data through Docker volumes. The explicit configuration of cluster-announce-ip for each service proved to be a pivotal detail in enabling seamless inter-node communication within the Docker network. Our subsequent deployment and verification steps demonstrated the cluster's health, its ability to distribute data efficiently, and most importantly, its automatic failover mechanism—a testament to its resilience.

Furthermore, we explored advanced topics, from comprehensive monitoring and stringent security practices to performance tuning and client-side integration, preparing you for the complexities of real-world application. We also briefly touched upon the transition from development-focused Docker Compose to production-grade orchestration with Kubernetes or leveraging fully managed cloud services.

Finally, we contextualized the Redis Cluster within the broader landscape of modern application development, highlighting its indispensable role in powering high-performance APIs, serving as a critical component managed by an API Gateway, and fundamentally underpinning the infrastructure of any successful Open Platform. The natural integration point for products like APIPark, an open-source AI gateway and API management platform, underscored the symbiotic relationship between a robust data layer and sophisticated API management solutions in creating a truly efficient and secure digital ecosystem.

By following this comprehensive guide, you are now equipped with not just a working Redis Cluster setup, but a profound understanding of its mechanics, deployment intricacies, and its strategic importance in building the scalable, resilient, and performant applications that characterize the modern technological landscape. This GitHub example serves as a powerful starting point for your own explorations into distributed Redis, empowering you to confidently build and manage complex data architectures.


Frequently Asked Questions (FAQs)

1. Why use Docker Compose for a Redis Cluster instead of just multiple docker run commands? Docker Compose simplifies the definition, networking, and volume management for multi-container applications. Instead of manually linking containers, creating networks, and mounting volumes with multiple lengthy docker run commands, docker-compose.yml allows you to define the entire multi-service application stack in a single, readable YAML file. With one command (docker-compose up -d), you can spin up all interdependent services, networks, and volumes, ensuring consistency and reproducibility across environments. This is especially beneficial for complex setups like a Redis Cluster with multiple nodes.

2. Is this Docker Compose setup suitable for a production Redis Cluster? While this Docker Compose setup provides a robust and fully functional Redis Cluster for development, testing, and demonstration, it is generally not recommended for production. Production environments demand higher levels of availability, automated scaling, self-healing capabilities, and integrated monitoring/logging across multiple hosts, which are beyond the scope of Docker Compose. For production, container orchestration platforms like Kubernetes (using StatefulSets) or cloud provider-managed Redis services (e.g., AWS ElastiCache, Azure Cache for Redis) are the preferred choices, as they offer enterprise-grade features and operational efficiencies.

3. What happens to the data if I stop and remove the containers without the -v flag? If you run docker-compose down without the -v flag, Docker will stop and remove the containers and networks, but it will not remove the named volumes (e.g., redis-data-1). This means that your Redis data and the cluster configuration file (nodes.conf) will persist on your host machine. If you then run docker-compose up -d again, new containers will be created and re-attach to the existing volumes, effectively resuming your cluster with its previous state and data. This can be useful for quick restarts without re-initializing the cluster.

4. How can I scale this Redis Cluster by adding more nodes? Scaling a Redis Cluster involves more than just adding more Docker Compose services. While you would add new service definitions to your docker-compose.yml and corresponding volumes, you would then need to use redis-cli --cluster add-node to explicitly add the new nodes to the existing cluster. For horizontal scaling of data capacity, you would add new master nodes and then use redis-cli --cluster reshard to migrate hash slots from existing masters to the new ones. For read scaling or enhanced high availability, you would add new replica nodes to existing masters using redis-cli --cluster add-node <new_node_ip>:<new_node_port> --cluster-slave --cluster-master-id <master_node_id>. This process is more involved and typically requires careful planning.

5. Why is cluster-announce-ip critical in this Docker Compose setup? In a Docker Compose environment, containers often communicate using internal DNS names (their service names) within the Docker network. By default, a Redis server might try to guess its public IP address or bind to 127.0.0.1 if not explicitly configured. If Redis announces an IP address that other containers cannot reach (e.g., 127.0.0.1 or an ephemeral internal IP that changes), the cluster formation will fail, or nodes will be unable to communicate. By setting cluster-announce-ip to the Docker Compose service name (e.g., redis-node-1), we ensure that Redis advertises an IP address that is consistently resolvable and reachable by other nodes within the redis-cluster-network, allowing the cluster to form and operate correctly.

🚀You can securely and efficiently call the OpenAI API on APIPark in just two steps:

Step 1: Deploy the APIPark AI gateway in 5 minutes.

APIPark is developed based on Golang, offering strong product performance and low development and maintenance costs. You can deploy APIPark with a single command line.

curl -sSO https://download.apipark.com/install/quick-start.sh; bash quick-start.sh
APIPark Command Installation Process

In my experience, you can see the successful deployment interface within 5 to 10 minutes. Then, you can log in to APIPark using your account.

APIPark System Interface 01

Step 2: Call the OpenAI API.

APIPark System Interface 02
Article Summary Image