Skip to content

๐Ÿšดโ€โ™€๏ธRedis๋กœ ๊ฒŒ์ž„๋ฐฉ ๊ด€๋ฆฌ ์ตœ์ ํ™”ํ•˜๊ธฐ

student079 edited this page Dec 3, 2024 · 1 revision

๐ŸŽฏ ๋„์ž…: ์™œ Redis์ธ๊ฐ€?

๊ฒŒ์ž„๋ฐฉ ๊ด€๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ, ๊ฒ€์ƒ‰, ์ •๋ ฌ, ์ˆ˜์ • ์ž‘์—…์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š” ์˜์—ญ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ๋ฐฉ์‹์€ ์„ฑ๋Šฅ ์ €ํ•˜์™€ ๋ณต์žกํ•œ ์œ ์ง€๋ณด์ˆ˜ ๋ฌธ์ œ๋ฅผ ์ดˆ๋ž˜ํ–ˆ์ง€๋งŒ, Redis๋Š” ์ด๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Redis๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฒŒ์ž„๋ฐฉ ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์ ํ™”ํ•œ ๊ณผ์ •๊ณผ ์„ฑ๊ณผ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค. Redis์˜ Hash, Sorted Set, List ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ด ํšจ์œจ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ๋™์‹œ์— ํ™•๋ณดํ•œ ์‚ฌ๋ก€๋ฅผ ๋‹ค๋ฃน๋‹ˆ๋‹ค.


๐Ÿšฉ ๊ธฐ์กด ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ๋ฐฉ์‹๊ณผ ๋ฌธ์ œ์ 

1๏ธโƒฃ ๊ธฐ์กด ๊ตฌ์กฐ

  • ๋ฐ์ดํ„ฐ ์ €์žฅ: JSON ๋ฌธ์ž์—ด๋กœ Redis์˜ SET ์ž๋ฃŒ๊ตฌ์กฐ์— ์ €์žฅ.
  • ๊ฒ€์ƒ‰๊ณผ ์ •๋ ฌ: ๋ชจ๋“  ๋ฐ์ดํ„ฐ์˜ ๊ฒ€์ƒ‰, ์ •๋ ฌ ์ž‘์—…์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ ˆ๋ฒจ์—์„œ ์ˆ˜ํ–‰.

2๏ธโƒฃ ๋ฌธ์ œ์ 

  1. ํšจ์œจ์„ฑ ๋ถ€์กฑ
    • ๋ฐฉ ์ด๋ฆ„ ๊ฒ€์ƒ‰์ด๋‚˜ ํŠน์ • ์กฐ๊ฑด์˜ ๋ฐฉ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœํšŒ.
    • Redis์˜ ๊ณ ์† ํ‚ค-๊ฐ’ ๊ฒ€์ƒ‰ ์ด์ ์„ ํ™œ์šฉํ•˜์ง€ ๋ชปํ•จ.
  2. ๋™์‹œ์„ฑ ๊ด€๋ฆฌ ์–ด๋ ค์›€
    • JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ์ „์ฒด ๊ฐ์ฒด๋ฅผ ์ฝ๊ณ , ์ˆ˜์ • ํ›„ ๋‹ค์‹œ ์ €์žฅํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ๋‹ค์ˆ˜์˜ ์‚ฌ์šฉ์ž๊ฐ€ ๋™์‹œ ์ž‘์—… ์‹œ ์ถฉ๋Œ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Œ.
  3. ๋ณต์žกํ•œ ์ฝ”๋“œ
    • JSON ๋ฌธ์ž์—ด ํŒŒ์‹ฑ ๋ฐ ์žฌ์ €์žฅ ๊ณผ์ •์ด ๋ณต์žกํ•˜๊ณ , ์œ ์ง€๋ณด์ˆ˜ ๋น„์šฉ ์ฆ๊ฐ€.

๐Ÿ› ๏ธ Redis ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ ์ตœ์ ํ™”

Redis์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์ž‘์—… ์„ฑ๋Šฅ์„ ๋Œ€ํญ ๊ฐœ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.


1๏ธโƒฃ Hash: ๋ฐฉ ์ •๋ณด ์ €์žฅ

ํ™œ์šฉ ๋ฐฉ์‹

Redis์˜ Hash ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๊ฒŒ์ž„๋ฐฉ์˜ ์ฃผ์š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•„๋“œ-๊ฐ’ ์Œ์œผ๋กœ ์ €์žฅ.

๊ตฌ์กฐ ์˜ˆ์‹œ

Key: room:{roomId}
Value: {
  "roomId": "room1",
  "roomName": "๊ฒŒ์ž„๋ฐฉ123",
  "hostNickname": "host1",
  "players": "[{\\"playerNickname\\":\\"player1\\",\\"isReady\\":true,\\"isMuted\\":false}]",
  "status": "waiting"
}

