Redis Cluster
๐Ÿ™ˆ

Redis Cluster

Created
Jul 18, 2024 04:11 AM
Last edited time
Last updated July 18, 2024
Tags
Backend
Language
URL

Intro::

Redis Cluster์— ๋Œ€ํ•œ ์ •๋ฆฌ๋ณธ์ž…๋‹ˆ๋‹ค.
ย 

Redis Cluster ๋ž€??

Redis ํด๋Ÿฌ์Šคํ„ฐ๋Š” Redis์˜ ๋ถ„์‚ฐ ๋ฒ„์ „์œผ๋กœ, ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ ๋…ธ๋“œ์— ๋ถ„์‚ฐ ์ €์žฅํ•˜์—ฌ ๊ณ ๊ฐ€์šฉ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. Redis ํด๋Ÿฌ์Šคํ„ฐ๋Š” ์—ฌ๋Ÿฌ Redis ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜์˜ ํด๋Ÿฌ์Šคํ„ฐ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ์ƒค๋”ฉํ•˜๊ณ , ๋…ธ๋“œ ์žฅ์•  ์‹œ ์ž๋™์œผ๋กœ ๋ณต๊ตฌํ•ฉ๋‹ˆ๋‹ค.

ํŠน์ง•

  1. ์ž๋™ ์ƒค๋”ฉ:
      • Redis ํด๋Ÿฌ์Šคํ„ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ์—ฌ๋Ÿฌ ๋…ธ๋“œ์— ๋ถ„์‚ฐ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€ํ•˜๋ฅผ ์ค„์ž…๋‹ˆ๋‹ค.
  1. ๊ณ ๊ฐ€์šฉ์„ฑ:
      • ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด์˜ ๊ฐ ๋…ธ๋“œ๋Š” ์ฃผ ๋…ธ๋“œ(๋งˆ์Šคํ„ฐ)์™€ ๋ณด์กฐ ๋…ธ๋“œ(์Šฌ๋ ˆ์ด๋ธŒ)๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋งˆ์Šคํ„ฐ ๋…ธ๋“œ๊ฐ€ ์žฅ์• ๋ฅผ ์ผ์œผํ‚ค๋ฉด ์Šฌ๋ ˆ์ด๋ธŒ ๋…ธ๋“œ๊ฐ€ ์ž๋™์œผ๋กœ ์Šน๊ฒฉ๋˜์–ด ๋งˆ์Šคํ„ฐ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  1. ํ™•์žฅ์„ฑ:
      • ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ•˜์—ฌ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ €์žฅ ์šฉ๋Ÿ‰๊ณผ ์ฒ˜๋ฆฌ ๋Šฅ๋ ฅ์„ ์œ ์—ฐํ•˜๊ฒŒ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.
  1. ๋…ธ๋“œ ๊ฐ„ ํ†ต์‹ :
      • Redis ํด๋Ÿฌ์Šคํ„ฐ์˜ ๋…ธ๋“œ๋“ค์€ ์„œ๋กœ ํ†ต์‹ ํ•˜๋ฉฐ, ๋ฐ์ดํ„ฐ ๋ถ„์‚ฐ๊ณผ ๋ณต์ œ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  1. ํŒŒํ‹ฐ์…”๋‹:
      • ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒํ‹ฐ์…”๋‹ํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ์— ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค. Redis ํด๋Ÿฌ์Šคํ„ฐ๋Š” ํ•ด์‹œ ์Šฌ๋กฏ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ 16384๊ฐœ์˜ ์Šฌ๋กฏ์— ๋งคํ•‘ํ•˜๊ณ , ์ด๋ฅผ ๋…ธ๋“œ์— ๋ถ„๋ฐฐํ•ฉ๋‹ˆ๋‹ค.

