๐ŸŒฑ Infra/Redis(Valkey)

[Redis/Valkey ๊ธฐ์ดˆ #1] Object, Data Type ์ดํ•ดํ•˜๊ธฐ

mini_world 2024. 12. 7. 02:57
๋ชฉ์ฐจ ์ ‘๊ธฐ

 

๐Ÿ“Œ Redis Object ๊ฐœ์š”

 

Redis ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ €์žฅ๊ณต๊ฐ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” In-Memory Key-value ์Šคํ† ์–ด ์ด๋‹ค.

  • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋ฏ€๋กœ ๋น ๋ฅธ ์ฝ๊ธฐ/์“ฐ๊ธฐ๊ฐ€ ๊ฐ€๋Šฅ
  • ํ•˜์ง€๋งŒ ๋ฉ”๋ชจ๋ฆฌ๋Š” ์œ ํ•œํ•œ ์ž์›์ด๋ฏ€๋กœ ํšจ์œจ์ ์ธ ๊ด€๋ฆฌ๊ฐ€ ํ•„์ˆ˜์ 

๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก, Redis๋Š” ๋ฐ์ดํ„ฐ๋ฅผ Object๋กœ ์ •์˜ํ•˜๊ณ ์žˆ๋‹ค.

Redis์˜ ๊ณ ์„ฑ๋Šฅ๊ณผ ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์„ ๊ฐ€๋Šฅํ•˜๊ธฐ ์œ„ํ•ด Object๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์—ญํ• ์„ ํ•œ๋‹ค.

  • ๋ฐ์ดํ„ฐ ํ‘œํ˜„
    • ๋ชจ๋“  ํ‚ค-๊ฐ’ ์Œ์„ RedisObject๋กœ ํ†ต์ผํ•˜์—ฌ ํ‘œํ˜„
    • ๋‹ค์–‘ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…(String, List, Hash ๋“ฑ)์„ ํ•˜๋‚˜์˜ ๊ตฌ์กฐ๋กœ ๊ด€๋ฆฌ
  • ๋ฉ”๋ชจ๋ฆฌ ์ตœ์ ํ™”
    • ๋ฐ์ดํ„ฐ ํŠน์„ฑ์— ๋”ฐ๋ผ ์ตœ์ ์˜ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์ž๋™ ์„ ํƒ
    • ์ž‘์€ ๋ฐ์ดํ„ฐ๋Š” ์••์ถ•๋œ ํ˜•ํƒœ๋กœ ์ €์žฅ
    • ์ค‘๋ณต ๋ฐ์ดํ„ฐ๋Š” ์ฐธ์กฐ ๋ฐฉ์‹์œผ๋กœ ์ €์žฅ
  • ์„ฑ๋Šฅ ์ตœ์ ํ™”
    • ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ํŒจํ„ด ์ถ”์  (LRU)
    • ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ํšŒ์ˆ˜ ์ •์ฑ… ์ง€์›
    • ๋ฐ์ดํ„ฐ ํƒ€์ž…๋ณ„ ํŠนํ™”๋œ ์—ฐ์‚ฐ ์ง€์›
    • ๋ฉ”๋ชจ๋ฆฌ ๋‹จํŽธํ™” ๋ฐฉ์ง€

 

 


๐Ÿ“Œ  Redis Object ์™€ SDS (Simple Dynamic String)

 

โ–บ Object

Object๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์–ด๋–ป๊ฒŒ ์ €์žฅ๋ ์ง€์— ๋Œ€ํ•œ ์ •์˜์ด๋ฉฐ. ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

typedef struct redisObject {
    unsigned type:4;      // ๋ฐ์ดํ„ฐ ํƒ€์ž…(4๋น„ํŠธ)
    unsigned encoding:4;  // ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹(4๋น„ํŠธ)
    unsigned lru:24;     // LRU ์‹œ๊ฐ„์ •๋ณด (24๋น„ํŠธ)
    int refcount;        // ์ฐธ์กฐ์นด์šดํŠธ
    void *ptr;          // ์‹ค์ œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ
} robj;
  • ํ•„๋“œ ์„ค๋ช…
    • type: ์‹ค์ œ ๊ฐ’(0:STRING, 1:LIST, 2:SET, 3:ZSET, 4:HASH)
    • encoding: ์‹ค์ œ ์‚ฌ์šฉ๋˜๋Š” ์ธ์ฝ”๋”ฉ ๊ฐ’๋“ค (INT, EMBSTR, RAW, HASHTABLE, LISTPACK ๋“ฑ)
    • lru (Least Recently Used): LRU ์ •๋ณด, ์ฆ‰ ๋งˆ์ง€๋ง‰ ์ ‘๊ทผ ์‹œ๊ฐ„ ์ €์žฅ (๋ฉ”๋ชจ๋ฆฌ ๋ถ€์กฑ์‹œ ์˜ค๋ž˜๋œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ์— ์ด์šฉ)
    • refcount: ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์ฐธ์กฐ ์นด์šดํŒ… (0์ด ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ)
    • refcount: ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํšŸ์ˆ˜ (๋ฉ”๋ชจ๋ฆฌ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์นด์šดํ„ฐ, 0๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ, ๋ฐ์ดํ„ฐ ๊ณต์œ  ์‹œ ์ฆ๊ฐ€)
    • ptr: ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ด

 

โ–บ SDS (Simple Dynamic String)

SDS๋Š” Redis๊ฐ€ ๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด์ด๋‹ค.
๊ธธ์ด ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ ์žˆ์œผ๋ฉฐ, ๋ฒ„ํผ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ฉ”๋ชจ๋ฆฌ ๋‚จ์€ ๊ณต๊ฐ„์„ ์ €์žฅํ•˜๊ณ , ์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

struct sdshdr {
    uint32_t len;    // ๋ฒ„ํผ์— ์‚ฌ์šฉ๋œ ๊ธธ์ด
    uint32_t free;   // ๋‚จ์€ ๋ฒ„ํผ ๊ธธ์ด
    char buf[];      // ์‹ค์ œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ
}

Object ํ•„๋“œ ๋‚ด *ptr๋ถ€๋ถ„์—์„œ SDS ํ˜น์€ ์‹ค์ œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๋ฅดํ‚ค๊ฒŒ ๋œ๋‹ค.

RedisObject (robj)
     โ”‚
    โ””โ”€โ”€ ptr (pointer) โ”€โ”€→ SDS
                 โ”‚
                โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€→ ์‹ค์ œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ

์ฆ‰, ์–ด๋–ค ๋ฐ์ดํ„ฐํƒ€์ž…์ด๋‚˜ ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š๋ƒ์— ๋”ฐ๋ผ SDS๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„์ˆ˜๋„ ์žˆ๋‹ค.
Redis๋Š” ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ์ €์žฅ ๋ฐฉ์‹์„ ์ž๋™์œผ๋กœ ์„ ํƒํ•œ๋‹ค.

###################################
# 1. ์ •์ˆ˜ (int)
# RedisObject
#     type: STRING
#     encoding: INT
#     ptr โ”€โ”€→ ์ง์ ‘ ์ •์ˆ˜๊ฐ’ ์ €์žฅ
127.0.0.1:6379> SET number 123
127.0.0.1:6379> OBJECT ENCODING number
"int"         // SDS ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ, ์ง์ ‘ ์ •์ˆ˜ ์ €์žฅ

###################################
# 2. ์ž‘์€ ๋ฌธ์ž์—ด (embstr)
# [RedisObject + SDS] (์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ)
127.0.0.1:6379> SET name "john"
127.0.0.1:6379> OBJECT ENCODING name
"embstr"      // RedisObject์™€ SDS๊ฐ€ ์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น

###################################
# 3. ํฐ ๋ฌธ์ž์—ด (raw)
# RedisObject → SDS
127.0.0.1:6379> SET bigtext "very...long...text..."
127.0.0.1:6379> OBJECT ENCODING bigtext
"raw"         // ์ผ๋ฐ˜์ ์ธ SDS ์‚ฌ์šฉ

###################################
# 4. ๋ฆฌ์ŠคํŠธ
# RedisObject → ์ž๋ฃŒ๊ตฌ์กฐ → SDS๋“ค
127.0.0.1:6379> RPUSH mylist "a" "b" "c"
127.0.0.1:6379> OBJECT ENCODING mylist
"quicklist"    // ๋‚ด๋ถ€์ ์œผ๋กœ SDS ์‚ฌ์šฉ

###################################
# 5.Hash (listpack/hashtable)
# RedisObject
#     type: HASH
#     encoding: HASHTABLE
#     ptr โ”€โ”€→ HashTable โ”€โ”€→ {
#         key1: SDS1,
#         key2: SDS2,
#         ...
#     }
127.0.0.1:6379> HSET user name "Tom" age "25"
127.0.0.1:6379> OBJECT ENCODING user
"listpack"     // ์ž‘์€ ๊ฒฝ์šฐ listpack, ํฐ ๊ฒฝ์šฐ hashtable

 

โ–บ Object, SDS, Datatype, Encoding ๊ด€๊ณ„

์ค‘์š” ์ปดํฌ๋„ŒํŠธ๋“ค์€ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง„๋‹ค.

RedisObject → SDS
    ↓
Data Types (String, List, Hash, Set, Sorted Set)
    ↓
Encoding (Raw, Int, Embstr, Ziplist, Quicklist...)

๋น„์œ ๋ฅผ ํ•˜์ž๋ฉด, Redis๊ฐ€ ๋Œ€ํ˜• ์„ ๋ฐ•์ด๋ผ๋ฉด, Object๋Š” ์šฉ๋„๋ณ„๋กœ ๊ตฌ๋ถ„๋œ ๊ณต๊ฐ„, SDS๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์€ ๋ฐ•์Šค๋ผ๊ณ  ๋ณผ์ˆ˜์žˆ๋‹ค.
๊ตฌํš(Object)์€ ์ปจํ…Œ์ด๋„ˆ(SDS)๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐฐ์น˜ํ•˜๊ณ  ๊ด€๋ฆฌํ• ์ง€ ๊ฒฐ์ •ํ•˜๊ณ ,
์ปจํ…Œ์ด๋„ˆ(SDS)๋Š” ์‹ค์ œ ํ™”๋ฌผ(๋ฐ์ดํ„ฐ)์„ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณด๊ด€ํ•œ๋‹ค.

๋Œ€ํ˜• ์„ ๋ฐ• (Redis)
โ”‚
โ”œโ”€โ”€ ๊ตฌํš (RedisObject)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ์ผ๋ฐ˜ ๋ฌผํ’ˆ ๋ณด๊ด€์†Œ (String Type)
โ”‚   โ”‚   โ”œโ”€โ”€ ์ปจํ…Œ์ด๋„ˆ๋“ค (SDS - raw/embstr)
โ”‚   โ”‚   โ””โ”€โ”€ ๋ฐ”๋‹ฅ์— ๋†“์ธ ์ž‘์€ ๋ฌผํ’ˆ๋“ค (์ •์ˆ˜ - int)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ์ปจํ…Œ์ด๋„ˆ ์ •๋ ฌ์†Œ (List Type)
โ”‚   โ”‚   โ””โ”€โ”€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ๋œ ์ปจํ…Œ์ด๋„ˆ๋“ค (QuickList → SDS๋“ค)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ์ปจํ…Œ์ด๋„ˆ ๋ถ„๋ฅ˜์†Œ (Hash Type)
โ”‚   โ”‚   โ””โ”€โ”€ ๋ผ๋ฒจ๋ง๋œ ์ปจํ…Œ์ด๋„ˆ๋“ค (field-SDS, value-SDS ์Œ)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ์ปจํ…Œ์ด๋„ˆ ์ค‘๋ณต์ œ๊ฑฐ์†Œ (Set Type)
โ”‚   โ”‚   โ””โ”€โ”€ ์œ ๋‹ˆํฌํ•œ ์ปจํ…Œ์ด๋„ˆ๋“ค (SDS๋“ค)
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ ์ปจํ…Œ์ด๋„ˆ ์šฐ์„ ์ˆœ์œ„์†Œ (Sorted Set Type)
โ”‚       โ””โ”€โ”€ ์ ์ˆ˜๊ฐ€ ๋งค๊ฒจ์ง„ ์ปจํ…Œ์ด๋„ˆ๋“ค (score-SDS ์Œ)

 

 


๐Ÿ“Œ  Redis ์‹ค์Šต ์ค€๋น„ํ•˜๊ธฐ

๋จผ์ € ์ดํ•ดํ•˜๋ฉด์„œ ์‹ค์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก Redis๋ฅผ ์ค€๋น„ํ•ด๋†“์ž ๐Ÿ’ช๐Ÿ’ช

# Redis ์ตœ์‹ ๋ฒ„์ „ ์‹คํ–‰
docker run --name redis -p 6379:6379 -d redis

# Redis CLI ์ ‘์†
docker exec -it redis redis-cli

 

 


๐Ÿ“Œ  Redis Data Type

 

โ–บ ๋ฐ์ดํ„ฐํƒ€์ž… ์ดํ•ดํ•˜๊ธฐ

๋ฐ์ดํ„ฐ ํƒ€์ž…์€ Object ๋‚ด์—์„œ type์œผ๋กœ ์ •์˜๋˜๋ฉฐ (unsigned type:4;) ์ˆซ์ž๋กœ ํ‘œํ˜„๋œ๋‹ค.
4๋น„ํŠธ๋กœ ํ‘œํ˜„๋˜๋ฏ€๋กœ ์ตœ๋Œ€ 16๊ฐ€์ง€ ํƒ€์ž…์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

define OBJ_STRING 0    // ๋ฌธ์ž์—ด
define OBJ_LIST 1      // ๋ฆฌ์ŠคํŠธ
define OBJ_SET 2       // ์…‹
define OBJ_ZSET 3      // ์ •๋ ฌ๋œ ์…‹
define OBJ_HASH 4      // ํ•ด์‹œ
define OBJ_STREAM 6    // ์ŠคํŠธ๋ฆผ

 

Redis Data Type์€ ๊ธฐ๋ณธ(Core) ๋ฐ์ดํ„ฐ ํƒ€์ž…๊ณผ, ํŠน์ˆ˜ ๋ฐ์ดํ„ฐํƒ€์ž…์ด ์กด์žฌํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ•  ์ ์€ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค๋งŒ RedisObject์˜ 4๋น„ํŠธ typeํ•„๋“œ๋กœ ํ‘œํ˜„๋œ๋‹ค๋Š”๊ฒƒ์ด๋‹ค.

ํŠน์ˆ˜๋ฐ์ดํ„ฐ ํƒ€์ž… ์˜ˆ์‹œ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํƒ€์ž…์ด ์žˆ๊ณ , ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ํ†ตํ•ด ํ™•์žฅ ๋˜๊ฑฐ๋‚˜ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋œ๋‹ค. 

  • Bitmap: ์‹ค์ œ๋กœ๋Š” STRING ํƒ€์ž…์˜ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ
  • Geospatial: ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ZSET ์‚ฌ์šฉ
  • HyperLogLog: STRING ํƒ€์ž…์œผ๋กœ ๊ตฌํ˜„
  • Bloom filter: STRING ํƒ€์ž… ๊ธฐ๋ฐ˜
  • Time series: ํŠน๋ณ„ํ•œ ๋ชจ๋“ˆ๋กœ ๊ตฌํ˜„

์˜ˆ๋ฅผ๋“ค์ž๋ฉด, ์•„๋ž˜์ฒ˜๋Ÿผ ๊ตฌํ˜„๋œ๋‹ค.

# Bitmap์€ ์‹ค์ œ๋กœ๋Š” STRING
SETBIT mykey 7 1   # ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” STRING ํƒ€์ž…

# Geospatial์€ ZSET ์‚ฌ์šฉ
GEOADD locations 13.361389 38.115556 "Palermo"  # ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” ZSET

# HyperLogLog๋„ STRING
PFADD visitors "user1"  # ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” STRING

 

Redis์—์„œ๋Š” ๋ฐ์ดํ„ฐํƒ€์ž…์„ ์ง์ ‘ ์ง€์ •ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๊ฒฐ์ •๋œ๋‹ค.
๋ช…๋ น์–ด์˜ ์ฒซ ๊ธ€์ž๋กœ ์–ด๋–ค ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋‹ค๋ฃจ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜์—์„œ ๋‹ค๋ฃฌ๋‹ค.

  • String: ์ ‘๋‘์–ด ์—†์Œ (SET, GET)
  • List: L ๋˜๋Š” R
  • Set: S
  • Sorted Set: Z
  • Hash: H
  • Stream: X

๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ™•์ธํ•˜๋ ค๋ฉด ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

# ํƒ€์ž… ํ™•์ธ ๋ช…๋ น์–ด
TYPE key๋ช…

 

 

โ–บ Core Data type

 

1. Strings (์ŠคํŠธ๋ง/๋ฌธ์ž์—ด)

- https://redis.io/docs/latest/develop/data-types/strings/

๋ฌธ์ž์—ด ์œ ํ˜•์„ ๊ฐ’์œผ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค. (๊ฐ’์€ ์ตœ๋Œ€ 512MB, ํฐ๋ฌธ์ž์—ด์€ ๋ฉ”๋ชจ๋ฆฌ ํŒŒํŽธํ™” ์ฃผ์˜)
์ผ๋ฐ˜ ๋ฌธ์ž์—ด ๋ฟ ๋งŒ ์•„๋‹ˆ๋ผ ์ˆซ์ž(์ •์ˆ˜/์‹ค์ˆ˜), JSON๋ฌธ์ž์—ด, ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋„ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
๊ฐ€์žฅ ๋งŽ์ด ํ™œ์šฉ๋˜๋Š” ์œ ํ˜•์œผ๋กœ, Redis๋กœ ๊ฐ€์žฅ ๋งŽ์ด ๊ตฌํ˜„๋˜๋Š” ์บ์‹œ/์„ธ์…˜์œผ๋กœ ํ™œ์šฉํ•œ๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
SET key value      # ๋ฌธ์ž์—ด ์ €์žฅ
SETNX                  # ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„๋•Œ๋งŒ ๋ฌธ์ž์—ด ์ €์žฅ (SET if Not eXists"์˜ ์•ฝ์ž, Lock๊ตฌํ˜„์— ์“ฐ์ž„)
GET key               # ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ์บ์‹œ (์›นํŽ˜์ด์ง€, API์‘๋‹ต, SQL์ฟผ๋ฆฌ ์บ์‹ฑ)
    • ์„ธ์…˜์ €์žฅ
    • ์นด์šดํ„ฐ (์กฐํšŒ์ˆ˜, ์ข‹์•„์š” ์ˆ˜)
    • ๋ฝ(Lock) ๊ตฌํ˜„
    • API ๋ ˆ์ดํŠธ ๋ฏธํ„ฐ๋ง

๊ฐ€์žฅ ๋„๋ฆฌ ์‚ฌ์šฉ๋˜๋Š” ์ฟผ๋ฆฌ ์บ์‹ฑ์€ ์ฟผ๋ฆฌ๋ฅผ Hashingํ•˜์—ฌ Key๋กœ ์ €์žฅํ•˜๊ณ , ๊ฐ’์„ Value๋กœ ์ €์žฅํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค.
์ฆ‰, ์ฟผ๋ฆฌ ์ž์ฒด๋ฅผ Hashํ•ด์„œ ๊ฐ™์€ ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Hash๊ฐ’์„ ์กฐํšŒํ•ด์„œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋„๋ก ํ•œ๋‹ค.
 Key = Hash(SQL)
 Value = ์ฟผ๋ฆฌ๊ฒฐ๊ณผ
์ด๊ฑด ๋‚˜์ค‘์— ๊ธฐํšŒ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋‹ค์‹œํ•œ๋ฒˆ ๋‹ค๋ฃจ๋„๋ก ํ•œ๋‹ค.

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๊ธฐ๋ณธ์ ์ธ GET/GET ํ™œ์šฉ
127.0.0.1:6379> set name "user01"
OK
127.0.0.1:6379> get name
"user01"

# ๊ฐ’ ํ™•์ธ
127.0.0.1:6379> EXISTS name
(integer) 1

# ํƒ€์ž… ์ ๊ฒ€
127.0.0.1:6379> type name
string

# SETNX ํ™œ์šฉ
127.0.0.1:6379> SETNX mykey "Hello"
(integer) 1     # ๋ฐ˜ํ™˜๊ฐ’: 1 (mykey๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์•„์„œ ์„ค์ •๋จ)
127.0.0.1:6379> SETNX mykey "World"
(integer) 0     # ๋ฐ˜ํ™˜๊ฐ’: 0 (mykey๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•จ)
127.0.0.1:6379> GET mykey
"Hello"


# ๊ฐ’์€ JSON์œผ๋กœ, TTL ์ถ”๊ฐ€
127.0.0.1:6379> SET session:user01 "{\"user_id\":user01,\"username\":\"lina\",\"login_time\":\"2024-12-07T10:00:00Z\"}" EX 100
OK

# TTL ํ™•์ธ
127.0.0.1:6379> TTL session:user01
(integer) 94

# TTL ์—ฐ์žฅ
127.0.0.1:6379> EXPIRE session:user01 3600
(integer) 1
127.0.0.1:6379> TTL session:user01
(integer) 3598

# ๊ฐ’ ์‚ญ์ œ
127.0.0.1:6379> DEL session:user01
(integer) 1
127.0.0.1:6379> GET session:user01
(nil)

 

 

2. Lists (๋ฆฌ์ŠคํŠธ)

- https://redis.io/docs/latest/develop/data-types/lists/

List๋Š” ๋ฌธ์ž์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์–ด ์–‘์ชฝ ๋์—์„œ Push/Pop ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
LPUSH key value    # ์™ผ์ชฝ(head)์— ์ถ”๊ฐ€
RPUSH key value    # ์˜ค๋ฅธ์ชฝ(tail)์— ์ถ”๊ฐ€
LPOP key                 # ์™ผ์ชฝ์—์„œ ์ œ๊ฑฐ ๋ฐ ๋ฐ˜ํ™˜
RPOP key                # ์˜ค๋ฅธ์ชฝ์—์„œ ์ œ๊ฑฐ ๋ฐ ๋ฐ˜ํ™˜
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ๋ฉ”์„ธ์ง€ ํ, ์ž‘์—… ๋Œ€๊ธฐ์—ด
    • ์ตœ๊ทผ ๊ฒŒ์‹œ๋ฌผ ๋ชฉ๋ก
    • ์†Œ์…œ ๋ฏธ๋””์–ด ํƒ€์ž„๋ผ์ธ

Lists๋Š” FIFO(First in, First Out) ํ ์‹œ์Šคํ…œ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฉ”์„ธ์ง€ ํ๋กœ ํ™œ์šฉํ• ๋•Œ๋Š” ๋ธ”๋กœํ‚นํŒ(BLPOP)์„ ํ™œ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹์œผ๋ฉฐ, ์ด๋•Œ๋Š” ๋ณ„๋„๋กœ ๊ณต๋ถ€ํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

 

โš ๏ธ์ฃผ์˜์‚ฌํ•ญโš ๏ธ

Redis๋Š” ๋ฐ์ดํ„ฐ์˜ ๋‚ด๊ตฌ์„ฑ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ(์บ์‹ฑ ํŠนํ™”), ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ์Œ์„ ์ธ์ง€ํ•ด์•ผํ•œ๋‹ค.
Redis๋Š” In-memory ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๊ณ , ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ์„œ๋ฒ„๊ฐ€ ์žฌ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒ ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค๋กœ์„œ์˜ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ(์žฌ์‹œ๋„ ๋“ฑ)๋„ ๋ถ€์กฑํ•˜๋‹ค.
๋”ฐ๋ผ์„œ, Redis๋Š” ๋น ๋ฅธ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ํ•„์š”ํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์†์‹ค์ด ํฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋ฉฐ, ๋‚ด๊ตฌ์„ฑ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” Kafka์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์†”๋ฃจ์…˜์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
Redis Database Backup(RDB), Append Only File(AOF) ์„ค์ •์„ ์ˆ˜์ • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ๋””์Šคํฌ์— ์“ฐ๋„๋ก ๊ตฌ์„ฑํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ,
RDB์„ค์ •์€ ๋งˆ์ง€๋ง‰ ์Šค๋ƒ…์ƒท ์ดํ›„์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ณ , AOF๋Š” appendfsync always๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  appendfsync everysec๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 1์ดˆ๋งˆ๋‹ค ์ €์žฅํ•˜๊ฒŒ ์„ค์ •ํ•˜๋”๋ผ๋„ ์ตœ๋Œ€ 1์ดˆ์˜ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
์ฆ‰, ์•„๋ฌด๋ฆฌ ์„ค์ •ํ•ด๋„ ์ „ํ†ต์ ์ธ RDBMS์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ ๋‚ด๊ตฌ์„ฑ(Durability)์„ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
127.0.0.1:6379> RPUSH numbers 1 2 3 4 5
(integer) 5

# 1. ์ „์ฒด ๋ฒ”์œ„ ์กฐํšŒ
127.0.0.1:6379> LRANGE numbers 0 -1 (0์€ ์‹œ์ž‘์ธ๋ฑ์Šค, -1์€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋œปํ•œ๋‹ค)
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

# 2. ์ฒ˜์Œ 3๊ฐœ๋งŒ ์กฐํšŒ (0,1,2 ์ธ๋ฑ์Šค)
127.0.0.1:6379> LRANGE numbers 0 2
1) "1"
2) "2"
3) "3"