์žฅ์ 

  • ํŠน์ • ํ•„๋“œ๋งŒ ์ฝ๊ฑฐ๋‚˜ ์ˆ˜์ • ๊ฐ€๋Šฅ (O(1)).
  • JSON ํŒŒ์‹ฑ ๋ฐ ์ง๋ ฌํ™” ๊ณผ์ •์ด ๋ถˆํ•„์š”.

2๏ธโƒฃ Sorted Set: ๋ฐฉ ์ด๋ฆ„ ๊ฒ€์ƒ‰

ํ™œ์šฉ ๋ฐฉ์‹

Redis์˜ Sorted Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•ด ๋ฐฉ ์ด๋ฆ„์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  Auto Complete๋ฅผ ๊ตฌํ˜„.

๊ตฌ์กฐ ์˜ˆ์‹œ

Key: room_names
Value: [
  {score: 0, value: "๊ฒŒ์ž„๋ฐฉ123"},
  {score: 0, value: "๊ฒŒ์ž„ํŒŒํ‹ฐ๋ฐฉ"},
  {score: 0, value: "ํด๋กœ๋ฐ”์˜ ๋ฐฉ"}
]

๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ZRANGEBYLEX ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠน์ • ํ‚ค์›Œ๋“œ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋ฐฉ ์ด๋ฆ„ ๊ฒ€์ƒ‰.

ZRANGEBYLEX room_names [๊ฒŒ์ž„] [๊ฒŒ์ž„\\xff]

์žฅ์ 

  • ๊ฒ€์ƒ‰ ์†๋„ O(log(N)).
  • Auto Complete์™€ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์นœํ™”์  ๊ธฐ๋Šฅ ์ง€์›.

3๏ธโƒฃ List: ๋ฐฉ ์ƒ์„ฑ ์ˆœ์„œ ๊ด€๋ฆฌ

ํ™œ์šฉ ๋ฐฉ์‹

Redis์˜ List ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ๋œ ๋ฐฉ ์ˆœ์„œ๋ฅผ ๊ด€๋ฆฌ.

๊ตฌ์กฐ ์˜ˆ์‹œ

Key: recent_rooms
Value: ["room1", "room2", "room3"]

์ž‘์—… ๋ฐฉ์‹

  • ์ƒˆ๋กœ์šด ๋ฐฉ ์ƒ์„ฑ ์‹œ RPUSH๋กœ roomId๋ฅผ ๋ฆฌ์ŠคํŠธ ๋งจ ๋’ค์— ์ถ”๊ฐ€.
  • LRANGE ๋ช…๋ น์–ด๋กœ ์ƒ์„ฑ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ ๋ชฉ๋ก ์กฐํšŒ.
RPUSH recent_rooms room1
RPUSH recent_rooms room2
LRANGE recent_rooms 0 10  # ์ตœ๊ทผ ์ƒ์„ฑ๋œ 10๊ฐœ์˜ ๋ฐฉ ์กฐํšŒ

์žฅ์ 

  • ์ƒ์„ฑ ์ˆœ์„œ ๊ธฐ๋ฐ˜ ์ •๋ ฌ ๋กœ์ง ์ œ๊ฑฐ.
  • ์ƒ์„ฑ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ ๋ชฉ๋ก์„ ๋น ๋ฅด๊ฒŒ ์กฐํšŒ ๊ฐ€๋Šฅ.

๐Ÿ“Š ์„ฑ๋Šฅ ๋น„๊ต: ๊ธฐ์กด ๋ฐฉ์‹ vs Redis ์ตœ์ ํ™”


๐Ÿ† ์ตœ์ ํ™” ๊ฒฐ๊ณผ

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ์˜ ์ฃผ์š” ์ด์ 

  1. Hash: ํ•„๋“œ ๋‹จ์œ„ ๋ฐ์ดํ„ฐ ์ €์žฅ๊ณผ ์ˆ˜์ •์œผ๋กœ ํšจ์œจ์  ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ.
  2. Sorted Set: ๋ฐฉ ์ด๋ฆ„ ๊ฒ€์ƒ‰ ์†๋„์™€ ์‚ฌ์šฉ์ž ๊ฒฝํ—˜ ๊ฐœ์„ .
  3. List: ๋ฐฉ ์ƒ์„ฑ ์ˆœ์„œ ๊ด€๋ฆฌ์˜ ๋‹จ์ˆœํ™”์™€ ์„ฑ๋Šฅ ๊ฐœ์„ .

Redis์˜ ๊ฐ•์ 

