Back to articles
DevOps & Systems

Configure Caddy Server as a Distributed API Gateway with Redis-Synced Rate Limiting

Detailed guide on configuring Caddy Server as a distributed API Gateway with Redis-synced rate limiting, suitable for microservices and modern web applications.

DPTCloud Engineering4 min read
Configure Caddy Server as a Distributed API Gateway with Redis-Synced Rate Limiting

Configure Caddy Server as a Distributed API Gateway

Caddy Server is a modern web server with automatic HTTPS and easy configuration. In distributed environments, Caddy can function as an API Gateway to route traffic, manage load, and protect backend services. When integrated with Redis, Caddy can perform rate limiting in a synchronized manner, helping prevent denial-of-service attacks and maintaining stable performance across microservices and modern web applications.

Prerequisites

  • One or more servers running Caddy (Ubuntu/Debian)
  • Redis server installed and running (can be on the same machine or a separate host)
  • Access to backend services (APIs, microservices)
  • Port 80 and 443 open (required for HTTPS)

Configure Caddy with Redis for Rate Limiting

Use the redis plugin for Caddy to enable synchronized rate limiting. First, install Caddy with the Redis plugin:

# Install Caddy with Redis plugin
curl -sSL https://getcaddy.com | bash -s personal redis

Create a Caddy configuration file (typically /etc/caddy/Caddyfile):

api.example.com {
    # Use Redis to store rate limiting state
    redis {
        host 127.0.0.1
        port 6379
        key_prefix caddy_rate_limit_
    }

    # Rate limiting: 100 requests per minute per IP
    ratelimit {
        ip
        100 1m
    }

    # Forward requests to backend API
    reverse_proxy /api/* http://backend-api:8080
}

Explanation:

  • redis: Configures connection to the Redis server.
  • key_prefix: Sets a prefix for Redis keys (helps distinguish between services).
  • ratelimit: Defines rate limits based on IP, allowing 100 requests per minute.
  • reverse_proxy: Routes incoming requests to the backend API service.

Start Caddy and Verify

Start and enable the Caddy service:

sudo systemctl start caddy
sudo systemctl enable caddy

Test access to the API from a client:

curl -X GET https://api.example.com/api/test

Check Redis to verify request counts are being tracked:

redis-cli KEYS "caddy_rate_limit_*"
redis-cli GET "caddy_rate_limit_127.0.0.1"

Troubleshooting

  • Redis connection fails: Verify Redis is running, port 6379 is open, and host/port settings are correct.
  • Rate limiting not working: Ensure the Redis plugin is installed correctly and Caddy has been restarted after configuration changes.
  • HTTPS not functioning: Confirm a valid domain is used and Caddy can obtain an SSL certificate automatically.

References

Assumptions: This setup is suitable for small to medium development and production environments. For larger-scale systems, consider dedicated API Gateway solutions like Kong or Apigee.

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478