Skip to content

Commit 54bcbcc

Browse files
committed
2024-07-07 add redis posts
1 parent 6d25fe9 commit 54bcbcc

5 files changed

Lines changed: 158 additions & 79 deletions

File tree

_posts/2024/fragments/2024-06-30-redis-key-rules.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ read_time: true
66
share: true
77
related: true
88
categories:
9-
- redis
9+
- Redis
1010
tags:
11-
- redis
12-
- key
13-
- rules
14-
- convention
11+
- Redis
12+
- Key
13+
- Convention
1514
toc: false
1615
toc_stiky: true
1716
description: ""
1817
article_section: ""
1918
meta_keywords: ""
20-
last_modified_at: '"2024-06-30 09:00:00 +0800"'
19+
last_modified_at: 2024-07-07T15:15:52+09:00
20+
created: 2024-06-22T10:56:38+09:00
2121
---
22-
2322
## TL; DR
2423
- 계층 구조를 표현하기 위한 delimiter를 정합니다 (":")
2524
- 쉽게 이해 가능하기 위한 길이로 key를 구성합니다.
@@ -32,6 +31,3 @@ Redis 키는 binary safe합니다. 즉, "foo"와 같은 문자열부터 JPEG 파
3231
- 스키마를 고수하려고 노력하세요.
3332
- 예를 들어 "object-type:id"는 "user:1000"처럼 좋은 아이디어입니다. .이나 -는 "comment:4321:reply.to" 또는 "comment:4321:reply-to"처럼 여러 단어로 구성된 필드에 자주 사용될 수 있습니다.
3433
- 허용되는 최대 키 크기는 512MB입니다.
35-
36-
# Refrences
37-
- https://redis.io/docs/latest/develop/use/keyspace/
Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,51 @@
11
---
2-
title: "redis cluster mode"
2+
title: redis - cluster mode
33
layout: single
44
author_profile: false
55
read_time: true
66
share: true
77
related: true
88
categories:
9-
- Daily
9+
- Redis
1010
tags:
11-
- Blog
11+
- Redis
12+
- Redis-Cluster
1213
toc: false
1314
toc_stiky: true
14-
description: ""
15+
description:
1516
article_section: ""
1617
meta_keywords: ""
17-
last_modified_at: '"2024-07-06 09:00:00 +0800"'
18+
last_modified_at: 2024-07-07T15:28:11+09:00
19+
created: 2024-07-06T15:10:07+09:00
1820
---
1921
redis cluster mode에 대해 이해하고, 현재 구성된 redis 환경 정보를 확인하는 방법에 대해 알아봅니다.
2022

