Skip to content

Instantly share code, notes, and snippets.

@jdnichollsc
Created February 11, 2025 18:48
Show Gist options
  • Save jdnichollsc/6a1e7bf764ff858f3ba1359647b13a2e to your computer and use it in GitHub Desktop.
Save jdnichollsc/6a1e7bf764ff858f3ba1359647b13a2e to your computer and use it in GitHub Desktop.
Redis cluster mode enabled with Docker compose for local development
version: '3.6'
# Define name templates
x-name-templates:
project-name: &project-name ${COMPOSE_PROJECT_NAME:-solana}
service-names:
redis-node-0: &name-redis-0 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-0
redis-node-1: &name-redis-1 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-1
redis-node-2: &name-redis-2 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-2
redis-node-3: &name-redis-3 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-3
redis-node-4: &name-redis-4 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-4
redis-node-5: &name-redis-5 ${COMPOSE_PROJECT_NAME:-solana}-redis-node-5
redis-cluster: &name-redis-cluster ${COMPOSE_PROJECT_NAME:-solana}-redis-cluster
redis-commander: &name-redis-commander ${COMPOSE_PROJECT_NAME:-solana}-redis-commander
transaction-simulator: &name-simulator ${COMPOSE_PROJECT_NAME:-solana}-transaction-simulator
volume-names:
redis-0: &vol-redis-0 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-0
redis-1: &vol-redis-1 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-1
redis-2: &vol-redis-2 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-2
redis-3: &vol-redis-3 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-3
redis-4: &vol-redis-4 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-4
redis-5: &vol-redis-5 ${COMPOSE_PROJECT_NAME:-solana}-redis-data-5
network-names:
default: &network-default ${COMPOSE_PROJECT_NAME:-solana}-network
# Define common service settings to reduce duplication
x-service-common: &service-common
env_file: .env
depends_on:
redis-node-0:
condition: service_healthy
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
- /app/node_modules
# Redis node common configuration
x-redis-node: &redis-node
image: redis:7.2
command: redis-server --port 6379
--cluster-enabled yes
--cluster-config-file nodes.conf
--cluster-node-timeout 5000
--appendonly yes
--bind 0.0.0.0
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 5s
retries: 20
start_period: 5s
services:
# Redis Cluster Configuration
redis-node-0:
<<: *redis-node
container_name: *name-redis-0
ports:
- "6379:6379"
volumes:
- redis_data_0:/data
redis-node-1:
<<: *redis-node
container_name: *name-redis-1
ports:
- "6380:6379"
volumes:
- redis_data_1:/data
redis-node-2:
<<: *redis-node
container_name: *name-redis-2
ports:
- "6381:6379"
volumes:
- redis_data_2:/data
redis-node-3:
<<: *redis-node
container_name: *name-redis-3
ports:
- "6382:6379"
volumes:
- redis_data_3:/data
redis-node-4:
<<: *redis-node
container_name: *name-redis-4
ports:
- "6383:6379"
volumes:
- redis_data_4:/data
redis-node-5:
<<: *redis-node
container_name: *name-redis-5
ports:
- "6384:6379"
volumes:
- redis_data_5:/data
redis-cluster:
image: redis:7.2
container_name: *name-redis-cluster
command: >
sh -c '
echo "βŒ› Waiting for Redis nodes to be ready..." &&
sleep 10 &&
echo "πŸ”„ Creating Redis Cluster..." &&
redis-cli --cluster create redis-node-0:6379 redis-node-1:6379 redis-node-2:6379 redis-node-3:6379 redis-node-4:6379 redis-node-5:6379 --cluster-replicas 1 --cluster-yes &&
echo "βœ… Redis Cluster created successfully!" &&
echo "πŸ” Verifying cluster status..." &&
redis-cli -h redis-node-0 -p 6379 cluster info | grep cluster_state:ok || exit 1 &&
tail -f /dev/null
'
depends_on:
redis-node-0:
condition: service_healthy
redis-node-1:
condition: service_healthy
redis-node-2:
condition: service_healthy
redis-node-3:
condition: service_healthy
redis-node-4:
condition: service_healthy
redis-node-5:
condition: service_healthy
redis-commander:
image: rediscommander/redis-commander:latest
container_name: *name-redis-commander
environment:
- REDIS_HOSTS=redis-node-0:redis-node-0:6379,redis-node-1:redis-node-1:6379,redis-node-2:redis-node-2:6379,redis-node-3:redis-node-3:6379,redis-node-4:redis-node-4:6379,redis-node-5:redis-node-5:6379
ports:
- "8081:8081"
depends_on:
- redis-cluster
# SERVICES
# Transaction Simulator Service
transaction-simulator:
<<: *service-common
container_name: *name-simulator
command: >
bash -c "
npm run start:simulator
"
environment:
- TRANSACTIONS_PER_SECOND=500
- REDIS_CLUSTER_NODES=redis-node-0:6379,redis-node-1:6379,redis-node-2:6379,redis-node-3:6379,redis-node-4:6379,redis-node-5:6379
networks:
default:
name: *network-default
driver: bridge
volumes:
redis_data_0:
name: *vol-redis-0
redis_data_1:
name: *vol-redis-1
redis_data_2:
name: *vol-redis-2
redis_data_3:
name: *vol-redis-3
redis_data_4:
name: *vol-redis-4
redis_data_5:
name: *vol-redis-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment