Redis Cluster usage

์žญ์žญ์ดยท2021๋…„ 6์›” 29์ผ
2

redis

๋ชฉ๋ก ๋ณด๊ธฐ
5/6
post-thumbnail

Redis Cluster usage

๐ŸŽ ๋ชฉ์ฐจ

๊ฐœ์š”

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

1. Start cluster

ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด 3๊ฐœ์˜ master๋กœ ์ด๋ฃจ์–ด์ง„ cluster๋ฅผ ๊ตฌ์„ฑํ•˜์˜€๋‹ค.

1.1. cluster table

nodecontainerport
1redis-master-17001
2redis-master-27002
3redis-master-37003

1.2. cluster info

$ docker exec -it redis-master-1 bash
$ redis-cli -p 7001
127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:87
cluster_stats_messages_pong_sent:92
cluster_stats_messages_sent:179
cluster_stats_messages_ping_received:90
cluster_stats_messages_pong_received:87
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:179

127.0.0.1:7001> cluster nodes
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624260271000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624260271040 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master - 0 1624260272044 2 connected 5461-10922

2. Get/Set data

redis๋Š” set $KEY $VALUE๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ , get $KEY๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

2.1. redis-cli -p $PORT

๋จผ์ € redis-cli๋กœ ์ ‘๊ทผ์„ ํ•œ๋‹ค.
7001๋ฒˆ ํฌํŠธ๋กœ ์ ‘์†์„ ํ•˜๋ฉด 7001๋ฒˆ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ํฌํŠธ์— access๋ฅผ ํ•  ๋•Œ์— error๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

root@ubuntu:/data$ redis-cli -p 7001
127.0.0.1:7001> set a b
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> get a
(error) MOVED 15495 127.0.0.1:7003
127.0.0.1:7001> set b c
OK
127.0.0.1:7001> get b
"c"
127.0.0.1:7001> exit

2.2. redis-cli -c -p $PORT

-c์˜ต์…˜์€ redis๋ฅผ cluster๋ชจ๋“œ๋กœ ์ ‘์†ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.
์œ„์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ํ•ด๋‹นํ•˜๋Š” hash slot์„ ๊ฐ€์ง„ ์ฃผ์†Œ๋กœ redirect๋ฅผ ํ•ด์ค€๋‹ค.

root@ubuntu:/data$ redis-cli -c -p 7001
127.0.0.1:7001> set a b
-> Redirected to slot [15495] located at 127.0.0.1:7003
OK
127.0.0.1:7003> get a
"b"
127.0.0.1:7003> set b c
-> Redirected to slot [3300] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get b
"c"
127.0.0.1:7001> set c d
-> Redirected to slot [7365] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get c
"d"
127.0.0.1:7002> get d
-> Redirected to slot [11298] located at 127.0.0.1:7003
(nil)
127.0.0.1:7003>
  • redis-cli -c -p 7001
    • 7001๋ฒˆ ํฌํŠธ์— ์„œ๋น„์Šค๋˜๊ณ  ์žˆ๋Š” cluster node์— ์ ‘์†ํ•œ๋‹ค.
  • 127.0.0.1:7001> set a b
    • 7001๋ฒˆ ๋…ธ๋“œ์—์„œ key:value๋ฅผ ์ €์žฅํ•œ๋‹ค. (a:b)
  • -> Redirected to slot [15495] located at 127.0.0.1:7003
    • a๋ผ๋Š” key๋ฅผ CRC16(key) mod 16384๋ฅผ ํ†ตํ•ด hashingํ•˜๊ณ , ํ•ด๋‹นํ•˜๋Š” hash slot์— redirect ํ•œ๋‹ค.
    • CRC16(a) mod 16384๋Š” 15495๋กœ 7003๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” hash slot์ด๋‹ค.
    • ๋”ฐ๋ผ์„œ 7003๋…ธ๋“œ๋กœ redirectํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.
  • 127.0.0.1:7003> get a
    • ์ดํ›„ 7003๋…ธ๋“œ์—์„œ ์‹คํ–‰๋˜๋ฉฐ ํ•ด๋‹น key์˜ value๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

3. Add node

์ด๋ฏธ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” cluster์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด์ž.

3.1. start new node

redis-7004.yaml์ด๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.
ํฌํŠธ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์„ค์ •์€ ๊ธฐ์กด ๋…ธ๋“œ๋“ค๊ณผ ๊ฐ™๋‹ค.

$ docker-compose -f redis-7004.yaml up -d
Creating redis-master-4 ... done

$ docker ps -a | grep redis
91f1d77161a7        redis:6.2.3         "docker-entrypoint.sโ€ฆ"   34 seconds ago      Up 32 seconds                                  redis-master-4
0706f949061d        redis:6.2.3         "docker-entrypoint.sโ€ฆ"   18 minutes ago      Up 18 minutes                                  redis-master-3
de5e52fb0428        redis:6.2.3         "docker-entrypoint.sโ€ฆ"   18 minutes ago      Up 9 minutes                                   redis-master-2
8a4cde915a47        redis:6.2.3         "docker-entrypoint.sโ€ฆ"   18 minutes ago      Up 18 minutes                                  redis-master-1