์žฅ์ 

  1. ๋†’์€ ๊ฐ€์šฉ์„ฑ:
      • ๋…ธ๋“œ ์žฅ์•  ์‹œ ์ž๋™์œผ๋กœ ๋ณต๊ตฌ๋˜๋ฏ€๋กœ ์„œ๋น„์Šค์˜ ๊ฐ€์šฉ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ์ˆ˜ํ‰์  ํ™•์žฅ:
      • ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ €์žฅ ์šฉ๋Ÿ‰๊ณผ ์ฒ˜๋ฆฌ ์„ฑ๋Šฅ์„ ์‰ฝ๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ์ž๋™ ๋ฐ์ดํ„ฐ ๋ถ„๋ฐฐ:
      • ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ๋ถ„์‚ฐ ์ €์žฅํ•˜์—ฌ ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€ํ•˜๋ฅผ ์ค„์ด๊ณ , ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
  1. ๊ณ ์† ์„ฑ๋Šฅ:
      • ๋ถ„์‚ฐ๋œ ๋…ธ๋“œ๋“ค์ด ๋ณ‘๋ ฌ๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ, ๋†’์€ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹จ์ 

  1. ๋ณต์žกํ•œ ์„ค์ •:
      • ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์„ค์ •ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ณผ์ •์ด ๋‹จ์ผ ์ธ์Šคํ„ด์Šค Redis์— ๋น„ํ•ด ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค.
  1. ๋„คํŠธ์›Œํฌ ์˜ค๋ฒ„ํ—ค๋“œ:
      • ๋…ธ๋“œ ๊ฐ„ ํ†ต์‹ ์œผ๋กœ ์ธํ•ด ๋„คํŠธ์›Œํฌ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ์ผ๊ด€์„ฑ ๋ฌธ์ œ:
      • ๋„คํŠธ์›Œํฌ ๋ถ„ํ•  ๋˜๋Š” ๋…ธ๋“œ ์žฅ์•  ์‹œ ์ผ์‹œ์ ์ธ ์ผ๊ด€์„ฑ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  1. ์ œํ•œ๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ง€์›:
      • ํŠน์ • ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ ๋ช…๋ น์–ด๋Š” ํด๋Ÿฌ์Šคํ„ฐ ๋ชจ๋“œ์—์„œ ์™„์ „ํžˆ ์ง€์›๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

Dockerfile.redis

FROM redis:latest CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]

Dockerfile.alpine

FROM alpine:latest RUN apk add --no-cache redis

redis.conf

port 6379 daemonize no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 3000 appendonly yes requirepass ๋น„๋ฐ€๋ฒˆํ˜ธ masterauth ๋น„๋ฐ€๋ฒˆ

docker-compose.yml

services: redis-master-1: container_name: redis-master-1 build: context: . dockerfile: Dockerfile.redis ports: - "6379:6379" volumes: - redis-master-1-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-master-2: container_name: redis-master-2 build: context: . dockerfile: Dockerfile.redis ports: - "6380:6379" volumes: - redis-master-2-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-master-3: container_name: redis-master-3 build: context: . dockerfile: Dockerfile.redis ports: - "6381:6379" volumes: - redis-master-3-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-slave-1: container_name: redis-slave-1 build: context: . dockerfile: Dockerfile.redis ports: - "6382:6379" volumes: - redis-slave-1-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-slave-2: container_name: redis-slave-2 build: context: . dockerfile: Dockerfile.redis ports: - "6383:6379" volumes: - redis-slave-2-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-slave-3: container_name: redis-slave-3 build: context: . dockerfile: Dockerfile.redis ports: - "6384:6379" volumes: - redis-slave-3-data:/data - ./redis.conf:/usr/local/etc/redis/redis.conf networks: - redis-cluster-net redis-setup: container_name: redis-setup build: context: . dockerfile: Dockerfile.alpine depends_on: - redis-master-1 - redis-master-2 - redis-master-3 - redis-slave-1 - redis-slave-2 - redis-slave-3 environment: - REDIS_PASSWORD=password volumes: - ./setup-redis-cluster.sh:/usr/local/etc/redis/setup-redis-cluster.sh networks: - redis-cluster-net command: ["sh", "-c", "chmod +x /usr/local/etc/redis/setup-redis-cluster.sh && /usr/local/etc/redis/setup-redis-cluster.sh"] volumes: redis-master-1-data: redis-master-2-data: redis-master-3-data: redis-slave-1-data: redis-slave-2-data: redis-slave-3-data: networks: redis-cluster-net:

setup-redis-cluster.sh

#!/bin/sh # Install redis-cli apk add --no-cache redis echo "Waiting for Redis nodes to start..." sleep 10 echo "Creating Redis cluster with masters..." yes yes | redis-cli --cluster create \ redis-master-1:6379 \ redis-master-2:6379 \ redis-master-3:6379 \ --cluster-replicas 0 \ -a ${REDIS_PASSWORD} echo "Adding slaves to the cluster..." redis-cli --cluster add-node redis-slave-1:6379 redis-master-1:6379 --cluster-slave -a ${REDIS_PASSWORD} redis-cli --cluster add-node redis-slave-2:6379 redis-master-2:6379 --cluster-slave -a ${REDIS_PASSWORD} redis-cli --cluster add-node redis-slave-3:6379 redis-master-3:6379 --cluster-slave -a ${REDIS_PASSWORD}

๋ฐ์ดํ„ฐ ํ™•์ธ ์˜ˆ์ œ

