x-defaults: &defaults
  restart: unless-stopped
  logging:
    driver: json-file
    options:
      max-size: 100m
      max-file: '3'

services:
  redis-cluster-init:
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: >
      sh -c "
      echo 'Waiting for all Redis instances to start...' &&
      sleep 5 &&
      redis-cli --cluster create
      redis-1:6379 redis-2:6379 redis-3:6379
      redis-4:6379 redis-5:6379 redis-6:6379
      --cluster-replicas 1 --cluster-yes
      "
    depends_on:
      redis-1:
        condition: service_healthy
      redis-2:
        condition: service_healthy
      redis-3:
        condition: service_healthy
      redis-4:
        condition: service_healthy
      redis-5:
        condition: service_healthy
      redis-6:
        condition: service_healthy
    profiles:
      - init

  redis-1:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7000:6379'
    volumes:
      - redis_1_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_1_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_1_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_1_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_1_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  redis-2:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7001:6379'
    volumes:
      - redis_2_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_2_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_2_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_2_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_2_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  redis-3:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7002:6379'
    volumes:
      - redis_3_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_3_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_3_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_3_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_3_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  redis-4:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7003:6379'
    volumes:
      - redis_4_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_4_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_4_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_4_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_4_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  redis-5:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7004:6379'
    volumes:
      - redis_5_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_5_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_5_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_5_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_5_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

  redis-6:
    <<: *defaults
    image: ${GLOBAL_REGISTRY:-}redis:${REDIS_VERSION:-8.2.1-alpine}
    environment:
      - TZ=${TZ:-UTC}
    command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
    ports:
      - '7005:6379'
    volumes:
      - redis_6_data:/data
    deploy:
      resources:
        limits:
          cpus: ${REDIS_CLUSTER_6_CPU_LIMIT:-0.5}
          memory: ${REDIS_CLUSTER_6_MEMORY_LIMIT:-512M}
        reservations:
          cpus: ${REDIS_CLUSTER_6_CPU_RESERVATION:-0.25}
          memory: ${REDIS_CLUSTER_6_MEMORY_RESERVATION:-256M}
    healthcheck:
      test: [CMD, redis-cli, ping]
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 5s

volumes:
  redis_1_data:
  redis_2_data:
  redis_3_data:
  redis_4_data:
  redis_5_data:
  redis_6_data:
