Every modern application needs fast data access. Whether you're running a web app, API, or microservices architecture, Redis is the go-to solution for caching, session storage, and real-time data processing. Instead of relying on managed services with unpredictable costs, self-hosting Redis gives you complete control over performance, persistence, and security.
In this guide, you'll deploy Redis with Docker Compose, configure persistence and authentication, and verify it's ready for production workloads.
Why Self-Host Redis?
- Cost Control: Managed Redis instances charge per GB-hour. Self-hosting on a VPS you already own costs nothing extra.
- Zero Latency: Run Redis on the same network (or same machine) as your applications for sub-millisecond response times.
- Full Configuration Control: Tune eviction policies, persistence settings, and memory limits to match your exact workload.
- No Vendor Lock-in: Your data stays on your infrastructure. Migrate anywhere without egress fees.
Prerequisites
- A VPS or dedicated server with Docker and Docker Compose installed
- Minimum 512MB RAM (1GB+ recommended for production)
- Basic familiarity with Docker Compose
Step 1: Create the Docker Compose File
Create a directory for your Redis deployment:
mkdir -p ~/redis && cd ~/redis
Create a docker-compose.yml file:
version: "3.8"
services:
redis:
image: redis:7-alpine
container_name: redis
restart: unless-stopped
ports:
- "6379:6379"
volumes:
- redis_data:/data
- ./redis.conf:/usr/local/etc/redis/redis.conf
command: redis-server /usr/local/etc/redis/redis.conf
environment:
- TZ=UTC
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
volumes:
redis_data:
driver: local
Step 2: Configure Redis
Create a redis.conf file with production-ready settings:
cat > redis.conf << 'EOF'
# Network
bind 0.0.0.0
protected-mode yes
port 6379
# Authentication (CHANGE THIS PASSWORD)
requirepass your-strong-password-here
# Persistence
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
# Memory Management
maxmemory 256mb
maxmemory-policy allkeys-lru
# Logging
loglevel notice
# Security
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command CONFIG ""
EOF
Important: Replace your-strong-password-here with a strong, unique password. The rename-command directives disable dangerous commands that could wipe your data.
Step 3: Start Redis
docker compose up -d
Verify the container is running:
docker ps | grep redis
You should see output similar to:
CONTAINER ID IMAGE COMMAND STATUS PORTS
a1b2c3d4e5f6 redis:7-alpine "docker-entrypoint.s…" Up 2 minutes 0.0.0.0:6379->6379/tcp
Step 4: Verify Redis is Working
Test the connection with authentication:
docker exec -it redis redis-cli -a your-strong-password-here ping
Expected response:
PONG
Test basic operations:
docker exec -it redis redis-cli -a your-strong-password-here
# Set a key
SET mykey "Hello Redis"
# Get the key
GET mykey
# Check info
INFO memory
INFO stats
Step 5: Test from a Remote Application
If your application runs on a different server, test remote connectivity:
# From your application server
redis-cli -h your-redis-server-ip -p 6379 -a your-strong-password-here ping
If you get PONG, Redis is ready to accept connections.
Production Hardening Tips
1. Use a Reverse Proxy with TLS
For remote connections, always encrypt traffic. Put Redis behind a reverse proxy like Traefik or use stunnel:
# Add to docker-compose.yml (if using Traefik)
labels:
- "traefik.tcp.routers.redis.rule=HostSNI(`*`)"
- "traefik.tcp.routers.redis.entrypoints=redis"
- "traefik.tcp.routers.redis.tls=true"
- "traefik.tcp.services.redis.loadbalancer.server.port=6379"
2. Set Up Monitoring
Export Redis metrics to Prometheus:
redis-exporter:
image: oliver006/redis_exporter:latest
container_name: redis-exporter
restart: unless-stopped
environment:
- REDIS_ADDR=redis://redis:6379
- REDIS_PASSWORD=your-strong-password-here
ports:
- "9121:9121"
3. Configure Backups
Add a backup service to dump Redis data periodically:
redis-backup:
image: redis:7-alpine
container_name: redis-backup
restart: unless-stopped
volumes:
- ./backups:/backups
entrypoint: |
sh -c "
while true; do
sleep 86400
redis-cli -h redis -a your-strong-password-here --rdb /backups/dump-$$(date +%Y%m%d).rdb
done
"
4. Restrict Network Access
If Redis only serves local containers, remove the port mapping and use Docker networks:
services:
redis:
# Remove: ports:
# - "6379:6379"
networks:
- app_network
networks:
app_network:
driver: bridge
Common Use Cases
- Session Storage: Store user sessions for web applications (Express, Django, Rails)
- API Rate Limiting: Use Redis with libraries like
rate-limiter-flexible - Job Queues: Power background workers with BullMQ, Celery, or Sidekiq
- Real-Time Features: Pub/sub for chat, notifications, and live updates
- Database Caching: Cache expensive queries with Redis as a query cache layer
Troubleshooting
Connection Refused
# Check if Redis is listening
docker exec -it redis netstat -tlnp | grep 6379
# Check logs
docker logs redis
Authentication Failed
# Verify password in redis.conf
docker exec -it redis cat /usr/local/etc/redis/redis.conf | grep requirepass
Out of Memory
# Check memory usage
docker exec -it redis redis-cli -a your-password info memory | grep used_memory_human
# Adjust maxmemory in redis.conf and restart
docker compose restart redis
Next Steps
Now that Redis is running, consider these related guides:
- Self-Host Grafana + Prometheus — Monitor Redis metrics with dashboards
- Self-Host Traefik — Add TLS termination for secure remote connections
- Self-Host n8n — Build workflows that use Redis for queue management
- Self-Host PostgreSQL — Pair Redis with a production database setup
Conclusion
Redis is one of the most impactful services you can self-host. In under 10 minutes, you've deployed a production-ready caching layer with persistence, authentication, and security hardening. The performance gains for your applications will be immediate, and the cost savings over managed alternatives add up quickly.
Keep your Redis instance secure, monitor its memory usage, and back up your data. With these practices in place, Redis will serve as a reliable foundation for your infrastructure.