# 3. 2๋ฒˆ์งธ๋ถ€ํ„ฐ 4๋ฒˆ์งธ๊นŒ์ง€ ์กฐํšŒ
127.0.0.1:6379> LRANGE numbers 1 3
1) "2"
2) "3"
3) "4"

# 4. ๋งˆ์ง€๋ง‰ 2๊ฐœ ์กฐํšŒ (์Œ์ˆ˜ ์ธ๋ฑ์Šค ์‚ฌ์šฉ)
127.0.0.1:6379> LRANGE numbers -2 -1
1) "4"
2) "5"

# ์˜ค๋ฅธ์ชฝ์— ๊ฐ’ ์ถ”๊ฐ€
127.0.0.1:6379> RPUSH numbers 6
(integer) 6
127.0.0.1:6379> LRANGE numbers 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"

# ์™ผ์ชฝ์— ๊ฐ’ ์ถ”๊ฐ€
127.0.0.1:6379> LPUSH numbers 7
(integer) 7
127.0.0.1:6379> LRANGE numbers 0 -1
1) "7"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"

# ๋ฆฌ์ŠคํŠธ ๊ธธ์ด ํ™•์ธ
127.0.0.1:6379> LLEN numbers
(integer) 7

# ํŠน์ • ์ธ๋ฑ์Šค ๊ฐ’ ์กฐํšŒ
127.0.0.1:6379> LINDEX numbers 3
"3"

# ์˜ค๋ฅธ์ชฝ์— ๊ฐ’ ์ œ๊ฑฐ
127.0.0.1:6379> RPOP numbers
"6"
127.0.0.1:6379> LRANGE numbers 0 -1
1) "7"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"

# ์™ผ์ชฝ์— ๊ฐ’ ์ œ๊ฑฐ
127.0.0.1:6379> LPOP numbers
"7"
127.0.0.1:6379> LRANGE numbers 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"

# ํŠน์ • ๋ฒ”์œ„๋งŒ ๋‚จ๊ธฐ๊ณ  ์‚ญ์ œ
127.0.0.1:6379> LTRIM numbers 2 3
OK
127.0.0.1:6379> LRANGE numbers 0 -1
1) "3"
2) "4"

 

 

3. Sets (์…‹)

- https://redis.io/docs/latest/develop/data-types/sets/

Sets์€ ์ˆœ์„œ๊ฐ€ ์—†๋Š” ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋ฉฐ, ์ค‘๋ณต๋ฐ์ดํ„ฐ๋ฅผ ํ—ˆ์šฉํ•˜์ง€์•Š๋Š”๋‹ค.
๋˜ํ•œ, Sets๋Š” ์ง‘ํ•ฉ ์—ฐ์‚ฐ(๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ ๋“ฑ)์„ ์ง€์›ํ•˜์—ฌ, ๋ฐ์ดํ„ฐ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์‰ฝ๊ฒŒ ๋ถ„์„ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ, ์ˆœ์„œ๋Š” ์ค‘์š”ํ•˜์ง€ ์•Š์ง€๋งŒ ์ค‘๋ณต๋˜๋ฉด ์•ˆ๋˜๋Š” ๋ฐ์ดํ„ฐ๋“ค์„ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ์ผ๋ฐ˜์ ์ธ ์ง‘ํ•ฉ์—ฐ์‚ฐ(๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ)์— ์ ํ•ฉํ•˜๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
SADD key member          # ์„ธํŠธ์— ์ƒˆ๋กœ์šด ๋ฉค๋ฒ„๋ฅผ ์ถ”๊ฐ€
SREM key member          # ์ง€์ •๋œ ๋ฉค๋ฒ„๋ฅผ ์ง‘ํ•ฉ์—์„œ ์ œ๊ฑฐ
SISMEMBER key member     # ๋ฌธ์ž์—ด์˜ ์ง‘ํ•ฉ ๋ฉค๋ฒ„์‹ญ ํ…Œ์ŠคํŠธ
SINTER key1 key2 ...        # ๋‘ ๊ฐœ ์ด์ƒ์˜ ์ง‘ํ•ฉ์ด ๊ณตํ†ต์ ์œผ๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉค๋ฒ„ ์ง‘ํ•ฉ(์ฆ‰, ๊ต์ง‘ํ•ฉ) ๋ฐ˜ํ™˜
SCARD key                       # ์ง‘ํ•ฉ์˜ ํฌ๊ธฐ(์ผ๋ช… ์นด๋””๋„๋ฆฌํ‹ฐ) ๋ฐ˜ํ™˜
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ํŠน์ • ๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€์— ์•ก์„ธ์Šค ํ•˜๋Š” IP๋ฆฌ์ŠคํŠธ
    • ํƒœ๊ทธ ์‹œ์Šคํ…œ (ํŠน์ • ํƒœ๊ทธ๋กœ ์กฐํšŒ ํ•ด์•ผํ• ๋•Œ ์ง‘ํ•ฉ์—ฐ์‚ฐ)
    • ํŒ”๋กœ์šฐ/ํŒ”๋กœ์›Œ ์‹œ์Šคํ…œ

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ์„ธํŠธ์— ์ƒˆ๋กœ์šด ๊ฐ’ ์ €์žฅ
127.0.0.1:6379> SADD myset "apple"
(integer) 1
127.0.0.1:6379> SADD myset "banana"
(integer) 1
127.0.0.1:6379> SADD myset "cherry"
(integer) 1

# ์„ธํŠธ ๋ฐ์ดํ„ฐ ์กฐํšŒ
127.0.0.1:6379> SMEMBERS myset
1) "apple"
2) "banana"
3) "cherry"

# ์„ธํŠธ์— ๋ฐ์ดํ„ฐ ์ œ๊ฑฐ
127.0.0.1:6379> SREM myset "banana"
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "apple"
2) "cherry"

# ์„ธํŠธ ์‚ญ์ œ
127.0.0.1:6379> DEL myset
(integer) 1
127.0.0.1:6379> SMEMBERS myset
(empty array)

# ๊ต์ง‘ํ•ฉ/ํ•ฉ์ง‘ํ•ฉ/์ฐจ์ง‘ํ•ฉ
127.0.0.1:6379> SADD set1 "apple" "banana" "cherry"
(integer) 3
127.0.0.1:6379> SADD set2 "banana" "cherry" "melon"
(integer) 3
127.0.0.1:6379> SINTER set1 set2 # ๊ต์ง‘ํ•ฉ (SINTER)
1) "banana"
2) "cherry"
127.0.0.1:6379> SUNION set1 set2 # ํ•ฉ์ง‘ํ•ฉ (SUNION)
1) "apple"
2) "banana"
3) "cherry"
4) "melon"
127.0.0.1:6379> SDIFF set1 set2 # ์ฐจ์ง‘ํ•ฉ (SDIFF)
1) "apple"
127.0.0.1:6379> SDIFF set2 set1 # ์ฐจ์ง‘ํ•ฉ (SDIFF)
1) "melon"

 

 

4. Sorted sets (์†Œํ‹ฐ๋“œ ์…‹)

- https://redis.io/docs/latest/develop/data-types/sorted-sets/ 

Sorted sets์€ Sets๊ณผ ๋น„์Šทํ•œ ํŠน์ง•์„ ๊ฐ€์ง€์ง€๋งŒ ๊ฐ ๋งด๋ฒ„๊ฐ€ Score๋ฅผ ๊ฐ€์ง€๋ฉฐ, Score๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
์ฆ‰, ์ค‘๋ณต์ œ๊ฑฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ , ์ง‘ํ•ฉ ์—ฐ์‚ฐ์— ํŠนํ™”๋˜์–ด ์žˆ์œผ๋ฉด์„œ๋„, Score๋ฅผ ๊ฐ€์ง€๋Š” ํŠน์ง•์„ ๊ฐ€์ง„๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
ZADD key score member       # ์ •๋ ฌ๋œ ์…‹์— ๋ฉค๋ฒ„์™€ ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€
ZREM key member                 # ์ง€์ •๋œ ๋ฉค๋ฒ„๋ฅผ ์ •๋ ฌ๋œ ์…‹์—์„œ ์ œ๊ฑฐ
ZSCORE key member             # ํŠน์ • ๋ฉค๋ฒ„์˜ ์ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
ZRANGE key start stop          # ์ง€์ •๋œ ๋ฒ”์œ„์˜ ๋ฉค๋ฒ„๋ฅผ ๋ฐ˜ํ™˜ (์ ์ˆ˜ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ)
ZREVRANGE key start stop    # ์ง€์ •๋œ ๋ฒ”์œ„์˜ ๋ฉค๋ฒ„๋ฅผ ๋ฐ˜ํ™˜ (์ ์ˆ˜ ๊ธฐ์ค€ ๋‚ด๋ฆผ์ฐจ์ˆœ)
ZRANK key member               # ํŠน์ • ๋ฉค๋ฒ„์˜ ์ˆœ์œ„๋ฅผ ๋ฐ˜ํ™˜ (์˜ค๋ฆ„์ฐจ์ˆœ)
ZCARD key                              # ์ •๋ ฌ๋œ ์…‹์˜ ํฌ๊ธฐ(์ผ๋ช… ์นด๋””๋„๋ฆฌํ‹ฐ) ๋ฐ˜ํ™˜
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ๋ฆฌ๋”๋ณด๋“œ (์ ์ˆ˜์— ๋”ฐ๋ผ ์ˆœ์œ„๊ฐ€ ๋งค๊ฒจ์ง€๋Š” ์‹œ์Šคํ…œ)
    • ์šฐ์„ ์ˆœ์œ„ ํ
    • ํƒ€์ž„์Šคํƒฌํ”„ ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ ์ •๋ ฌ

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ์„ธํŠธ์— ์ƒˆ๋กœ์šด ๊ฐ’(๋ฉค๋ฒ„) ์ €์žฅ
127.0.0.1:6379> ZADD leaderboard 100 "Alice"
(integer) 1
127.0.0.1:6379> ZADD leaderboard 72 "Bob"
(integer) 1
127.0.0.1:6379> ZADD leaderboard 88 "Charlie"
(integer) 1

