Redis Sentinel HA
A production-grade Redis high-availability stack using Redis Sentinel for automatic failover and health monitoring. The cluster consists of one primary Redis instance for writes, two read replicas for scaling read traffic, and three Sentinel nodes that monitor the primary and trigger automatic promotion of a replica if the primary fails. With three sentinels, the quorum requirement of two ensures split-brain scenarios are avoided during network partitions. The primary is configured with AOF persistence and periodic RDB snapshots for data durability, while replicas synchronize asynchronously. All services include health checks and resource limits, and data is persisted in named volumes across the primary and replica nodes. This stack is ideal for applications requiring sub-second failover and high cache availability.
Included Services
redis_primary
redis:7-alpine
redis_replica_1
redis:7-alpine
redis_replica_2
redis:7-alpine
sentinel_1
redis:7-alpine
sentinel_2
redis:7-alpine
sentinel_3
redis:7-alpine
Generated YAML
# Generated by ComposeHub (composehub.dev)
name: redis-sentinel
services:
redis_primary:
image: redis:7-alpine
restart: unless-stopped
command:
- redis-server
- "--maxmemory"
- ${REDIS_MAXMEMORY:-256mb}
- "--maxmemory-policy"
- allkeys-lru
- "--appendonly"
- yes
- "--appendfsync"
- everysec
- "--save"
- "900"
- "1"
- "--save"
- "300"
- "10"
- "--save"
- "60"
- "10000"
- "--requirepass"
- ${REDIS_PASSWORD:-changeme}
- "--masterauth"
- ${REDIS_PASSWORD:-changeme}
ports:
- 6379:6379
volumes:
- redis_primary_data:/data
networks:
- redis
healthcheck:
test:
- CMD-SHELL
- redis-cli -a ${REDIS_PASSWORD:-changeme} ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 10s
deploy:
resources:
limits:
cpus: "1.00"
memory: 384M
reservations:
cpus: "0.25"
memory: 128M
labels:
com.composeforge.description: Redis 7 primary node (read/write)
redis_replica_1:
image: redis:7-alpine
restart: unless-stopped
command:
- redis-server
- "--replicaof"
- redis_primary
- "6379"
- "--maxmemory"
- ${REDIS_MAXMEMORY:-256mb}
- "--maxmemory-policy"
- allkeys-lru
- "--appendonly"
- yes
- "--appendfsync"
- everysec
- "--requirepass"
- ${REDIS_PASSWORD:-changeme}
- "--masterauth"
- ${REDIS_PASSWORD:-changeme}
- "--replica-read-only"
- yes
volumes:
- redis_replica1_data:/data
networks:
- redis
depends_on:
redis_primary:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- redis-cli -a ${REDIS_PASSWORD:-changeme} ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
deploy:
resources:
limits:
cpus: "0.50"
memory: 384M
reservations:
cpus: "0.10"
memory: 64M
labels:
com.composeforge.description: Redis 7 replica node 1 (read-only)
redis_replica_2:
image: redis:7-alpine
restart: unless-stopped
command:
- redis-server
- "--replicaof"
- redis_primary
- "6379"
- "--maxmemory"
- ${REDIS_MAXMEMORY:-256mb}
- "--maxmemory-policy"
- allkeys-lru
- "--appendonly"
- yes
- "--appendfsync"
- everysec
- "--requirepass"
- ${REDIS_PASSWORD:-changeme}
- "--masterauth"
- ${REDIS_PASSWORD:-changeme}
- "--replica-read-only"
- yes
volumes:
- redis_replica2_data:/data
networks:
- redis
depends_on:
redis_primary:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- redis-cli -a ${REDIS_PASSWORD:-changeme} ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
deploy:
resources:
limits:
cpus: "0.50"
memory: 384M
reservations:
cpus: "0.10"
memory: 64M
labels:
com.composeforge.description: Redis 7 replica node 2 (read-only)
sentinel_1:
image: redis:7-alpine
restart: unless-stopped
command:
- sh
- "-c"
- |-
echo "sentinel monitor mymaster redis_primary 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster ${REDIS_PASSWORD:-changeme}
sentinel resolve-hostnames yes
sentinel announce-hostnames yes" > /tmp/sentinel.conf && redis-sentinel /tmp/sentinel.conf
ports:
- 26379:26379
networks:
- redis
depends_on:
redis_primary:
condition: service_healthy
redis_replica_1:
condition: service_healthy
redis_replica_2:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- redis-cli -p 26379 ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
deploy:
resources:
limits:
cpus: "0.25"
memory: 64M
reservations:
cpus: "0.05"
memory: 16M
labels:
com.composeforge.description: Redis Sentinel node 1 for monitoring and failover
sentinel_2:
image: redis:7-alpine
restart: unless-stopped
command:
- sh
- "-c"
- |-
echo "sentinel monitor mymaster redis_primary 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster ${REDIS_PASSWORD:-changeme}
sentinel resolve-hostnames yes
sentinel announce-hostnames yes" > /tmp/sentinel.conf && redis-sentinel /tmp/sentinel.conf
networks:
- redis
depends_on:
redis_primary:
condition: service_healthy
redis_replica_1:
condition: service_healthy
redis_replica_2:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- redis-cli -p 26379 ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
deploy:
resources:
limits:
cpus: "0.25"
memory: 64M
reservations:
cpus: "0.05"
memory: 16M
labels:
com.composeforge.description: Redis Sentinel node 2 for monitoring and failover
sentinel_3:
image: redis:7-alpine
restart: unless-stopped
command:
- sh
- "-c"
- |-
echo "sentinel monitor mymaster redis_primary 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 10000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster ${REDIS_PASSWORD:-changeme}
sentinel resolve-hostnames yes
sentinel announce-hostnames yes" > /tmp/sentinel.conf && redis-sentinel /tmp/sentinel.conf
networks:
- redis
depends_on:
redis_primary:
condition: service_healthy
redis_replica_1:
condition: service_healthy
redis_replica_2:
condition: service_healthy
healthcheck:
test:
- CMD-SHELL
- redis-cli -p 26379 ping | grep -q PONG
interval: 15s
timeout: 5s
retries: 3
start_period: 15s
deploy:
resources:
limits:
cpus: "0.25"
memory: 64M
reservations:
cpus: "0.05"
memory: 16M
labels:
com.composeforge.description: Redis Sentinel node 3 for monitoring and failover
networks:
redis:
driver: bridge
volumes:
redis_primary_data:
driver: local
redis_replica1_data:
driver: local
redis_replica2_data:
driver: local
Quick Info
- Services
- 6
- Networks
- 1
- Volumes
- 3
When to Use
This template is ideal for setting up a redis sentinel ha environment. All services are pre-configured with healthchecks, resource limits, and sensible defaults. Customize environment variables before deploying to production.
Tips
- Change all default passwords before deploying
- Review resource limits for your hardware
- Add a reverse proxy for production HTTPS
- Configure backup strategies for data volumes