[Redis] Primary-Replica와 Sentinel로 장애에도 대응가능한 시스템 구축하기

shinny·2024년 7월 19일

[Troubleshooting]

목록 보기
4/4

🌟 문제 의식

Redis의 경우 쿠폰 발급 과정에서, 쿠폰 발급 요청을 저장하는 저장소 역할을 한다. 또한 캐시 저장 등의 역할도 하고 있다. 만일 Redis에 장애가 발생할 경우 전체 서비스가 중단되는 문제가 있다.

💥 Redis 서버에 장애가 발생해도, 즉각 대응할 수 있는 시스템을 만들어야한다.

1️⃣ 방법

  • Primary, Replica로 Redis 서버 운영
  • Sentinel로 Redis 관리(Primary에 문제 발생 시, Replica가 Primary로 승격)

1-1. Redis, Sentinel 같은 네트워크로 구성

  • docker-compose.yml
services:
  master:
    image: redis:latest
    container_name: master
    volumes:
      - ./master.conf:/usr/local/etc/redis/redis.conf
    command: redis-server /usr/local/etc/redis/redis.conf
    ports:
      - "6379:6379"
      - "6380:6380"
      - "6381:6381"
      - "5000:5000"
      - "5001:5001"
      - "5002:5002"
      
  redis-1:
    image: redis:latest
    network_mode: "service:master"
    container_name: slave-1
    volumes:
      - ./slave:/slave
    command: redis-server /slave/slave-1.conf

  redis-2:
    network_mode: "service:master"
    image: redis:latest
    container_name: slave-2
    volumes:
      - ./slave:/slave
    command: redis-server /slave/slave-2.conf

  sentinel-1:
    network_mode: "service:master"
    image: redis:latest
    container_name: sentinel-1
    volumes:
      - ./sentinel:/sentinel
    command: redis-server /sentinel/sentinel-1.conf --sentinel
    depends_on:
      - master
      
  sentinel-2:
    network_mode: "service:master"
    image: redis:latest
    container_name: sentinel-2
    volumes:
      - ./sentinel:/sentinel
    command: redis-server /sentinel/sentinel-2.conf --sentinel
    depends_on:
      - master
      
  sentinel-3:
    network_mode: "service:master"
    image: redis:latest
    container_name: sentinel-3
    volumes:
      - ./sentinel:/sentinel
    command: redis-server /sentinel/sentinel-3.conf --sentinel
    depends_on:
      - master

설명

  • network_mode : service:master로 같은 네트워크 내 구성

1-2.Primary, Replica 설정

  • master.conf
port 6379
bind 0.0.0.0
masterauth 1111
requirepass 1111
  • slave.conf
port 6380 // 6381
bind 0.0.0.0
replicaof master 6379
masterauth 1111
requirepass 1111
  • primary에서 info replication

  • replica에서 info replication

1-3. Sentinel

  • sentinel.conf
port 5000 // 5001, 5002
sentinel monitor myprimary master 6379 2
sentinel down-after-milliseconds myprimary 5000
sentinel failover-timeout myprimary 60000
sentinel auth-pass myprimary 1111
sentinel resolve-hostnames yes

sentinel monitor myprimary master 6379 2

  • sentinel이 primary redis 모니터링
  • 2 : primary down을 최종 결정하기위해 동의해야 하는 Sentinel 수

sentinel resolve-hostnames yes
Error 해결 : Can't resolve master instance hostname.

  • sentinel masters

  • sentinel slaves myprimary

2️⃣ 평가

  • Primary, Replica 설정(master : 1, slaves : 2)

    Replica : 쓰기 불가능, 읽기 가능
    Primary : 쓰기/읽기 가능, 쓰고 나서는 데이터가 Replica에 잘 복제됨

  • Sentinel 구축으로 Primary Redis에 문제 발생 시, Replica Redis가 Primary로 승격

  • sentinel-1의 log

순서

  • replica redis 2개 등록
  • primary redis down
  • sdown(subjectively down)
  • new epoch -> vote for leader
  • odown(objectively down) = 모든 sentinel이 sdown 동의
  • switch master to 6380
  • switch prev primary to replica(prev primary 다시 up되었을 때)
  • replica-1의 log

순서

  • connecting to primary(6379)
  • lost connection with primary
  • master mode enabled(primary로 승격됨)
  • 6381의 sync 요청
  • 6379(prev primary)의 sync 요청

3️⃣ 결론

redis 서버 장애 발생 시에도 즉각적인 대응이 가능해졌다.

profile
꾸준히, 성실하게, 탁월하게 매일 한다

0개의 댓글