# ๊ฐ’(๋งด๋ฒ„) ์กฐํšŒ
127.0.0.1:6379> ZRANGE leaderboard 0 -1  # ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ „์ฒด ๋ฒ”์œ„ ์กฐํšŒ
1) "Bob"
2) "Charlie"
3) "Alice"
127.0.0.1:6379> ZREVRANGE leaderboard 0 -1  # ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ „์ฒด ๋ฒ”์œ„ ์กฐํšŒ
1) "Alice"
2) "Charlie"
3) "Bob"

# ํŠน์ • ๋งด๋ฒ„์˜ ๊ฐ’ ๋ฐ˜ํ™˜
127.0.0.1:6379> ZSCORE leaderboard "Bob"
"72"

# ํŠน์ • ๋งด๋ฒ„์˜ ์ˆœ์œ„ ๋ฐ˜ํ™˜
127.0.0.1:6379> ZRANK leaderboard "Charlie"
(integer) 1

# ์ •๋ ฌ๋œ ์…‹์˜ ํฌ๊ธฐ ๋ฐ˜ํ™˜
127.0.0.1:6379> ZCARD leaderboard
(integer) 3

# ๋งด๋ฒ„์™€ ์ ์ˆ˜ ํ•จ๊ป˜ ์กฐํšŒ
127.0.0.1:6379>  ZRANGE leaderboard 0 -1 WITHSCORES # ์˜ค๋ฆ„์ฐจ์ˆœ
1) "Bob"
2) "72"
3) "Charlie"
4) "88"
5) "Alice"
6) "100"
127.0.0.1:6379> ZREVRANGE leaderboard 0 -1 WITHSCORES # ๋‚ด๋ฆผ์ฐจ์ˆœ
1) "Alice"
2) "100"
3) "Charlie"
4) "88"
5) "Bob"
6) "72"

# ๋งด๋ฒ„ ์ œ๊ฑฐ
127.0.0.1:6379> ZREM leaderboard "Alice"
(integer) 1
127.0.0.1:6379> ZRANGE leaderboard 0 -1 WITHSCORES
1) "Bob"
2) "72"
3) "Charlie"
4) "88"

 

 

 

5. Hashes (ํ•ด์‹œ)

- https://redis.io/docs/latest/develop/data-types/hashes/

Hash๋Š” ๊ฐ’์— field-value ์Œ์„ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
์œ„ String ํƒ€์ž…์—์„œ๋„ JSON์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ, Hash์˜ ๊ฒฝ์šฐ ๊ฐœ๋ณ„ํ•„๋“œ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์—ฌ ๋ถ€๋ถ„ ์—…๋ฐ์ดํŠธํ•˜๋Š”๋ฐ ๋” ์šฉ์ดํ•˜๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
HSET key field value                  # ํ•˜๋‚˜ ์ด์ƒ์˜ ํ•„๋“œ์— ๊ฐ’ ์„ค์ •
HGET key field                            # ์ฃผ์–ด์ง„ ํ•„๋“œ์˜ ๊ฐ’ ๋ฐ˜ํ™˜
HMGET key field1 field2 ...        # ์—ฌ๋Ÿฌ ํ•„๋“œ ํ•œ๋ฒˆ์— ์กฐํšŒ
HINCRBY key field increment   # ์ฃผ์–ด์ง„ ํ•„๋“œ์˜ ๊ฐ’์„ ์ œ๊ณต๋œ ์ •์ˆ˜๋งŒํผ ์ฆ๊ฐ€
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ์ €์žฅ
    • ์„ค์ • ์ •๋ณด ๊ด€๋ฆฌ
    • ์ƒํ’ˆ ์ •๋ณด ์ €์žฅ
    • ๋ณต์žกํ•œ ์„ธ์…˜ ๋ฐ์ดํ„ฐ ์ €์žฅ

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๋ฐ์ดํ„ฐ ์ €์žฅ
127.0.0.1:6379> HSET user:user02 username "user02" email "user02@example.com" age "30"
(integer) 3

# ํŠน์ • ํ•„๋“œ ์กฐํšŒ
127.0.0.1:6379> HGET user:user02 username
"user02"

# ์—ฌ๋Ÿฌ ํ•„๋“œ ํ•œ๋ฒˆ์— ์กฐํšŒ
127.0.0.1:6379> HMGET user:user02 username email age
1) "user02"
2) "user02@example.com"
3) "30"

# ๋ชจ๋“  field-value ์กฐํšŒ
127.0.0.1:6379> HGETALL user:user02
1) "username"
2) "user02"
3) "email"
4) "user02@example.com"
5) "age"
6) "30"

# ํŠน์ • ํ•„๋“œ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
127.0.0.1:6379> HEXISTS user:user02 email
(integer) 1

# ๋ชจ๋“  ํ•„๋“œ ์ด๋ฆ„ ์กฐํšŒ
127.0.0.1:6379> HKEYS user:user02
1) "username"
2) "email"
3) "age"

# ๋ชจ๋“  ๊ฐ’ ์กฐํšŒ
127.0.0.1:6379> HVALS user:user02
1) "user02"
2) "user02@example.com"
3) "30"

# ํ•„๋“œ ๊ฐœ์ˆ˜ ์กฐํšŒ
127.0.0.1:6379> HLEN user:user02
(integer) 3

# ์ˆซ์ž ๊ฐ’ ์ฆ๊ฐ€
127.0.0.1:6379> HINCRBY user:user02 age 2
(integer) 32

# ํŠน์ • ํ•„๋“œ ๊ฐ’ ์ˆ˜์ •
127.0.0.1:6379> HSET user:user02 username "lina"
(integer) 0  # # ๊ธฐ์กด ํ•„๋“œ ์ˆ˜์ •์‹œ 0 ๋ฐ˜ํ™˜, ์ƒˆ๋กœ์šด ํ•„๋“œ ์ถ”๊ฐ€์‹œ 1 ๋ฐ˜ํ™˜

# ํŠน์ • ํ•„๋“œ ์‚ญ์ œ (ํŠน์ • ํ•„๋“œ ์„ ํƒ์ ์œผ๋กœ ์‚ญ์ œ)
127.0.0.1:6379> HDEL user:user02 username
(integer) 1

# ํ‚ค ์ž์ฒด๋ฅผ ์ „๋ถ€ ์‚ญ์ œ
127.0.0.1:6379> DEL user:user02
(integer) 0

HASH ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ๋‘ ๊ฐ€์ง€ ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
์ธ์ฝ”๋”ฉ์„ ์„ ํƒํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž๋™์œผ๋กœ ์„ค์ •๋˜๋ฉฐ,
์ค‘๊ฐ„์— ์กฐ๊ฑด์ด ๋„˜์œผ๋ฉด listpack์—์„œ hashtable๋กœ ์ž๋™์œผ๋กœ ์ „ํ™˜๋œ๋‹ค.

๊ตฌ๋ถ„ ์ธ์ฝ”๋”ฉ ์กฐ๊ฑด  ์žฅ์ 
์ž‘์€ Hash Ziplist
listpack (7.0์ด์ƒ)
ํ•„๋“œ ๊ฐœ์ˆ˜๊ฐ€ 512๊ฐœ ์ดํ•˜ 
๋ชจ๋“  ํ•„๋“œ์™€ ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ 64๋ฐ”์ดํŠธ ์ดํ•˜
(๋‘ ์กฐ๊ฑด ๋ชจ๋‘ ๋งŒ์กฑํ•ด์•ผ ํ•จ)
๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ์ ์Œ
์ž‘์€ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ์ด ๋น ๋ฆ„
ํฐ Hash Hashtable ํ•„๋“œ ๊ฐœ์ˆ˜๊ฐ€ 512๊ฐœ ์ดˆ๊ณผ
ํ•„๋“œ๋‚˜ ๊ฐ’์˜ ๊ธธ์ด๊ฐ€ 64๋ฐ”์ดํŠธ ์ดˆ๊ณผ
๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ์— ํšจ์œจ์ 
๊ฐœ๋ณ„ ํ•„๋“œ ์ ‘๊ทผ์ด O(1)๋กœ ๋งค์šฐ ๋น ๋ฆ„
ํ•„๋“œ ์ถ”๊ฐ€/์‚ญ์ œ๊ฐ€ ์ž์œ ๋กœ์›€
๋ฉ”๋ชจ๋ฆฌ ํŒŒํŽธํ™”๊ฐ€ ์ ์Œ