Redis๋ฅผ ํ™œ์šฉํ•œ ์ตœ์ ํ™”๋Š” ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ๋Œ€ํญ ๊ฐœ์„ ํ•˜๊ณ  ์„œ๋ฒ„ ๋ถ€ํ•˜๋ฅผ ์ค„์˜€์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ์‹ค์‹œ๊ฐ„ ๋ฐ˜์‘์„ฑ์ด ์ค‘์š”ํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ Redis๋Š” ์ตœ์ ์˜ ์„ ํƒ์ž„์„ ์ฆ๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ

  1. Redis Documentation - Hashes
  2. Redis Documentation - Sorted Sets
  3. Redis Documentation - Lists

Redis ์ตœ์ ํ™”์— ๋Œ€ํ•ด ๋” ๊ถ๊ธˆํ•œ ์ ์ด๋‚˜ ์˜๊ฒฌ์ด ์žˆ์œผ์‹œ๋‹ค๋ฉด ๊ณต์œ ํ•ด์ฃผ์„ธ์š”! ๐Ÿ˜Š

์•ˆ๋…•! ํด๋กœ๋ฐ”ํŒŒํŠธ๋ผ

ํŒ€ ๋ฌธํ™”

๐Ÿค Ground Rule

ํ˜‘์—… ๋ฐฉ์‹

๐Ÿ”’ Convention
๐ŸŒณ Git Branch ์ „๋žต

๊ฐœ๋ฐœ ๋ฌธ์„œ

๐Ÿ› ๏ธ AGT - Automatic Git & Github Tool
๐Ÿ“Š WebRTC Mesh โ€ ํŠธ๋ž˜ํ”ฝ ๊ณ„์‚ฐ
๐ŸŽข WebRTC Mesh - ํ—˜๋‚œํ•œ ์—ฌ์ •
๐Ÿ’ฌ WebRTC๋ฅผ ์•Œ์•„๋ณด์ž
๐Ÿ“ฎ SSE(Server Sent Events)
๐Ÿ“– SSE Pagination
โณ Socket ํ†ต์‹ ์—์„œ ๋น„๋™๊ธฐ ์ž‘์—… ์ˆœ์„œ ๋ณด์žฅ ๋ฐฉ๋ฒ•
๐Ÿ“ก Redis pub/sub๋ฅผ ํ™œ์šฉํ•œ SSE ์ ์šฉ๊ธฐ
๐Ÿ—๏ธ Naver Cloud Platform์„ ํ™œ์šฉํ•œ ๋ฐฐํฌ ์ „๋žต
โš”๏ธ๐Ÿš€ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ: ๋‹จ์ผ ์ธ์Šคํ„ด์Šค VS NKS
๐Ÿšดโ€โ™€๏ธ Redis๋กœ ๊ฒŒ์ž„๋ฐฉ ๊ด€๋ฆฌ ์ตœ์ ํ™”: ํšจ์œจ์ ์ธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์™€ ์„ฑ๋Šฅ ๊ฐœ์„ 

ํšŒ์˜๋ก

๐Ÿ“† ํšŒ์˜๋ก ์บ˜๋ฆฐ๋”
๐Ÿ–ค ๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ ํ…œํ”Œ๋ฆฟ
๐Ÿ–ค ํšŒ๊ณ  ํ…œํ”Œ๋ฆฟ

๋ฉ˜ํ† ๋ง

0๏ธโƒฃ 0์ฃผ์ฐจ ๋ฉ˜ํ† ๋ง ์ผ์ง€
1๏ธโƒฃ 1์ฃผ์ฐจ ๋ฉ˜ํ† ๋ง ์ผ์ง€
2๏ธโƒฃ 2์ฃผ์ฐจ ๋ฉ˜ํ† ๋ง ์ผ์ง€
3๏ธโƒฃ 3์ฃผ์ฐจ ๋ฉ˜ํ† ๋ง ์ผ์ง€
4๏ธโƒฃ 4์ฃผ์ฐจ ๋ฉ˜ํ† ๋ง ์ผ์ง€

๋ฐœํ‘œ

0๏ธโƒฃ 0์ฃผ์ฐจ ๋ฐœํ‘œ
1๏ธโƒฃ 1์ฃผ์ฐจ ๋ฐœํ‘œ
2๏ธโƒฃ 2์ฃผ์ฐจ ๋ฐœํ‘œ
3๏ธโƒฃ 3์ฃผ์ฐจ ๋ฐœํ‘œ
4๏ธโƒฃ 4์ฃผ์ฐจ ๋ฐœํ‘œ
5๏ธโƒฃ ์ตœ์ข… ๋ฐœํ‘œ

Clone this wiki locally