# ์ปจํ…Œ์ด๋„ˆ ์ ‘์† docker exec -it redis-master-1 sh # ํด๋Ÿฌ์Šคํ„ฐ ๋ชจ๋“œ ๋ ˆ๋””์Šค cli ์ ‘์† redis-cli -c -a password # key value ์ €์žฅ set key1 "Hello, Redis!" # ๋ฐ์ดํ„ฐ ํ™•์ธ get key1 "Hello, Redis!" # ๋ฐ์ดํ„ฐ๊ฐ€ ์‹ค์ œ๋กœ ์ €์žฅ๋œ ํ‚ค์Šฌ๋กฏํ™•์ธ CLUSTER KEYSLOT key1 (integer) 9189 # ๋…ธ๋“œ๋ณ„ ๋‹ด๋‹น ํ‚ค์Šฌ๋กฏ๋ฒ”์œ„๋ฅผ ํ™•์ธํ•˜์—ฌ ์–ด๋Š ๋…ธ๋“œ๊ฐ€ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š”์ง€ ํŒŒ์•… ๊ฐ€๋Šฅ CLUSTER NODES 2dc5480b5c9521c5d987d56136ef23cc4422cfa2 172.19.0.2:6379@16379 slave 3376c85cb33ca95be7a30ef980019e2c50da7afb 0 1721288671554 3 connected 3376c85cb33ca95be7a30ef980019e2c50da7afb 172.19.0.5:6379@16379 master - 0 1721288671554 3 connected 10923-16383 121edfec52734ec5d1d3c3e7fc4e6da91996d0f7 172.19.0.4:6379@16379 slave 6313f3c66d1e407140f9140e368fda08e3f3f10e 0 1721288671554 2 connected 1adf90a107637a3e45fdb9bd19fc55b8c4b0a423 172.19.0.7:6379@16379 slave 4b82b9e5bbf9676a52eab80dd17eea77a938aa9d 0 1721288671050 1 connected 6313f3c66d1e407140f9140e368fda08e3f3f10e 172.19.0.3:6379@16379 master - 0 1721288671000 2 connected 5461-10922 4b82b9e5bbf9676a52eab80dd17eea77a938aa9d 172.19.0.6:6379@16379 myself,master - 0 1721288670000 1 connected 0-5460
ํ‚ค์Šฌ๋กฏ์— ๋ถ„์‚ฐ๋˜์–ด ํ‚ค๊ฐ€ ์ €์žฅ๋˜๋ฉฐ, ํ‚ค์Šฌ๋กฏ์—๋Š” ์—ฌ๋Ÿฌ ํ‚ค์Šฌ๋กฏ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ย 

Trouble Shooting

WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
# ๋กœ์ปฌ์—์„œ ์„ค์ • sudo sysctl vm.overcommit_memory=1
์ปจํ…Œ์ด๋„ˆ์—์„œ๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ๋ณด์•ˆ์ ์ธ ์ด์œ ๋กœ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
ย 

References::

5๋ถ„ ์•ˆ์— ๊ตฌ์ถ•ํ•˜๋Š” Redis-Cluster
Redis-Cluster ํŠน์ง•? master๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ ๋‘์–ด ๋ถ„์‚ฐ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ(Sharding), scale out ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์„œ๋ฒ„๋ฅผ ๋Š˜๋ฆด์ˆ˜๋ก ์ €์žฅํ• ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด ๋ฌดํ•œ๋Œ€๋กœ ์ปค์ง„๋‹ค. master์— ํ•˜๋‚˜ ์ด์ƒ์˜ slave ๋ฅผ ๋‘˜ ์ˆ˜ ์žˆ๋‹ค. master 1,2,3 ์ด ์žˆ๋‹ค๋ฉด ๋ฐ์ดํ„ฐ๋Š” 3๊ฐœ์ค‘์— ํ•˜๋‚˜์— ์ €์žฅ๋˜๋ฉฐ, client ๊ฐ€ ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ ์š”์ฒญ์‹œ ์ €์žฅ๋œ ๊ณณ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋งˆ์Šคํ„ฐ์— ์š”์ฒญ ํ–ˆ๋‹ค๋ฉด ์ €์žฅ๋œ ๋งˆ์Šคํ„ฐ ์ •๋ณด๋ฅผ ์•Œ๋ ค์ฃผ๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์ „๋‹ฌ๋ฐ›์€ ๋งˆ์Šคํ„ฐ ์ •๋ณด์— ๋‹ค์‹œ ์š”์ฒญํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค. But, ํ•ด๋‹น ๋ถ€๋ถ„์€ redis-cluster ๋ฅผ ์ง€์›ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋‹ค ํ•ด์ค€๋‹ค. * ์ฐธ๊ณ  - scale up : ๋‹จ์ผ ์„œ๋ฒ„์˜ ์ŠคํŽ™์„ ์˜ฌ๋ ค ์„œ๋ฒ„ ์„ฑ๋Šฅ์„ ๋†’ํžŒ๋‹ค. - scale out : ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์„œ๋ฒ„ ..
5๋ถ„ ์•ˆ์— ๊ตฌ์ถ•ํ•˜๋Š” Redis-Cluster

Loading Comments...