์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์„ ํ™•์ธํ•ด๋ณด์ž.

# small hash
127.0.0.1:6379> HSET smallhash field1 "123123" field2 "this is data"
(integer) 1
127.0.0.1:6379> OBJECT ENCODING smallhash
"listpack"

# big hash
127.0.0.1:6379> HSET bighash description "This is a very long description that definitely exceeds 64 bytes. We need to make sure it's long enough to trigger the hashtable encoding in Redis. This should do it!"
(integer) 1
127.0.0.1:6379> OBJECT ENCODING bighash
"hashtable"

 

 

6. Streams (์ŠคํŠธ๋ฆผ)

- https://redis.io/docs/latest/develop/data-types/streams/ 

Streams๋Š” ๋กœ๊ทธ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ์‹œํ€€์Šค๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ, ๋ฉ”์‹œ์ง€ ํ์™€ ์œ ์‚ฌํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.
๊ฐ ๋ฉ”์‹œ์ง€๋Š” ๊ณ ์œ ํ•œ ID๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, ID๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ๊ด€๋œ ํ•ญ๋ชฉ์„ ๊ฒ€์ƒ‰ํ•˜๊ฑฐ๋‚˜ ํ›„์†ํ•ญ๋ชฉ์„ ์ฝ๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
XADD key ID field value                     # ์ŠคํŠธ๋ฆผ์— ์ƒˆ ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
XREAD COUNT n STREAMS key ID  # ์ŠคํŠธ๋ฆผ์—์„œ ๋ฉ”์‹œ์ง€ ์ฝ๊ธฐ
XRANGE                                              # ๋‘ ID ์‚ฌ์ด์˜ ํ•ญ๋ชฉ ๋ฒ”์œ„ ๋ฐ˜ํ™˜
XLEN key                                             # ์ŠคํŠธ๋ฆผ์˜ ๊ธธ์ด ๋ฐ˜ํ™˜
XDEL key ID                                         # ์ŠคํŠธ๋ฆผ์—์„œ ๋ฉ”์‹œ์ง€ ์‚ญ์ œ
  • ํ™œ์šฉ์‚ฌ๋ก€
    • Pub/Sub
    • ์ด๋ฒคํŠธ ์†Œ์‹ฑ: ์‚ฌ์šฉ์ž ๋™์ž‘ ํด๋ฆญ ๋“ฑ ์ถ”์ 
    • ๋กœ๊ทธ ์ˆ˜์ง‘: ์‹ค์‹œ๊ฐ„ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๋ฐ ๋ถ„์„ (์˜ˆ, ์„ผ์„œ ๋ชจ๋‹ˆํ„ฐ๋ง)
    • ๋ฐ์ดํ„ฐ ์ŠคํŠธ๋ฆฌ๋ฐ: ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ํ”ผ๋“œ ๊ด€๋ฆฌ

Redis๋ฅผ Pub/Sub์œผ๋กœ ํ™œ์šฉํ•˜๋Š”๊ฒƒ์€ XGROUP ์ปค๋ฉ˜๋“œ Docs์—์„œ ์ž์„ธํžˆ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ž˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ์ด๋‹ค.

# ์ŠคํŠธ๋ฆผ์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
127.0.0.1:6379> XADD events * type "user_signup" user_id "123"
"1735318144850-0"
127.0.0.1:6379> XADD events * type "purchase" user_id "456" amount "99.99"
"1735318174147-0"

# Subscriber ๊ทธ๋ฃน ์ƒ์„ฑ
127.0.0.1:6379> XGROUP CREATE events mygroup $ MKSTREAM
OK

# Subscriber ๋ฐ์ดํ„ฐ ์†Œ๋น„
127.0.0.1:6379> XREADGROUP GROUP mygroup consumer1 COUNT 0 STREAMS events > # ์ฝ์ง€์•Š์€ ๋ฉ”์„ธ์ง€ ์ฝ๊ธฐ
1) 1) "events"
   2) 1) 1) "1735318174147-0"
         2) 1) "type"
            2) "purchase"
            3) "user_id"
            4) "456"
            5) "amount"
            6) "99.99"
            
127.0.0.1:6379> XREADGROUP GROUP mygroup consumer1 COUNT 0 STREAMS events > # ์ฝ์ง€ ์•Š์€ ๋ฉ”์„ธ์ง€ ์—†์Œ
(nil)

127.0.0.1:6379> XREADGROUP GROUP mygroup consumer1 COUNT 0 STREAMS events 0 # ํŠน์ • ๋ฉ”์„ธ์ง€ ์ฝ๊ธฐ
1) 1) "events"
   2) 1) 1) "1735318174147-0"
         2) 1) "type"
            2) "purchase"
            3) "user_id"
            4) "456"
            5) "amount"
            6) "99.99"

# ๋ฉ”์„ธ์ง€ ํ™•์ธ ์ฒ˜๋ฆฌ 
127.0.0.1:6379> XREADGROUP GROUP mygroup consumer1 COUNT 0 STREAMS events 0
1) 1) "events"
   2) 1) 1) "1735318174147-0"
         2) 1) "type"
            2) "purchase"
            3) "user_id"
            4) "456"
            5) "amount"
            6) "99.99"
127.0.0.1:6379> XACK events mygroup 1735318174147-0
(integer) 1
127.0.0.1:6379> XREADGROUP GROUP mygroup consumer1 COUNT 0 STREAMS events 0
1) 1) "events"
   2) (empty array)

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ์ŠคํŠธ๋ฆผ์— ์ƒˆ ๋ฉ”์„ธ์ง€ ์ €์žฅ
127.0.0.1:6379> XADD user_clicks * user_id 1 action "click" page "home"
"1735316702887-0"
127.0.0.1:6379> XADD user_clicks * user_id 2 action "click" page "about"
"1735316712422-0"
127.0.0.1:6379> XADD user_clicks * user_id 1 action "click" page "contact"
"1735316719007-0"
127.0.0.1:6379> XADD user_clicks * user_id 2 action "click" page "log-in"
"1735316729574-0"

# ์ŠคํŠธ๋ฆผ ๋ฉ”์„ธ์ง€ ์ฝ๊ธฐ
127.0.0.1:6379> XREAD COUNT 2 STREAMS user_clicks 0  # 0(์ฒ˜์Œ)๋ถ€ํ„ฐ 2๊ฐœ์˜ ๋ฉ”์„ธ์ง€๋ฅผ ์ฝ๊ธฐ
1) 1) "user_clicks"
   2) 1) 1) "1735316702887-0"
         2) 1) "user_id"
            2) "1"
            3) "action"
            4) "click"
            5) "page"
            6) "home"
      2) 1) "1735316712422-0"
         2) 1) "user_id"
            2) "2"
            3) "action"
            4) "click"
            5) "page"
            6) "about"

# ๋ชจ๋“  ๋ฉ”์„ธ์ง€ ์กฐํšŒ
127.0.0.1:6379> XRANGE user_clicks - +
1) 1) "1735316702887-0"
   2) 1) "user_id"
      2) "1"
      3) "action"
      4) "click"
      5) "page"
      6) "home"
2) 1) "1735316712422-0"
   2) 1) "user_id"
      2) "2"
      3) "action"
      4) "click"
      5) "page"
      6) "about"
3) 1) "1735316719007-0"
   2) 1) "user_id"
      2) "1"
      3) "action"
      4) "click"
      5) "page"
      6) "contact"
4) 1) "1735316729574-0"
   2) 1) "user_id"
      2) "2"
      3) "action"
      4) "click"
      5) "page"
      6) "log-in"
      
# ์ŠคํŠธ๋ฆผ ๊ธธ์ด ๋ฐ˜ํ™˜
127.0.0.1:6379> XLEN user_clicks
(integer) 4

# ๋ฉ”์„ธ์ง€ ์‚ญ์ œ
127.0.0.1:6379> XDEL user_clicks 1735316719007-0
(integer) 1
127.0.0.1:6379> XRANGE user_clicks - +
1) 1) "1735316702887-0"
   2) 1) "user_id"
      2) "1"
      3) "action"
      4) "click"
      5) "page"
      6) "home"
