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.
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
- Guide to Migrating from Shared Hosting to VPS: 7-Day Roadmap for Non-Technical Users
- Configure Oracle ARM VPS for Ghost CMS and SQLite with High Performance and Millions of Views
- NVMe Cloud VPS for Docker, Nginx & Databases
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.
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.
VPS-4E
10% VAT included
- CPU: 4 vCPU
- RAM: 8 GB
- SSD: 50 GB NVMe
