blog

Setting Up a Redis Cluster with Docker-Compose: A Step-by-Step Guide on GitHub

Redis is an in-memory data structure store widely used as a database, cache, and message broker. Setting up a Redis cluster can greatly enhance the scalability and performance of your applications. In this comprehensive guide, we will explore how to set up a Redis cluster using Docker-Compose while integrating an AI Gateway for efficient traffic management.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Understanding Redis Cluster
  4. Setting Up Docker-Compose
  5. Configuring Redis Nodes
  6. Running the Redis Cluster
  7. Testing the Setup
  8. Integrating the AI Gateway
  9. Traffic Control with the AI Gateway
  10. Conclusion
  11. Further Readings

1. Introduction

In modern software architecture, the need for distributed systems has increased significantly. Redis, particularly in its clustered form, provides an ideal solution for horizontally scaling databases. Docker-Compose simplifies the orchestration of multi-container Docker applications, making it an ideal tool for setting up a Redis cluster. Furthermore, incorporating an AI Gateway, such as the Espressive Barista LLM Gateway, can add layers of intelligence and traffic control to your architecture.

2. Prerequisites

Before diving into the setup process, ensure you have the following:

  • A system with Docker and Docker-Compose installed.
  • Basic knowledge of Redis and Docker concepts.
  • Access to the command line interface (CLI) of your operating system.

Installation Instructions

To install Docker and Docker-Compose, refer to the official documentation.

3. Understanding Redis Cluster

A Redis cluster is a distributed implementation of Redis that allows multiple Redis nodes to work together.

Key Features of Redis Cluster

  • Scalability: As your application grows, you can add more nodes without significant downtime.
  • Fault Tolerance: Redis Cluster can tolerate some node failures and continue to operate.
  • Data Sharding: Data is automatically distributed across multiple nodes.

You can find detailed information about Redis Clusters in the official Redis Cluster documentation.

4. Setting Up Docker-Compose

Docker-Compose uses a docker-compose.yml file to define and manage multi-container applications. Here’s how you can set it up for our Redis cluster:

Step 1: Create a Directory for Your Project

mkdir redis-cluster
cd redis-cluster

Step 2: Create a Docker-Compose File

Create a file named docker-compose.yml and add the following configuration:

version: '3.7'

services:
  redis-node-1:
    image: redis:6.0
    volumes:
      - ./data/node1:/data
    ports:
      - "7001:6379"
    networks:
      - redis-cluster
    command: ["redis-server", "--cluster-enabled", "yes", "--cluster-config-file", "/data/node1.conf", "--cluster-node-timeout", "5000"]

  redis-node-2:
    image: redis:6.0
    volumes:
      - ./data/node2:/data
    ports:
      - "7002:6379"
    networks:
      - redis-cluster
    command: ["redis-server", "--cluster-enabled", "yes", "--cluster-config-file", "/data/node2.conf", "--cluster-node-timeout", "5000"]

  redis-node-3:
    image: redis:6.0
    volumes:
      - ./data/node3:/data
    ports:
      - "7003:6379"
    networks:
      - redis-cluster
    command: ["redis-server", "--cluster-enabled", "yes", "--cluster-config-file", "/data/node3.conf", "--cluster-node-timeout", "5000"]

networks:
  redis-cluster:
    driver: bridge

Here, we have defined three Redis nodes, each running in a separate container.

5. Configuring Redis Nodes

After setting up the Docker-Compose configuration, we need to configure the Redis nodes to form a cluster.

Step 1: Start the Docker-Compose Application

Run the following command from your project directory:

docker-compose up -d

Step 2: Create the Redis Cluster

Once all nodes are up, execute the following command to create the cluster:

docker exec -it <redis-node-1-container-id> redis-cli --cluster create 172.18.0.2:6379 172.18.0.3:6379 172.18.0.4:6379 --cluster-replicas 1

Make sure to replace <redis-node-1-container-id> with the actual container ID of redis-node-1. This command sets up a cluster with specified replicas.

6. Running the Redis Cluster

At this point, the Redis Cluster should be operational. You can check the status of the cluster using the following command:

docker exec -it <redis-node-1-container-id> redis-cli cluster info

The output will confirm that the cluster is up and running.

7. Testing the Setup

To test if the cluster is functioning correctly, you can use the Redis CLI to set and get values.

docker exec -it <redis-node-1-container-id> redis-cli set key "Hello Redis Cluster"
docker exec -it <redis-node-1-container-id> redis-cli get key

Ensure that you can retrieve the value “Hello Redis Cluster” without issues.

8. Integrating the AI Gateway

To enhance performance and traffic handling, we will integrate the AI Gateway, specifically the Espressive Barista LLM Gateway into our architecture.

Step 1: Create a Gateway Service

Update your docker-compose.yml file by adding the AI Gateway service configuration.

ai-gateway:
  image: espressive/barista-gateway
  ports:
    - "8080:8080"
  networks:
    - redis-cluster

Step 2: Building the Complete Setup

After adding the AI Gateway, your Docker Compose file should look similar to this:

version: '3.7'

services:
  redis-node-1:
    # previous configurations

  redis-node-2:
    # previous configurations

  redis-node-3:
    # previous configurations

  ai-gateway:
    image: espressive/barista-gateway
    ports:
      - "8080:8080"
    networks:
      - redis-cluster

networks:
  redis-cluster:
    driver: bridge

Step 3: Rebuild and Start the Services

Run the command:

docker-compose up -d --build

9. Traffic Control with the AI Gateway

The AI Gateway allows intelligent routing and traffic management. You can implement traffic control policies to optimize request handling towards the Redis cluster.

Here’s an example JSON configuration mapping requests to specific endpoints:

{
  "routes": [
    {
      "path": "/redis",
      "service": "redis-node-1",
      "method": "GET",
      "filters": {
        "ratelimiter": {
          "max_requests": 100,
          "time_frame": "1m"
        }
      }
    }
  ]
}

This configuration guards the Redis endpoint with a rate-limiting filter.

10. Conclusion

Setting up a Redis cluster with Docker-Compose allows you to leverage the power of distributed databases efficiently. Moreover, integrating an AI Gateway can open new avenues for traffic control and intelligent request handling.

By following the steps outlined in this guide, you should now have a fully operational Redis cluster that is resilient, scalable, and integrated with advanced traffic management capabilities through an AI Gateway.

11. Further Readings

Additional Resource Table

Resource Description
Redis Cluster Tutorial Comprehensive guide on Redis clustering.
Docker-Compose Installation Guide Steps for Docker-Compose setup.
Espressive Barista LLM Gateway Documentation for the AI Gateway.

With this setup, you can effectively manage data and traffic through your Redis cluster integrated with intelligent processing capabilities via the AI Gateway.

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! 👇👇👇

Remember to deploy this architecture in accordance with your application needs and adjust configurations for optimal performance. Happy clustering!

🚀You can securely and efficiently call the 文心一言 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 文心一言 API.

APIPark System Interface 02