2) 1) "1735316712422-0"
   2) 1) "user_id"
      2) "2"
      3) "action"
      4) "click"
      5) "page"
      6) "about"
3) 1) "1735316729574-0"
   2) 1) "user_id"
      2) "2"
      3) "action"
      4) "click"
      5) "page"
      6) "log-in"


โš ๏ธ์ฃผ์˜์‚ฌํ•ญโš ๏ธ 1. ๋ฐ์ดํ„ฐ ๋ถ„๋ฐฐ ์ €์žฅ

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

# ๊ถŒ์žฅํ•˜์ง€์•Š์Œ : ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์˜ ์ŠคํŠธ๋ฆผ์— ์ €์žฅ
XADD user_clicks * user_id 1 action "click" page "home"
XADD user_clicks * user_id 2 action "click" page "about"
XADD user_clicks * user_id 1 action "click" page "contact"

# ๊ถŒ์žฅ : ์ ์ ˆํ•œ ๋ณด์กฐ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ„๋„์˜ ์ŠคํŠธ๋ฆผ์— ์ €์žฅ
XADD user_clicks:1 * action "click" page "home"
XADD user_clicks:2 * action "click" page "about"
XADD user_clicks:1 * action "click" page "contact"

โš ๏ธ์ฃผ์˜์‚ฌํ•ญโš ๏ธ 2. ๋ฐ์ดํ„ฐ ๋‚ด๊ตฌ์„ฑ

์œ„ List์—์„œ๋„ ์„ค๋ช…ํ–ˆ์ง€๋งŒ, ํ•œ๋ฒˆ ๋” ๊ฐ•์กฐํ•œ๋‹ค.
Redis๋Š” ๋ฐ์ดํ„ฐ์˜ ๋‚ด๊ตฌ์„ฑ์„ ๋ณด์žฅํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ(์บ์‹ฑ ํŠนํ™”), ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ์Œ์„ ์ธ์ง€ํ•ด์•ผํ•œ๋‹ค.
Redis๋Š” In-memory ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์ด๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๊ณ , ๋น„๊ต์  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ์„œ๋ฒ„๊ฐ€ ์žฌ์‹œ์ž‘๋˜๊ฑฐ๋‚˜ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒ ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค๋กœ์„œ์˜ ๊ณ ๊ธ‰ ๊ธฐ๋Šฅ(์žฌ์‹œ๋„ ๋“ฑ)๋„ ๋ถ€์กฑํ•˜๋‹ค.
๋”ฐ๋ผ์„œ, Redis๋Š” ๋น ๋ฅธ ์ฒ˜๋ฆฌ ์†๋„๊ฐ€ ํ•„์š”ํ•˜๊ณ  ๋ฐ์ดํ„ฐ ์†์‹ค์ด ํฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋ฉฐ, ๋‚ด๊ตฌ์„ฑ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ์—๋Š” Kafka์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ์†”๋ฃจ์…˜์„ ๊ณ ๋ คํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
Redis Database Backup(RDB), Append Only File(AOF) ์„ค์ •์„ ์ˆ˜์ • ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ฆ‰์‹œ ๋””์Šคํฌ์— ์“ฐ๋„๋ก ๊ตฌ์„ฑํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ,
RDB์„ค์ •์€ ๋งˆ์ง€๋ง‰ ์Šค๋ƒ…์ƒท ์ดํ›„์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์†์‹ค๋  ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ณ , AOF๋Š” appendfsync always๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  appendfsync everysec๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 1์ดˆ๋งˆ๋‹ค ์ €์žฅํ•˜๊ฒŒ ์„ค์ •ํ•˜๋”๋ผ๋„ ์ตœ๋Œ€ 1์ดˆ์˜ ๋ฐ์ดํ„ฐ ์†์‹ค์ด ๋ฐœ์ƒํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
์ฆ‰, ์•„๋ฌด๋ฆฌ ์„ค์ •ํ•ด๋„ ์ „ํ†ต์ ์ธ RDBMS์ฒ˜๋Ÿผ ๋ฐ์ดํ„ฐ ๋‚ด๊ตฌ์„ฑ(Durability)์„ ๋ณด์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

 

โ–บ Special Data Type

 

1. Beatmaps (๋น„ํŠธ๋งต)

https://redis.io/docs/latest/develop/data-types/bitmaps/

Bitmaps๋Š” Redis์˜ String ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜์—ฌ ๋น„ํŠธ ๋‹จ์œ„(0/1)์˜ ์กฐ์ž‘์„ ์ง€์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
ํšจ์œจ์ ์ธ ๋น„ํŠธ ์—ฐ์‚ฐ์„ ํ†ตํ•ด ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
SETBIT key offset value           # ํŠน์ • ๋น„ํŠธ ์œ„์น˜์— ๊ฐ’์„ ์„ค์ •
GETBIT key offset                     # ํŠน์ • ๋น„ํŠธ ์œ„์น˜์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜
BITCOUNT key [start end]       # ๋น„ํŠธ๋งต์—์„œ 1๋กœ ์„ค์ •๋œ ๋น„ํŠธ์˜ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
BITOP <AND | OR | XOR | NOT> destkey key [key ...].  # ๋น„ํŠธ ์—ฐ์‚ฐ ์ˆ˜ํ–‰ (AND, OR, XOR, NOT)
BITPOS  key bit [start [end [BYTE | BIT]]]                      # ์ง€์ •๋œ ๊ฐ’ (0 ๋˜๋Š” 1)์„ ๊ฐ–๋Š” ์ฒซ ๋ฒˆ์งธ ๋น„ํŠธ ์„ธํŠธ ๋ฐ˜ํ™˜
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ํ”Œ๋ž˜๊ทธ ๊ด€๋ฆฌ: ์‚ฌ์šฉ์ž์˜ ์ƒํƒœ๋‚˜ ์„ค์ •์„ ๋น„ํŠธ๋กœ ๊ด€๋ฆฌ
    • ์œ ๋‹ˆํฌ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ๊ณ„์‚ฐ: ํŠน์ • ๊ธฐ๊ฐ„ ๋™์•ˆ์˜ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ํšจ์œจ์ ์œผ๋กœ ๊ณ„์‚ฐ
    • ๋Œ€๋Ÿ‰ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ: ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์˜ ๊ฐ„๋‹จํ•œ ๋น„ํŠธ ๊ณ„์‚ฐ

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๋ฐ์ดํ„ฐ ์ž…๋ ฅ (ํŠน์ • ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๋ฌธํ–ˆ์Œ์„ ๊ธฐ๋ก)
127.0.0.1:6379> SETBIT day1:visitors 1 1
(integer) 0
127.0.0.1:6379> SETBIT day1:visitors 2 1
(integer) 0
127.0.0.1:6379> SETBIT day1:visitors 9 1
(integer) 0

# ๋ฐ์ดํ„ฐ ์กฐํšŒ (ํŠน์ • ์‚ฌ์šฉ์ž ๋ฐฉ๋ฌธํ–ˆ๋Š”์ง€ ํ™•์ธ)
127.0.0.1:6379> GETBIT day1:visitors 1
(integer) 1
127.0.0.1:6379> GETBIT day1:visitors 2
(integer) 1
127.0.0.1:6379> GETBIT day1:visitors 3
(integer) 0

# ์ด ์œ ๋‹ˆํฌ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ๊ณ„์‚ฐ
127.0.0.1:6379> BITCOUNT day1:visitors
(integer) 3

# OR ์—ฐ์‚ฐ (Day1, Day2์— ๋ฐฉ๋ฌธํ•œ ์ด ์‚ฌ์šฉ์ž)
127.0.0.1:6379> BITOP OR total_visitors day1:visitors day2:visitors
(integer) 2
127.0.0.1:6379> BITCOUNT total_visitors
(integer) 4

# AND ์—ฐ์‚ฐ (Day1, Day2์— ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž)
127.0.0.1:6379> BITOP AND total_visitors day1:visitors day2:visitors
(integer) 2
127.0.0.1:6379> BITCOUNT total_visitors
(integer) 1

 

 

2. Geospatial (์ง€๋ฆฌ๊ณต๊ฐ„)

- https://redis.io/docs/latest/develop/data-types/geospatial/

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

  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