2123
## What is Cluster Mode?
22-
> Redis 클러스터는 단순히 [데이터 분할 전략 입니다.](https://www.digitalocean.com/community/tutorials/understanding-database-sharding). 여러 Redis 노드에 걸쳐 데이터를 자동으로 분할합니다.
24+
> redis 클러스터는 단순히 [데이터 분할 전략 입니다.](https://www.digitalocean.com/community/tutorials/understanding-database-sharding) 여러 redis 노드에 걸쳐 데이터를 자동으로 분할합니다.
2325
24-
Redis는 주노드(Primary node)와 복제 노드(Replica Node)로 구성하는 것이 일반적이며, 이런 구성을 shard
25-
로 여러 벌 준비하여 data partinioning을 수행하는 것이 클러스터 모드(cluster mode) 입니다.
26-
샤딩으로 데이터는 수평분할 어 저장됨에 따라 scale out, 단일 장애 지점(SPOF) 개선, 성능 향상 등의 이점을 얻을 수 있습니다.
26+
redis는 주노드(primary node)와 복제 노드(replica node)로 구성하는 것이 일반적이며, 이런 구성을 shard
27+
로 여러 벌 준비하여 data partitioning이 될 수 있는 구성이 클러스터 모드(cluster mode) 입니다.
2728

28-
![[Pasted image 20240706161619.png]]
29-
(출처: https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/)
30-
![[Pasted image 20240706161646.png]]
31-
(출처: https://ot-container-kit.github.io/redis-operator/guide/setup.html?ref=breezymind.com#redis-cluster)![[Pasted image 20240706161857.png]]
32-
(출처: https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/)
29+
shading으로 데이터는 수평분할 어 저장됨에 따라 scale out, 단일 장애 지점(SPOF) 개선, 성능 향상 등의 이점을 얻을 수 있으며, 클러스터 구조에 따라 복잡성이 증가할 수 있으므로 상황에 따라 적절한 구조를 선택하는 것이 필요합니다.
30+
![](https://i.imgur.com/435CqKW.png)
31+
(출처:[How to work with Cluster Mode on Amazon ElastiCache for Redis](https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/))
32+
![](https://i.imgur.com/cQ60lqb.png)aws article [How to work with Cluster Mode on Amazon ElastiCache for Redis](https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/)에서 다루고 있는 용어와 [redis.io 문서](https://redis.io/docs/latest/operate/oss_and_stack/management/sentinel/)에서 표현되는 용어가 미묘하게 다르지만, 위 이미지와 같이 비슷한 구조로 나누어 설명하고 있는것을 알 수 있습니다.
3333

34-
## Cluster Mode 종류
35-
> AWS ElastiCache. But the truth is, ElastiCache isn’t Redis. Instead, it’s a Redis-compatible imitator managed by AWS with limited extra functionality.
34+
## Redis Mode 별 차이 비교
35+
aws article에서 각 mode별 구성에 대해 잘 정리한 표가 있어서 가져왔습니다. 앞서 언급한 내용과 같이 각각의 모드는 redis.io에서 언급하고 있는 standalone, sentienl, cluster mode와 유사한 범주로 언급하고 있음을 알 수 있습니다.
36+
![](https://i.imgur.com/UKhYAt4.png)
37+
(출처:[How to work with Cluster Mode on Amazon ElastiCache for Redis](https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/))
3638

37-
(redis cloud 소개 관련 글에서 위와 같이 언급)[https://redis.io/cloud/compare-us-with-aws-elasticache/?utm_campaign=gg_s_competitor_bam_acq_apac-en&utm_source=google&utm_medium=cpc&utm_content=elasticache&utm_term=&gad_source=1&gclid=Cj0KCQjw1qO0BhDwARIsANfnkv9vMuzTeVvGvdHDSbmArIOkrxbHmuqfSoYLVemtJVpzkat8vF5lyNYaAtxzEALw_wcB]하며 차이를 소개하고 있지만 Cluster Mode의 경우 실제 운영 구조가 동일할 것이라고 생각되어 아래와 같이 가져왔습니다.
38-
![[Pasted image 20240706163022.png]]
39+
수평 확장을 통해 성능상의 이점 등을 얻기 위해선 결국 data partitioning을 통한 sharding이 되어야 하는데, redis의 경우 hashslot을 통해 파티셔닝을 처리하는 것을 아래 그림에서 설명하고 있습니다.
40+
![](https://i.imgur.com/PwcCqGW.png)
41+
(출처:[How to work with Cluster Mode on Amazon ElastiCache for Redis](https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/))
42+
43+
따라서 aws managed 서비스인 elastiCache를 사용하든 별도로 구성된 cluster mode를 사용하든 현재 구성된 클러스터 구조를 확인하고 이해하고 있어야 sharding을 통해 얻을수 있는 이점과, 관리 포인트를 이해하고 주의할 수 있을 것 입니다.
3944

4045
# redis-cli로 cluster mode 환경 살펴보기
41-
redis-cli를 설치하려면 환경에 맞게 (링크)[https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/] 에서 확인하고 설치해줍니다.
46+
redis-cli를 설치하려면 환경에 맞게 [링크](https://redis.io/docs/latest/operate/oss_and_stack/install/install-redis/) 에서 확인하고 설치해줍니다.
4247

4348
현재 실행된 환경을 확인하는 방법은 다음과 같습니다.
44-
4549
```sh
4650
# Connect to Redis server
4751
$ redis-cli -h 127.0.0.1 -p 6379
@@ -62,7 +66,26 @@ standalone 모드로 실행한 경우 아래와 같이 표시됩니다.
6266
2) "no"
6367
```
6468

65-
# Reference
69+
aws elastiCache로 redis cluster를 구성한 경우 아래와 같은 방법으로 정보를 확인할 수 있습니다.
70+
```
71+
xxx.cache.amazonaws.com:6379> CLUSTER INFO
72+
cluster_state:ok
73+
cluster_slots_assigned:16384
74+
cluster_slots_ok:16384
75+
cluster_slots_pfail:0
76+
cluster_slots_fail:0
77+
cluster_known_nodes:2
78+
cluster_size:1
79+
...
80+
```
81+
82+
```
83+
xxx.cache.amazonaws.com:6379> CLUSTER NODES
84+
{slave-node-id} 192.168.xxx.xxx:6379@1122 slave {master-node-id} 0 1720327133306 5 connected
85+
{master-node-id} 192.168.xxx.xxx:6379@1122 myself,master - 0 1700412779000 5 connected 0-16383
86+
```
87+
88+
# References
6689
- https://aws.amazon.com/ko/blogs/database/work-with-cluster-mode-on-amazon-elasticache-for-redis/
6790
- https://ot-container-kit.github.io/redis-operator/guide/setup.html?ref=breezymind.com#redis-cluster
68-
- https://www.digitalocean.com/community/tutorials/understanding-database-sharding
91+
- https://www.digitalocean.com/community/tutorials/understanding-database-sharding

_posts/2024/fragments/2024-07-06-sharding.md

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: redis - hashtag
3+
layout: single
4+
author_profile: false
5+
read_time: true
6+
share: true
7+
related: true
8+
categories:
9+
- Redis
10+
tags:
11+
- Redis
12+
- Key
13+
- Hashtag
14+
toc: false
15+
toc_stiky: true
16+
description: ""
17+
article_section: ""
18+
meta_keywords: ""
19+
completed: false
20+
visible:
21+
status:
22+
created: 2024-06-30T19:08
23+
last_modified_at: 2024-07-07T15:20:12+09:00
24+
thumnail_url:
25+
priority:
26+
---
27+
# TL;DR
28+
- redis cluster mode에서 MGET으로 데이터를 가져오려면, 저장할때 key에 hashtag인 `{}`를 포함해주어야 합니다.
29+
# Hashslot
30+
> CROSSSLOT Keys in request don't hash to the same slot
31+
32+
위와 같은 에러 메세지를 받았다면, cluster mode로 실행중인 redis에 [MGET](https://redis.io/docs/latest/commands/mget/)등으로 한번에 많은 요청을 받으려고 시도 할 때 발생합니다.
33+
[aws 질문글](https://repost.aws/knowledge-center/elasticache-crossslot-keys-error-redis) 중에 관련된 내용이 있는데, 문제를 해결하기 위해서 키 내에 hashslot 명시하는 기준인 hashtag `{}` 를 포함하면 됩니다.
34+
하지만, 기본적으로 cluster mode에서 hashslot이 저장되는 방식에 대한 이해를 바탕으로 hashslot을 설정하는 것이 좋습니다.
35+
36+
아래와 같은 key를 예시로 들어봅니다.
37+
```
38+
1. categories:movies:{1}:name
39+
2. categories:{movies}:1:name
40+
```
41+
cluster mode의 경우 hashslot을 n개의 shard로 나누어 관리하므로 같은 공간에 저장되는 것이 좋습니다. 담고자 하는 정보가 동일한 hashslot에 묶여 함께 저장되길 바라는 곳에 `{}`를 포함해야 합니다.
42+
43+
따라서, 특정 영화의 여러 속성을 한 번에 가져오는 경우가 더 일반적이라면 첫 번째 구조를 추천합니다.
44+
그러나 범주 내의 여러 영화를 한 번에 가져오는 경우가 더 일반적이라면 두 번째 구조를 사용할 수 있습니다.
45+
46+
# Refrences
47+
- https://redis.io/docs/latest/develop/use/keyspace/
48+
- https://repost.aws/knowledge-center/elasticache-crossslot-keys-error-redis
49+
- https://redis.io/docs/latest/operate/oss_and_stack/reference/cluster-spec/
50+
51+
52+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: redis - sharding
3+
layout: single
4+
author_profile: false
5+
read_time: true
6+
share: true
7+
related: true
8+
categories:
9+
- Redis
10+
tags:
11+
- Sharding
12+
toc: false
13+
toc_stiky: true
14+
description: ""
15+
article_section: ""
16+
meta_keywords: ""
17+
completed: false
18+
visible:
19+
status:
20+
created: 2024-06-30T19:08
21+
last_modified_at: 2024-07-07T15:25:19+09:00
22+
thumnail_url:
23+
priority:
24+
---
25+
# TL; DR
26+
Sharding은 수평분할과 관련된 아키텍쳐 패턴입니다.
27+
수평 확장과 단일 장애점(SPOF) 문제를 개선하기에 용이하지만, 그만큼 올바르게 동작하도록 구성하려면 많은 비용과 노력이 필요합니다.
28+
# What is Sharding?
29+
각 파티션이 동일한 스키마와 열을 갖지만 그 데이터인 행은 다르게 나누어 갖습니다.
30+
![](https://i.imgur.com/8nXGQga.png)
31+
위 그림이 이 내용을 쉽게 설명하고 있는데, Sharding은 Horizontal Partitions 방식으로 데이터를 나누어 저장하는 것을 의미합니다.
32+
33+
# Pros & Cons
34+
## Pros
35+
- Scale out에 용이합니다.
36+
- 큰 테이블을 여러개로 샤딩하면 쿼리 성능이 올라갈 수 있습니다.
37+
- 특정 Sharding된 영역에 문제 생긴다고 해도, 전체 서비스 장애가 발생하는 것을 막을 수 있습니다.
38+
## Cons
39+
- Sharding을 제대로 구현하고 적용하는데 어려움이 많습니다. 잘못 구성하게 되면 데이터 손실등이 발생할 수 있습니다.
40+
- 샤딩 알고리즘 등에 따라 샤드가 불균해질 수 있고 이를 관리해줘야 할 수 있습니다.
41+
- 샤딩된 구조를 단일 아키텍쳐로 돌리기 어렵습니다.
42+
43+
# Sharding Strategies
44+
### 키 기반 샤딩(Key-based Sharding)
45+
**설명**: 각 데이터 항목에 대해 특정 키(예: 해시 함수 결과)를 사용하여 샤드를 결정합니다.
46+
**특징**: 해시 함수에 의해 고르게 분포되도록 설계되어 있어, 모든 샤드에 걸쳐 데이터가 균등하게 분포됩니다.
47+
### 범위 기반 샤딩(Range-based Sharding)
48+
**설명**: 특정 범위의 값을 기반으로 데이터를 샤드에 분할합니다.
49+
**특징**: 연속적인 데이터 범위가 같은 샤드에 저장되므로, 특정 범위의 데이터를 조회할 때 효율적입니다.
50+
### 디렉토리 기반 샤딩(Directory-based Sharding)
51+
**설명**: 샤딩 디렉토리라는 매핑 테이블을 사용하여 데이터 항목이 어떤 샤드에 있는지 관리합니다.
52+
**특징**: 유연하지만 디렉토리 테이블을 관리하기 위한 오버헤드가 발생할 수 있습니다.
53+
54+
# References
55+
- https://www.digitalocean.com/community/tutorials/understanding-database-sharding

0 commit comments

Comments
 (0)