3.2. add node

cluster์— ์ƒˆ๋กœ ๋„์›Œ์ง„ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

$ docker exec -it redis-master-1 bash
$ redis-cli -p 7001 --cluster add-node 127.0.0.1:7004 127.0.0.1:7001
>>> Adding node 127.0.0.1:7004 to cluster 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001
   slots:[0-5460] (5461 slots) master
M: 3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003
   slots:[10923-16383] (5461 slots) master
M: 79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 127.0.0.1:7004 to make it join the cluster.
[OK] New node added correctly.
  • redis-cli -p port --cluster add-node ์ถ”๊ฐ€ํ• ๋…ธ๋“œ ์ž‘์—…ํ• ๋…ธ๋“œ
    • "์ถ”๊ฐ€ํ• ๋…ธ๋“œ"์— ์‹ ๊ทœ ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

3.3. check

127.0.0.1:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:4
cluster_size:3
cluster_current_epoch:3
  • cluster_size:3
    • ๋…ธ๋“œ๋Š” ์ถ”๊ฐ€๋˜์—ˆ์ง€๋งŒ hash slot์ด ํ• ๋‹น๋˜์ง€ ์•Š์•„ size๋Š” ๊ทธ๋Œ€๋กœ์ด๋‹ค.
127.0.0.1:7001> cluster nodes
2bc7574b8d23ec967260fb822cccad471e3cecfa 127.0.0.1:7004@17004 master - 0 1624261424857 0 connected
027a002ecc012b61a5997f151ad01bccbb65d1c0 127.0.0.1:7001@17001 myself,master - 0 1624261425000 1 connected 0-5460
3c349984f0bb61490c170ab68f2617a35d9581d6 127.0.0.1:7003@17003 master - 0 1624261425863 3 connected 10923-16383
79816979a6dd4b226e476121dd385ed6c25e5151 127.0.0.1:7002@17002 master - 0 1624261424000 2 connected 5461-10922
  • ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 7004๋…ธ๋“œ์— hash slot์ด ํ‘œ์‹œ๋˜์–ด์žˆ์ง€ ์•Š์Œ.

3.4. reshard

์‹ ๊ทœ ๋…ธ๋“œ๋Š” ๊ธฐ์กด ๋…ธ๋“œ์˜ hash slot์„ ๋‚˜๋ˆ„์–ด ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
reshard๋ฅผ ํ†ตํ•ด ๋‚˜๋ˆ„์–ด๊ฐ€์งˆ hash slot์„ ์ •ํ•  ์ˆ˜ ์žˆ๊ณ ,
rebalance๋ฅผ ํ†ตํ•ด hash slot์„ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ฐฐ๋ถ„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

4. Redis-cli

redis-cli์˜ ์˜ต์…˜์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • create
    • ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
    • replicas๋ฅผ ์ง€์ •ํ•˜์—ฌ slave๊ฐœ์ˆ˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.
  • reshard
    • ์Šฌ๋กฏ์„ ๋…ธ๋“œ์— ํ• ๋‹น/์žฌํ• ๋‹นํ•œ๋‹ค.
  • moveslots
    • ์Šฌ๋กฏ์„ ์ง€์ •ํ•˜์—ฌ ์ด๋™ํ•œ๋‹ค.
  • add-node
    • ํด๋Ÿฌ์Šคํ„ฐ์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
    • master/slave๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
  • del-node
    • ํด๋Ÿฌ์Šคํ„ฐ์—์„œ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  • info
    • ํด๋Ÿฌ์Šคํ„ฐ ์ •๋ณด๋ฅผ ์กฐํšŒํ•œ๋‹ค.
  • rebalance
    • ์Šฌ๋กฏ์„ ๊ท ํ˜•์žˆ๊ฒŒ ์žฌ๋ถ„๋ฐฐํ•œ๋‹ค.
  • check
    • ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ฒดํฌํ•œ๋‹ค.
  • call
    • ํด๋Ÿฌ์Šคํ„ฐ ๋…ธ๋“œ์— ๋ช…๋ น์„ ์‹คํ–‰ํ•œ๋‹ค.
  • set-timeout
    • ํด๋Ÿฌ์Šคํ„ฐ ๋…ธ๋“œ์— cluster-node-timeout์„ ์„ค์ •ํ•œ๋‹ค.
  • help
    • ๋„์›€๋ง์„ ๋ณด์—ฌ์ค€๋‹ค.
  • ์ฐธ๊ณ  ๋งํฌ

0๊ฐœ์˜ ๋Œ“๊ธ€