GEOADD key longitude latitude member           # ์œ„์น˜ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€
GEOPOS key member                                          # ํŠน์ • ๋ฉค๋ฒ„์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
GEODIST key member1 member2 [unit]            # ๋‘ ์œ„์น˜ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜
GEORADIUS key longitude latitude radius [unit]         # ๋ฐ˜๊ฒฝ ๋‚ด ์œ„์น˜ ๊ฒ€์ƒ‰
GEORADIUSBYMEMBER key member radius [unit]    # ํŠน์ • ๋ฉค๋ฒ„๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ฐ˜๊ฒฝ ๋‚ด ์œ„์น˜ ๊ฒ€์ƒ‰
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ์œ„์น˜ ๊ธฐ๋ฐ˜ ์„œ๋น„์Šค: ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ฃผ๋ณ€ ์ •๋ณด๋ฅผ ์ œ๊ณต
    • ๋ฌผ๋ฅ˜ ๋ฐ ๋ฐฐ์†ก ๊ด€๋ฆฌ: ํŠน์ • ์œ„์น˜๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ฐ˜๊ฒฝ ๋‚ด ๋ฐฐ์†ก์ง€ ๊ฒ€์ƒ‰
    • ์ง€๋„ ๋ฐ ๋‚ด๋น„๊ฒŒ์ด์…˜: ๊ฒฝ๋กœ ํƒ์ƒ‰ ๋ฐ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๋ฐ์ดํ„ฐ ์ž…๋ ฅ (๋„์‹œ ์œ„์น˜ ์ •๋ณด ์ž…๋ ฅ)
127.0.0.1:6379> GEOADD cities 126.9784 37.5665 "Seoul"
(integer) 1
127.0.0.1:6379> GEOADD cities 129.0756 35.1796 "Busan"
(integer) 1
127.0.0.1:6379> GEOADD cities 116.4074 39.9042 "Beijing"
(integer) 1
127.0.0.1:6379> GEOADD cities 139.6917 35.6895 "Tokyo"
(integer) 1
127.0.0.1:6379> GEOADD cities -0.1276 51.5074 "London"
(integer) 1
127.0.0.1:6379> GEOADD cities -74.0060 40.7128 "New York"
(integer) 1
127.0.0.1:6379> GEOADD cities 2.3522 48.8566 "Paris"
(integer) 1
127.0.0.1:6379> GEOADD cities 151.2093 -33.8688 "Sydney"
(integer) 1
127.0.0.1:6379> GEOADD cities 13.4050 52.5200 "Berlin"
(integer) 1
127.0.0.1:6379> GEOADD cities 55.2708 25.2048 "Dubai"
(integer) 1
127.0.0.1:6379> GEOADD cities 37.6173 55.7558 "Moscow"
(integer) 1
127.0.0.1:6379> GEOADD cities -58.3816 -34.6037 "Buenos Aires"
(integer) 1

# ์กฐํšŒ (์„œ์šธ ์œ„์น˜ ๋ฐ˜ํ™˜)
127.0.0.1:6379> GEOPOS cities "Seoul"
1) 1) "126.97840064764022827"
   2) "37.5665003062872529"
   
# ๊ฑฐ๋ฆฌ ๋ฐ˜ํ™˜ (๋‘ ๋‘์‹œ ๊ฐ„์˜ ๊ฑฐ๋ฆฌ)
127.0.0.1:6379> GEODIST cities "Seoul" "Busan" km
"325.1823"
127.0.0.1:6379> GEODIST cities "Seoul" "New York" km
"11055.6856"

# ํŠน์ • ์œ„์น˜ ์ค‘์‹ฌ์˜ ๋„์‹œ ๊ฒ€์ƒ‰
127.0.0.1:6379> GEORADIUS cities 126.9784 37.5665 50 km
1) "Seoul"

# ํŠน์ • ๋„์‹œ ์ค‘์‹ฌ์œผ๋กœ ๋ฐ˜๊ฒฝ ๋‚ด ๊ฒ€์ƒ‰
127.0.0.1:6379> GEORADIUSBYMEMBER cities "Paris" 500 km
1) "Paris"
2) "London"
127.0.0.1:6379> GEORADIUSBYMEMBER cities "Seoul" 500 km
1) "Seoul"
2) "Busan"
127.0.0.1:6379> GEORADIUSBYMEMBER cities "Seoul" 1000 km
1) "Beijing"
2) "Busan"
3) "Seoul"

 

 

3. HyperLogLog (HLL)

- https://redis.io/docs/latest/develop/data-types/probabilistic/hyperloglogs/

HyperLogLog๋Š” ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์…‹์˜ ์œ ๋‹ˆํฌ ์š”์†Œ ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‹ค.
์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๊ณ , ํ•ด์‹œ๊ฐ’์„ ์‚ฌ์šฉํ•ด์„œ ์œ ๋‹ˆํฌ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถ”์ •ํ•˜๋Š”๋ฐ ํŠนํ™”๋˜์–ด์žˆ๋‹ค.
์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š”๊ฒƒ์ด ์•„๋‹˜์„ ์ฃผ์˜ํ•˜์ž!

  • ํŠน์ง•
    • ์ถ”์ • ๊ธฐ๋ฐ˜: ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๊ณ , ํ•ด์‹œ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ์œ ๋‹ˆํฌ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถ”์ •ํ•จ
    • ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ: ์•ฝ 12KB์˜ ๊ณ ์ •๋œ ๋ฉ”๋ชจ๋ฆฌ๋งŒ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์‹ญ์–ต ๊ฐœ์˜ ์œ ๋‹ˆํฌ ์š”์†Œ๋ฅผ ์ถ”์ •ํ•  ์ˆ˜ ์žˆ์Œ
    • ์ •ํ™•๋„: ์•ฝ 0.81%์˜ ์˜ค์ฐจ ๋ฒ”์œ„ ๋‚ด์—์„œ ์œ ๋‹ˆํฌ ์š”์†Œ ์ˆ˜๋ฅผ ์ถ”์ •ํ•จ
  • ๊ธฐ๋ณธ ์ปค๋ฉ˜๋“œ
PFADD key element [element ...]   # ์š”์†Œ๋ฅผ HyperLogLog์— ์ถ”๊ฐ€
PFCOUNT key [key ...]             # ์œ ๋‹ˆํฌ ์š”์†Œ์˜ ์ถ”์ • ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
PFMERGE destkey sourcekey [sourcekey ...] # ์—ฌ๋Ÿฌ HyperLogLog๋ฅผ ๋ณ‘ํ•ฉ
  • ํ™œ์šฉ์‚ฌ๋ก€
    • ์œ ๋‹ˆํฌ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ๊ณ„์‚ฐ: ์›น์‚ฌ์ดํŠธ์˜ ์ผ์ผ ์œ ๋‹ˆํฌ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ์ถ”์ •
    • ์ด๋ฒคํŠธ ์นด์šดํŒ…: ๋Œ€๊ทœ๋ชจ ์ด๋ฒคํŠธ์˜ ์œ ๋‹ˆํฌ ๋ฐœ์ƒ ํšŸ์ˆ˜ ์ถ”์ •
    • ๋ฐ์ดํ„ฐ ๋ถ„์„: ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ์—์„œ ์œ ๋‹ˆํฌ ์š”์†Œ ์ˆ˜๋ฅผ ๋น ๋ฅด๊ฒŒ ์ถ”์ •

๐Ÿ‘‡๐Ÿ‘‡ ์‹ค์Šต์€ ์•„๋ž˜ ๋”๋ณด๊ธฐ ํด๋ฆญ ๐Ÿ‘‡๐Ÿ‘‡ 

๋”๋ณด๊ธฐ
# ๋ฐ์ดํ„ฐ ์ž…๋ ฅ (์‚ฌ์šฉ์ž๊ฐ€ ๋…ธ๋ž˜๋ฅผ ์žฌ์ƒํ• ๋•Œ๋งˆ๋‹ค HLL์— ์ถ”๊ฐ€)
127.0.0.1:6379> PFADD ROSE_APT "user1"
(integer) 1
127.0.0.1:6379> PFADD ROSE_APT "user2"
(integer) 1
127.0.0.1:6379> PFADD ROSE_APT "user3"
(integer) 1
127.0.0.1:6379> PFADD ROSE_APT "user1"
(integer) 0

# ์œ ๋‹ˆํฌ ์‚ฌ์šฉ์ž ์ˆ˜ ์ถ”์ •
127.0.0.1:6379> PFCOUNT ROSE_APT
(integer) 3

# ์—ฌ๋Ÿฌ ๋…ธ๋ž˜์˜ ์œ ๋‹ˆํฌ ์‚ฌ์šฉ์ž ์ˆ˜ ๋ณ‘ํ•ฉ
127.0.0.1:6379> PFADD AESPA_SUPERNOVA "user2"
(integer) 1
127.0.0.1:6379> PFADD AESPA_SUPERNOVA "user4"
(integer) 1
127.0.0.1:6379> PFMERGE K_POP ROSE_APT AESPA_SUPERNOVA
OK
127.0.0.1:6379> PFCOUNT K_POP
(integer) 4

 

 

 

728x90