DBMS/Redis

[REDIS] ๐Ÿ“š ์ž๋ฃŒ๊ตฌ์กฐ ๋ช…๋ น์–ด ์ข…๋ฅ˜ & ํ™œ์šฉ ์‚ฌ๋ก€ ๐Ÿ’ฏ ์ด์ •๋ฆฌ

์ธํŒŒ_ 2022. 7. 4. 10:09

redis-์ž๋ฃŒ๊ตฌ์กฐ

Redis ๋ฐ์ดํ„ฐ ํƒ€์ž… (Collection)

Redis์˜ ์žฅ์  ์ค‘ ํ•˜๋‚˜๋Š” Key-Value ์Šคํ† ๋ฆฌ์ง€์—์„œ Value๋Š” ๋‹จ์ˆœํ•œ Object๊ฐ€ ์•„๋‹ˆ๋ผ ๋‹ค์–‘ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

String, Set, Sorted Set, Hash, List ๋“ฑ ๋‹ค์–‘ํ•œ ํƒ€์ž…์„ ์ง€์›ํ•œ๋‹ค.

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๊ฐ ๋ ˆ๋””์Šค์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์•Œ์•„๋ณด๊ณ  ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ช…๋ น์–ด(command)๋ฅผ ์ •๋ฆฌํ•ด๋ณด๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณด๊ฒŸ๋‹ค.

Redis Collections ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜์ 
ํ•˜๋‚˜์˜ ์ปฌ๋ ‰์…˜์— ๋„ˆ๋ฌด ๋งŽ์€ ์•„์ดํ…œ์„ ๋‹ด์œผ๋ฉด ์ข‹์ง€ ์•Š๋‹ค.
๊ฐ€๋Šฅํ•˜๋ฉด 10000๊ฐœ ์ดํ•˜์˜, ๋ช‡์ฒœ๊ฐœ ์ˆ˜์ค€์˜ ๋ฐ์ดํ„ฐ ์…‹์„ ์œ ์ง€ํ•˜๋Š”๊ฒŒ Redis ์„ฑ๋Šฅ์— ์˜ํ–ฅ์ฃผ์ง€ ์•Š๋Š”๋‹ค.

redis-collection


Redis - Strings

  • ์ผ๋ฐ˜์ ์ธ ๋ฌธ์ž์—ด
  • ๊ฐ’์€ ์ตœ๋Œ€ 512 MB์ด๋ฉฐ, String์œผ๋กœ ๋  ์ˆ˜ ์žˆ๋Š” binary data๋„, JPEG ์ด๋ฏธ์ง€๋„ ์ €์žฅ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ๋‹จ์ˆœ ์ฆ๊ฐ ์—ฐ์‚ฐ์— ์ข‹์Œ
  • string-string ๋งคํ•‘์„ ์ด์šฉํ•˜์—ฌ ์—ฐ๊ฒฐ๋˜๋Š” ์ž๋ฃŒ ๋งคํ•‘์„ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. HTML ๋งคํ•‘๋„ ๊ฐ€๋Šฅ

Redis - Strings

 

Strings ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

  • SET: SET, SETNX, SETEX, SETPEX, MSET, MSETNX, APPEND, SETRANGE
  • GET: GET, MGET, GETRANGE, STRLEN
  • INCR: INCR, DECR, INCRBY, DECRBY, INCRBYFLOAT
  • Enterprise: SETS, DELS, APPENDS (subquery)
Commands Syntax Description
DECR key 1์”ฉ ๊ฐ๊ฐ€, ์‹ ๊ทœ์ด๋ฉด -1๋กœ setting.
DECRBY key decrement decrement๋งŒํผ ๊ฐ์†Œ. ์‹ ๊ทœ์ด๋ฉด -decrement๋กœ setting.
DEL key [key ...] ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ
GET key ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ
GETSET key value ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•˜๊ณ  ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
INCR key 1์”ฉ ์ฆ๊ฐ€, ์‹ ๊ทœ์ด๋ฉด 1๋กœ setting.
INCRBY key increment increment๋งŒํผ ์ฆ๊ฐ€. ์‹ ๊ทœ์ด๋ฉด increment๋กœ setting.
MGET key [key ...] ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ์กฐํšŒ
SET key value [EX seconds]
[PX milliseconds] [NX|XX]
๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ, key๊ฐ€ ์ด๋ฏธ ์žˆ์œผ๋ฉด ๋ฎ์–ด์“ด๋‹ค.
SETNX key value ์ง€์ •ํ•œ key๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋งŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
MSET key value [key value ...] ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ์ €์žฅ
MSETNX key value [key value ...] ์ง€์ •ํ•œ key๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์—๋งŒ, ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•œ๋ฒˆ์— ์ €์žฅ
APPEND key value ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€, ์ง€์ •ํ•œ key๊ฐ€ ์—†์œผ๋ฉด ์ €์žฅ
SETEX key seconds value ์ง€์ •ํ•œ ์‹œ๊ฐ„(์ดˆ) ์ดํ›„์— ๋ฐ์ดํ„ฐ ์ž๋™ ์‚ญ์ œ
SETRANGE key offset value ์ง€์ •ํ•œ ์œ„์น˜(offset)๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒน์ณ์“ด๋‹ค
STRLEN key ๋ฐ์ดํ„ฐ์˜ ๋ฐ”์ดํŠธ์ˆ˜๋ฅผ ๋ฆฌํ„ด
GETRANGE key start end ๋ฐ์ดํ„ฐ์˜ ์ผ๋ถ€ ๋ฌธ์ž์—ด์„ ์กฐํšŒ
INCRBYFLOAT key increment ์‹ค์ˆ˜์—ฐ์‚ฐ, increment๋งŒํผ ์ฆ๊ฐ€. ์‹ ๊ทœ์ด๋ฉด increment๋กœ setting.
PSETEX key milliseconds value ์ง€์ •ํ•œ ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์ดˆ) ์ดํ›„์— ๋ฐ์ดํ„ฐ ์ž๋™ ์‚ญ์ œ
STRALGO STRALGO LCS ๋‘ ๋ฌธ์ž์—ด์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ํ‰๊ฐ€
GETEX key [EX seconds] ๋ฐ์ดํ„ฐ ์กฐํšŒ์™€ ๋งŒ๋ฃŒ ์‹œ๊ฐ„ ์„ค์ •
GETDEL key ๋ฐ์ดํ„ฐ ์กฐํšŒ์™€ ์‚ญ์ œ
SETS key (subquery) SETS key (subquery)
DELS (subquery) DELS (subquery)
APPENDS (subquery) APPENDS key (subquery)
# ํ•œ๊ฐœ ์กฐํšŒ
set <key> <value>
get <key> <value>

# ์—ฌ๋Ÿฌ๊ฐœ ์กฐํšŒ
mset <key> <value> <key> <value> ...
mget <key> <key> <key> ...
127.0.0.1:6379> set hello "world!"
OK

127.0.0.1:6379> get hello
"world!"

127.0.0.1:6379> get count
"-351"

127.0.0.1:6379> set count 50
OK

127.0.0.1:6379> incr count
(integer) 51

127.0.0.1:6379> get count
"51"

127.0.0.1:6379> incrby count 100
(integer) 151

127.0.0.1:6379> decr count
(integer) 150

127.0.0.1:6379> decrby count 500
(integer) -350

127.0.0.1:6379> mset a "hello" b "world"
OK

127.0.0.1:6379> mget a b
1) "hello"
2) "world"

Redis - Bitmaps

  • bitmaps์€ string์˜ ๋ณ€ํ˜•
  • bit ๋‹จ์œ„ ์—ฐ์‚ฐ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • String์ด 512MB ์ €์žฅ ํ•  ์ˆ˜ ์žˆ๋“ฏ์ด 2^32 bit๊นŒ์ง€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
  • ์ €์žฅํ•  ๋•Œ, ์ €์žฅ ๊ณต๊ฐ„ ์ ˆ์•ฝ์— ํฐ ์žฅ์ ์ด ์žˆ๋‹ค.

Redis - Bitmaps

 

Bits ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

Commands Syntax Description
GETBIT key offset bit ๊ฐ’ ์กฐํšŒ
SETBIT key offset value bit ๊ฐ’ ์กฐ์ •
BITCOUNT key [start end] 1์ธ bit ์ˆ˜๋ฅผ ์„ผ๋‹ค
BITOP key operation destkey key [key ...] bit ์—ฐ์‚ฐ( AND, OR, XOR, NOT) ์‹คํ–‰
BITPOS key bit [start [end]] ์ง€์ •ํ•œ bit์˜ ์œ„์น˜๋ฅผ ๊ตฌํ•œ๋‹ค
BITFIELD key [GET type offset] [SET type offset value] Perform arbitrary bitfield integer operations on strings
# setbit <key> <offset> <value>
# key: ํ•ด๋‹น ๋น„ํŠธ๋งต์„ ์นญํ•  ๊ฐ’
# offset: 0 ๋ณด๋‹ค ํฐ ์ •์ˆ˜์˜ ๊ฐ’
# value: 0 ๋˜๋Š” 1์˜ ๋น„ํŠธ ๊ฐ’

> setbit 20220410 4885 1

> getbit 20220410 4885

> bitcount 20220410 # ๋ฒ”์œ„ ๋‚ด์˜ 1๋กœ ์„ค์ •๋œ bit์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

Redis - Bitmaps


Redis - Lists

  • array ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ. ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ 
  • ์ถ”๊ฐ€ / ์‚ญ์ œ / ์กฐํšŒํ•˜๋Š” ๊ฒƒ์€ O(1)์˜ ์†๋„๋ฅผ ๊ฐ€์ง€์ง€๋งŒ, ์ค‘๊ฐ„์˜ ํŠน์ • index ๊ฐ’์„ ์กฐํšŒํ•  ๋•Œ๋Š” O(N)์˜ ์†๋„๋ฅผ ๊ฐ€์ง€๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.
  • ์ฆ‰, ์ค‘๊ฐ„์— ์ถ”๊ฐ€/์‚ญ์ œ๊ฐ€ ๋Š๋ฆฌ๋‹ค. ๋”ฐ๋ผ์„œ head-tail์—์„œ ์ถ”๊ฐ€/์‚ญ์ œ ํ•œ๋‹ค. (push / pop ์—ฐ์‚ฐ)
  • ๋ฉ”์„ธ์ง€ queue๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์ ์ ˆํ•˜๋‹ค.
์†Œ์…œ๋„คํŠธ์›Œํฌ์—์„œ ํƒ€์ž„๋ผ์ธ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ๋•Œ LPUSH๋ฅผ ํ†ตํ•ด ์ œ์ผ ์ฒซ ๋ถ€๋ถ„์— Insertํ•˜๋ฉฐ LRANGE ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ผ์ • ํฌ๊ธฐ๋ฅผ ๊ณ ์ •์ ์œผ๋กœ ๋น ๋ฅด๊ฒŒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
LPUSH ๋ช…๋ น์–ด์™€ LTRIM ๋ช…๋ น์–ด๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋ฉด Lists์˜ ํฌ๊ธฐ๋ฅผ ํ•ญ์ƒ ์ผ์ •ํ•˜๊ฒŒ ๊ณ ์ •์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
LPUSH์™€ RPOP์„ ์ด์šฉํ•œ๋‹ค๋ฉด message๋ฅผ ์ „๋‹ฌํ•˜๋Š” queue๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Redis - Lists

 

Lists ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

  • SET (PUSH): LPUSH, RPUSH, LPUSHX, RPUSHX, LSET, LINSERT, RPOPLPUSH
  • GET: LRANGE, LINDEX, LLEN
  • POP: LPOP, RPOP, BLPOP, BRPOP
  • REM: LREM, LTRIM
  • BLOCK: BLPOP, BRPOP, BRPOPLPUSH
  • Enterprise: LREVRANGE, LPUSHS, RPUSHS (subquery)
Commands Syntax Description
LPUSH key value [value ...] ์™ผ์ชฝ์—์„œ ๋ฆฌ์ŠคํŠธ์˜ ์˜ค๋ฅธ์ชฝ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
RPOP key ๋ฆฌ์ŠคํŠธ ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ค๊ณ , ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ์‚ญ์ œ
LPOP key ๋ฆฌ์ŠคํŠธ ์™ผ์ชฝ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์˜ค๊ณ , ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ์‚ญ์ œ
RPUSH key value [value ...] ์˜ค๋ฅธ์ชฝ์—์„œ ๋ฆฌ์ŠคํŠธ์˜ ์™ผ์ชฝ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
LRANGE key start stop ์ธ๋ฑ์Šค๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ
LLEN key ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ด ๊ฐฏ์ˆ˜๋ฅผ ์กฐํšŒ
LINDEX key index ์ธ๋ฑ์Šค๋กœ ํŠน์ • ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ
LSET key index value ์ธ๋ฑ์Šค๋กœ ํŠน์ • ์œ„์น˜์˜ ๊ฐ’์„ ๋ฐ”๊ฟˆ
LREM key count value ๊ฐ’์„ ์ง€์ •ํ•ด์„œ ์‚ญ์ œ
LTRIM key start stop ์ธ๋ฑ์Šค๋กœ ์ง€์ •ํ•œ ๋ฒ”์œ„ ๋ฐ–์˜ ๊ฐ’๋“ค์„ ์‚ญ์ œ
RPOPLPUSH key src_key dest_key RPOP + LPUSH
BLPOP key [key ...] timeout ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ, ์ง€์ •ํ•œ ์‹œ๊ฐ„๋งŒํผ ๊ธฐ๋‹ค๋ ค์„œ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด LPOP ์‹คํ–‰
BRPOP key [key ...] timeout ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ, ์ง€์ •ํ•œ ์‹œ๊ฐ„๋งŒํผ ๊ธฐ๋‹ค๋ ค์„œ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด RPOP ์‹คํ–‰
BRPOPLPUSH src_key dest_key timeout ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ, ์ง€์ •ํ•œ ์‹œ๊ฐ„๋งŒํผ ๊ธฐ๋‹ค๋ ค์„œ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด RPOPLPUSH ์‹คํ–‰
LINSERT key BEFORE|AFTER pivot value ์ง€์ •ํ•œ ๊ฐ’ ์•ž/๋’ค์— ์ƒˆ ๊ฐ’ ์ €์žฅ
LPUSHX key value ๊ธฐ์กด์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ LPUSH ์‹คํ–‰
RPUSHX key value ๊ธฐ์กด์— ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ RPUSH ์‹คํ–‰
LPOS key element ๊ฐ’์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์กฐํšŒ
LMOVE source destination ๋ฆฌ์ŠคํŠธ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™
BLMOVE source destination ๋ฆฌ์ŠคํŠธ๊ฐ„ ๋ฐ์ดํ„ฐ ์ด๋™ - ๋Œ€๊ธฐ
LLS key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์กฐํšŒ
LRM key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์‚ญ์ œ
LREVRANGE key key start stop ์ธ๋ฑ์Šค๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ์—ญ์ˆœ์œผ๋กœ ์กฐํšŒ
LPUSHS key (subquery) ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
RPUSHS key (subquery) ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
# ์™ผ์ชฝ์— ์‚ฝ์ž…
lpush <key> <value>

# ์˜ค๋ฅธ์ชฝ์— ์‚ฝ์ž…
rpush <key> <value>

# ์‚ญ์ œ
lpop <key>
rpop <key>
# LPUSH๋ฅผ ํ†ตํ•œ list ์ƒ์„ฑ
127.0.0.1:6379> LPUSH myList "a"
(integer) 1
127.0.0.1:6379> LRANGE myList 0 -1
1) "a"

# LPUSH , RPUSH๋ฅผ ํ†ตํ•œ ์š”์†Œ ์‚ฝ์ž… ๊ฒฐ๊ณผ.
127.0.0.1:6379> LPUSH myList "b"
(integer) 2
127.0.0.1:6379> RPUSH myList "c"
(integer) 3
127.0.0.1:6379> LRANGE myList 0 -1
1) "b"
2) "a"
3) "c"

# LPUSHX , RPUSHX ์‚ฌ์šฉ ์˜ˆ
# key๊ฐ€ ์—†๋Š”๊ณณ์— ์ถ”๊ฐ€ํ• ๋ ค๊ณ  ํ•˜๋Š” ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜.
127.0.0.1:6379> LPUSHX myList2 "a"
(integer) 0
127.0.0.1:6379> LPUSH myList "d"
(integer) 4


# ๊ธฐ์กด์— ์žˆ๋˜ myList์š”์†Œ๋“ค์€ [ d , b , a , c ] ์ˆœ์œผ๋กœ ๋˜์–ด์žˆ์Œ.
# LPOP , RPOP์„ ํ†ตํ•ด ๋งจ ์ขŒ ์šฐ์ธก ์š”์†Œ ํ•œ๊ฐœ์”ฉ ์ œ๊ฑฐ 
127.0.0.1:6379> LPOP myList
"d"
127.0.0.1:6379> RPOP myList
"c"
127.0.0.1:6379> LRANGE myList 0 -1
1) "b"
2) "a"

# ํ˜„์žฌ List์˜ ์š”์†Œ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅ.
127.0.0.1:6379> LLEN myList
(integer) 2

# LREM์„ ํ†ตํ•ธ ํ•ด๋‹น ์š”์†Œ ์‚ญ์ œ , count๋ฅผ 0 ์œผ๋กœ ํ•ด์„œ ์š”์†Œ์ค‘์— "a"๋ž‘ ๋งค์นญ๋˜๋Š” ๊ฐ’์„ ์‚ญ์ œ
127.0.0.1:6379> LREM myList 0 "a"
(integer) 1
127.0.0.1:6379> LRANGE myList 0 -1
1) "b"

# ํ•ด๋‹น key์— ํ•ด๋‹น๋˜๋Š” index๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ์ˆ˜์ •.
# ํ˜„์žฌ b๋กœ ๋‚จ์•„์žˆ๋˜ ์š”์†Œ๊ฐ’์„ z๋กœ ๋ณ€๊ฒฝ
127.0.0.1:6379> LSET myList 0 "z"
OK
127.0.0.1:6379> LRANGE myList 0 -1
1) "z"

# RPOPLPUSH 
127.0.0.1:6379> RPOPLPUSH myList hello
"z"
127.0.0.1:6379> LRANGE hello 0 -1
1) "z"

Redis - Hashes

  • field-value๋กœ ๊ตฌ์„ฑ ๋˜์–ด์žˆ๋Š” ์ „ํ˜•์ ์ธ hash์˜ ํ˜•ํƒœ (ํŒŒ์ด์ฌ์˜ ๋”•์…”๋„ˆ๋ฆฌ๋‚˜ js๊ฐ์ฒด ์ •๋„๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค)
  • key ํ•˜์œ„์— subkey๋ฅผ ์ด์šฉํ•ด ์ถ”๊ฐ€์ ์ธ Hash Table์„ ์ œ๊ณตํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ
  • ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ—ˆ์šฉํ•˜๋Š” ํ•œ, ์ œํ•œ์—†์ด field๋“ค์„ ๋„ฃ์„ ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

Redis - Hashes

 

Hashes ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

  • SET: HSET, HMSET, HSETNX
  • GET: HGET, HMGET, HLEN, HKEYS, HVALS, HGETALL, HSTRLEN, HSCAN, HEXISTS
  • REM: HDEL
  • INCR: HINCRBY, HINCRBYFLOAT
Commands Syntax Description
HSET key field value Field์™€ value๋ฅผ ์ €์žฅ
HDEL key field [field ...] Field๋กœ value๋ฅผ ์‚ญ์ œ
HGET key field Field๋กœ value๋ฅผ ์กฐํšŒ
HLEN key Field ๊ฐฏ์ˆ˜ ์กฐํšŒ
HMSET key field value [field value ...] ์—ฌ๋Ÿฌ๊ฐœ์˜ field์™€ value๋ฅผ ์ €์žฅ
HMGET key field [field ...] ์—ฌ๋Ÿฌ๊ฐœ์˜ value๋ฅผ ์กฐํšŒ
HKEYS key Key์— ์†ํ•œ ๋ชจ๋“  field name์„ ์กฐํšŒ
HVALS key Key์— ์†ํ•œ ๋ชจ๋“  value๋ฅผ ์กฐํšŒ
HGETALL key Key์— ์†ํ•œ ๋ชจ๋“  field์™€ value์„ ์กฐํšŒ
HINCRBY key field increment value๋ฅผ increment ๋งŒํผ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ
HEXISTS key field Field๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ
HSETNX key field value Field๊ฐ€ ๊ธฐ์กด์— ์—†์œผ๋ฉด ์ €์žฅ
HINCRBYFLOAT key field increment_float value๋ฅผ increment_float ๋งŒํผ ์ฆ๊ฐ€ ๋˜๋Š” ๊ฐ์†Œ
HSCAN key cursor [MATCH pattern]
[COUNT count]
Field, member๋ฅผ ์ผ์ • ๋‹จ์œ„ ๊ฐฏ์ˆ˜ ๋งŒํผ์”ฉ ์กฐํšŒ
HSTRLEN key field value์˜ ๊ธธ์ด(byte)๋ฅผ ์กฐํšŒ
# ํ•œ๊ฐœ ๊ฐ’ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ
hset <key> <subkey> <value>
hget <key> <subkey>

# ์—ฌ๋Ÿฌ ๊ฐ’ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ
hmset <key> <subkey> <value> <subkey> <value> ...
hnget <key> <subkey> <subkey> <subkey> ... 

# ๋ชจ๋“  subkey์™€ value ๊ฐ€์ ธ์˜ค๊ธฐ, Collection์— ๋„ˆ๋ฌด ๋งŽ์€ key๊ฐ€ ์žˆ์œผ๋ฉด ์žฅ์• ์˜ ์›์ธ์ด ๋จ
hgetall <key>

# ๋ชจ๋“  value๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
hvlas <key>
# field - value : name - jinmin / year - 1995 / month - 3
127.0.0.1:6379> hset hh name jinmin year 1995 month 3
(integer) 3

127.0.0.1:6379> hget hh name
"jinmin"

127.0.0.1:6379> hget hh year
"1995"

127.0.0.1:6379> hdel hh year
(integer) 1

127.0.0.1:6379> hlen hh
(integer) 2

127.0.0.1:6379> hgetAll hh
1) "name"
2) "jinmin"
3) "month"
4) "3"

127.0.0.1:6379> hkeys hh
1) "name"
2) "month"

127.0.0.1:6379> hvals hh
1) "jinmin"
2) "3"

Redis - Sets

  • ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์ง€ ์•Š๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ (js์˜ set์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค)
  • ์œ ๋‹ˆํฌํ•œ key๊ฐ’
  • ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ง‘ํ•ฉ
  • ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ์ €์žฅํ•˜๋ฉด ์ตœ์ข… ํ•œ๋ฒˆ๋งŒ ์ €์žฅ๋œ๋‹ค.
  • Set๊ฐ„์˜ ์—ฐ์‚ฐ์„ ์ง€์›. ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ด๋ฅผ ๋งค์šฐ ๋น ๋ฅธ ์‹œ๊ฐ„๋‚ด์— ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹จ, ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋ถ€ ๋‹ค ๊ฐ–๊ณ ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์ด ์žˆ์œผ๋ฏ€๋กœ ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

Redis - Sets

 

Sets ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

  • SET: SADD, SMOVE
  • GET: SMEMBERS, SCARD, SRANDMEMBER, SISMEMBER, SSCAN
  • POP: SPOP
  • REM: SREM
  • ์ง‘ํ•ฉ์—ฐ์‚ฐ: SUNION, SINTER, SDIFF, SUNIONSTORE, SINTERSTORE, SDIFFSTORE
  • Enterprise: SLS, SRM, SLEN, SADDS (subquery)
Commands Syntax Description
SADD key member [member ...] ์ง‘ํ•ฉ์— member๋ฅผ ์ถ”๊ฐ€
SREM key member [member ...] ์ง‘ํ•ฉ์—์„œ member๋ฅผ ์‚ญ์ œ
SMEMBERS key ์ง‘ํ•ฉ์˜ ๋ชจ๋“  member๋ฅผ ์กฐํšŒ
SCARD key ์ง‘ํ•ฉ์— ์†ํ•œ member์˜ ๊ฐฏ์ˆ˜๋ฅผ ์กฐํšŒ
SUNION key [key ...] ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•จ
SINTER key [key ...] ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•จ
SDIFF key [key ...] ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•จ
SUNIONSTORE dest_key src_key [src_key ...] ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
SINTERSTORE dest_key src_key [src_key ...] ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
SDIFFSTORE dest_key src_key [src_key ...] ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
SISMEMBER key member ์ง‘ํ•ฉ์— member๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
SMOVE src_key dest_key member ์†Œ์Šค ์ง‘ํ•ฉ์˜ member๋ฅผ ๋ชฉ์  ์ง‘ํ•ฉ์œผ๋กœ ์ด๋™
SPOP key [count] ์ง‘ํ•ฉ์—์„œ ๋ฌด์ž‘์œ„๋กœ member๋ฅผ ๊ฐ€์ ธ์˜ด
SRANDMEMBER key [count] ์ง‘ํ•ฉ์—์„œ ๋ฌด์ž‘์œ„๋กœ member๋ฅผ ์กฐํšŒ
SSCAN key cursor [MATCH pattern]
[COUNT count]
member๋ฅผ ์ผ์ • ๋‹จ์œ„ ๊ฐฏ์ˆ˜ ๋งŒํผ์”ฉ ์กฐํšŒ
SMISMEMBER key member [member ...] ์ง‘ํ•ฉ์— member๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ - ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€๋Šฅ
SLS key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์กฐํšŒ
SRM key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์‚ญ์ œ
SLEN key ํ‚ค์— ์†ํ•œ ๋ฉค๋ฒ„ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด
SADDS key (subquery) ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ member๋ฅผ ์ถ”๊ฐ€
sadd <key> <item>

# ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌ, ์žˆ์œผ๋ฉด 1 ์—†์œผ๋ฉด 0 ๋ฐ˜ํ™˜
sismember <key> <item>

# ์‚ญ์ œ
srem <key> <value>

# key์˜ ๋ชจ๋“  item ์กฐํšŒ
smembers <key>
127.0.0.1:6379> sadd myset a # ์ถ”๊ฐ€๋œ member ๊ฐฏ์ˆ˜ ๋ฐ˜ํ™˜
(integer) 1

127.0.0.1:6379> sadd myset a
(integer) 0

127.0.0.1:6379> sadd myset b
(integer) 1

127.0.0.1:6379> sadd myset c
(integer) 1

127.0.0.1:6379> srem myset c # ์‚ญ์ œ๋œ member ๊ฐฏ์ˆ˜ ๋ฐ˜ํ™˜
(integer) 1

127.0.0.1:6379> smembers myset
1) "b"
2) "a"

127.0.0.1:6379> scard myset
(integer) 2

127.0.0.1:6379> sadd myset c d e f # ์—ฌ๋Ÿฌ member ์‚ฝ์ž… ๊ฐ€๋Šฅ
(integer) 4

127.0.0.1:6379> smembers myset
1) "d"
2) "c"
3) "a"
4) "f"
5) "b"
6) "e"

127.0.0.1:6379> spop myset 3 # ๋žœ๋ค member ์‚ญ์ œ
1) "d"
2) "c"
3) "f"

127.0.0.1:6379> smembers myset
1) "a"
2) "b"
3) "e"

Redis - Sorted Sets

  • set์— score๋ผ๋Š” ํ•„๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ ๋ฐ์ดํ„ฐ ํ˜• (score๋Š” ์ผ์ข…์˜ ๊ฐ€์ค‘์น˜)
  • ์ผ๋ฐ˜์ ์œผ๋กœ set์€ ์ •๋ ฌ์ด ๋˜์–ด์žˆ์ง€์•Š๊ณ  insert ํ•œ ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด๊ฐ„๋‹ค.
    ๊ทธ๋Ÿฌ๋‚˜ Sorted Set์€ Set์˜ ํŠน์„ฑ์„ ๊ทธ๋Œ€๋กœ ๊ฐ€์ง€๋ฉฐ ์ถ”๊ฐ€์ ์œผ๋กœ ์ €์žฅ๋œ member๋“ค์˜ ์ˆœ์„œ๋„ ๊ด€๋ฆฌํ•œ๋‹ค. 
  • ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋ ๋•Œ๋ถ€ํ„ฐ score ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋ฉฐ ์ €์žฅ
  • sorted set์—์„œ ๋ฐ์ดํ„ฐ๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‚ด๋ถ€ ์ •๋ ฌ
  • value๋Š” ์ค‘๋ณต ๋ถˆ๊ฐ€๋Šฅ, score๋Š” ์ค‘๋ณต ๊ฐ€๋Šฅ
  • ๋งŒ์•ฝ score ๊ฐ’์ด ๊ฐ™์œผ๋ฉด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์ €์žฅ 
์œ ์ € ๋žญํ‚น ๋ณด๋“œ์„œ๋ฒ„ ๊ฐ™์€ ๊ตฌํ˜„์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Redis - Sorted Sets

 

Sorted Sets ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

  • SET: ZADD
  • GET: ZRANGE, ZRANGEBYSCORE, ZRANGEBYLEX, ZREVRANGE, ZREVRANGEBYSCORE, ZREVRANGEBYLEX, ZRANK, ZREVRANK, ZSCORE, ZCARD, ZCOUNT, ZLEXCOUNT, ZSCAN
  • POP: ZPOPMIN, ZPOPMAX
  • REM: ZREM, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZREMRANGEBYLEX
  • INCR: ZINCRBY
  • ์ง‘ํ•ฉ์—ฐ์‚ฐ: ZUNIONSTORE, ZINTERSTORE
  • Enterprise: ZISMEMBER, ZLS, ZRM, SLEN, SADDS (subquery)
Commands Syntax Description
ZADD key score member [score member ...] ์ง‘ํ•ฉ์— score์™€ member๋ฅผ ์ถ”๊ฐ€
ZCARD key ์ง‘ํ•ฉ์— ์†ํ•œ member์˜ ๊ฐฏ์ˆ˜๋ฅผ ์กฐํšŒ
ZINCRBY key increment member ์ง€์ •ํ•œ ๋งŒํผ score ์ฆ๊ฐ€, ๊ฐ์†Œ
ZRANGE key start stop [withscores] index๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ์กฐํšŒ
ZRANGEBYSCORE key min max [withscores]
[limit offset count]
score๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ์กฐํšŒ
ZREM key member [member ...] ์ง‘ํ•ฉ์—์„œ member๋ฅผ ์‚ญ์ œ
ZREMRANGEBYSCORE key min max score๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ member๋ฅผ ์‚ญ์ œ
ZREVRANGE key start stop [withscores] index๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ํฐ ๊ฒƒ๋ถ€ํ„ฐ ์กฐํšŒ
ZSCORE key member member๋ฅผ ์ง€์ •ํ•ด์„œ score๋ฅผ ์กฐํšŒ
ZCOUNT key min max score๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ๊ฐฏ์ˆ˜ ์กฐํšŒ
ZRANK key member member๋ฅผ ์ง€์ •ํ•ด์„œ rank(index)๋ฅผ ์กฐํšŒ
ZREVRANK key member member๋ฅผ ์ง€์ •ํ•ด์„œ reverse rank(index)๋ฅผ ์กฐํšŒ
ZREMRANGEBYRANK key start stop index๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ member๋ฅผ ์‚ญ์ œ
ZUNIONSTORE dest_key numkeys src_key [src_key ...]
[WEIGHTS weight [weight ...]]
[AGGREGATE SUM|MIN|MAX]
ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
ZINTERSTORE dest_key numkeys src_key [src_key ...]
[WEIGHTS weight [weight ...]]
[AGGREGATE SUM|MIN|MAX]
๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
ZREVRANGEBYSCORE key max min [withscores]
[limit offset count]
score๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ํฐ ๊ฒƒ๋ถ€ํ„ฐ ์กฐํšŒ
ZSCAN key cursor [MATCH pattern]
[COUNT count]
score, member๋ฅผ ์ผ์ • ๋‹จ์œ„ ๊ฐฏ์ˆ˜ ๋งŒํผ์”ฉ ์กฐํšŒ
ZRANGEBYLEX key min max [limit offset count] member๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ์กฐํšŒ
ZLEXCOUNT key min max member๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ๊ฐฏ์ˆ˜ ์กฐํšŒ
ZREMRANGEBYLEX key min max member๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ member๋ฅผ ์‚ญ์ œ
ZREVRANGEBYLEX key max min [limit offset count] member๋กœ ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•ด์„œ ํฐ ๊ฒƒ๋ถ€ํ„ฐ ์กฐํšŒ
ZPOPMIN key ์ž‘์€ ๊ฐ’๋ถ€ํ„ฐ ๊บผ๋‚ด์˜จ๋‹ค
ZPOPMAX key ํฐ ๊ฐ’๋ถ€ํ„ฐ ๊บผ๋‚ด์˜จ๋‹ค
BZPOPMIN key ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์ž‘์€ ๊ฐ’๋ถ€ํ„ฐ ๊บผ๋‚ด์˜จ๋‹ค
BZPOPMAX key ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ํฐ ๊ฐ’๋ถ€ํ„ฐ ๊บผ๋‚ด์˜จ๋‹ค
ZMSCORE member [member ...] member์˜ score๋ฅผ ๋ฆฌํ„ด - ์—ฌ๋Ÿฌ ๊ฐœ ๊ฐ€๋Šฅ
ZRANDMEMBER key ์ž„์˜(random)์˜ ๋ฉค๋ฒ„๋ฅผ ์กฐํšŒ
ZRANGESTORE dst src start stop ์กฐํšŒํ•ด์„œ ๋‹ค๋ฅธ ํ‚ค์— ์ €์žฅ
ZUNION numkeys key [key ...] ํ•ฉ์ง‘ํ•ฉ์„ ๊ตฌํ•จ
ZINTER numkeys key [key ...] ๊ต์ง‘ํ•ฉ์„ ๊ตฌํ•จ
ZDIFF numkeys key [key ...] ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•จ
ZDIFFSTORE destination numkeys key [key ...] ์ฐจ์ง‘ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์— ์ €์žฅ
ZISMEMBER key member ์ง‘ํ•ฉ์— member๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
ZLS key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์กฐํšŒ
ZRM key pattern ํŒจํ„ด(pattern)์œผ๋กœ ๊ฐ’(value) ์‚ญ์ œ
ZLEN key ํ‚ค์— ์†ํ•œ ๋ฉค๋ฒ„ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ด
ZADDS key (subquery) ์„œ๋ธŒ์ฟผ๋ฆฌ๋กœ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
# ZADD : key์— score-member๋ฅผ ์ถ”๊ฐ€
127.0.0.1:6379> zadd fruit 2 apple
(integer) 1

127.0.0.1:6379> zadd fruit 10 banana
(integer) 1

# ๋ณต์ˆ˜๊ฐœ์˜ score-member๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ
127.0.0.1:6379> zadd fruit 8 melon 4 orange 6 watermelon
(integer) 3

# ์ด๋ฏธ ์ถ”๊ฐ€ ๋œ member๋ฅผ add ์‹œ score๊ฐ€ ์—…๋ฐ์ดํŠธ
127.0.0.1:6379> zadd fruit 15 apple
(integer) 0

# ZSCORE : member์— ํ•ด๋‹นํ•˜๋Š” score ๊ฐ’ ๋ฆฌํ„ด
127.0.0.1:6379> zscore fruit apple
"15"

# ZRANK : member์— ํ•ด๋‹นํ•˜๋Š” rank(์ˆœ์œ„) ๊ฐ’ ๋ฆฌํ„ด
127.0.0.1:6379> zrank fruit melon
(integer) 2

# ZRANGE : key์— ํ•ด๋‹นํ•˜๋Š” start - stop ๋‚ด๊ฐ€ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ๋ฅผ ์ถ”์ถœ
127.0.0.1:6379> zrange fruit 0 -1
1) "orange"
2) "watermelon"
3) "melon"
4) "banana"
5) "apple"

Redis - HyperLogLogs

  • ๊ต‰์žฅํžˆ ๋งŽ์€์–‘์˜ ๋ฐ์ดํ„ฐ๋ฅผ dumpํ• ๋•Œ ์‚ฌ์šฉ
  • ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ countํ• ๋•Œ ์ฃผ๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. (์˜ค์ฐจ ๋ฒ”์œ„ 0.81%)
  • set๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์ €์žฅ๋˜๋Š” ์šฉ๋Ÿ‰์€ ๋งค์šฐ ์ž‘๋‹ค (์ €์žฅ ๋˜๋Š” ๋ชจ๋“  ๊ฐ’์ด 12kb ๊ณ ์ •)
  • ํ•˜์ง€๋งŒ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋Š” ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์—†๋‹ค. (๋ฐ์ดํ„ฐ ๋ณดํ˜ธ์— ์ ์ ˆ)
์—„์ฒญ ํฌ๊ณ  ์œ ๋‹ˆํฌ ํ•œ ๊ฐ’ ์นด์šดํŒ… ํ•  ๋•Œ ์‚ฌ์šฉ
→ ์›น ์‚ฌ์ดํŠธ ๋ฐฉ๋ฌธ ip ๊ฐœ์ˆ˜ ์นด์šดํŒ…, ํ•˜๋ฃจ ์ข…์ผ ํฌ๋กค๋ง ํ•œ url ๊ฐœ์ˆ˜ ๋ช‡๊ฐœ ์ธ์ง€, ๊ฒ€์ƒ‰ ์—”์ง„์—์„œ ๊ฒ€์ƒ‰ ํ•œ ๋‹จ์–ด ๋ช‡๊ฐœ ์ธ์ง€

 

HyperLogLog ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

Commands Syntax Description
PFADD key ele [ele ...] ์›์†Œ(element) ์ถ”๊ฐ€
PFCOUNT key [key ...] ์›์†Œ ๊ฐœ์ˆ˜ ์กฐํšŒ
PFMERGE destkey sourcekey [sourcekey ...] ์ง‘ํ•ฉ ๋จธ์ง€(Merge)
> PFADD crawled:20171124 "http://www.google.com/"
(integer) 1

> PFADD crawled:20171124 "http://www.redis.com/"
(integer) 1

> PFADD crawled:20171124 "http://www.redis.io/"
(integer) 1

> PFADD crawled:20171125 "http://www.redisearch.io/"
(integer) 1

> PFADD crawled:20171125 "http://www.redis.io/"
(integer) 1

> PFCOUNT crawled:20171124
(integer) 3

> PFMERGE crawled:20171124-25 crawled:20171124 crawled:20171125
OK

> PFCOUNT crawled:20171124-25
(integer) 4

Redis - HyperLogLogs


Redis - Streams

  • Streams๋Š” log๋ฅผ ์ €์žฅํ•˜๊ธฐ ๊ฐ€์žฅ ์ข‹์€ ์ž๋ฃŒ ๊ตฌ์กฐ
  • append-only ์ด๋ฉฐ ์ค‘๊ฐ„์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.
  • ์ฝ์–ด ์˜ฌ๋•Œ id๊ฐ’ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹œ๊ฐ„ ๋ฒ”์œ„๋กœ ๊ฒ€์ƒ‰
  • tail -f ์‚ฌ์šฉ ํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ์‹ ๊ทœ ์ถ”๊ฐ€ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
์†Œ๋น„์ž๋ณ„ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ (์†Œ๋น„์ž ๊ทธ๋ฃน) ๋“ฑ ํ™œ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

Streams ๋ช…๋ น์–ด ๋ฆฌ์ŠคํŠธ

Commands Syntax Description
XADD key ID field string [field string ...] ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
XLEN key  
XRANGE key start end [COUNT count]  
XREVRANGE key end start [COUNT count]  
XREAD [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...] ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ
XDEL key ID [ID ...] ๋ฐ์ดํ„ฐ ์‚ญ์ œ
XTRIM key MAXLEN [~] count ๋ฐ์ดํ„ฐ ์—ฌ๋Ÿฌ๊ฐœ ์‚ญ์ œ
XGROUP [CREATE key group id-or-$] [DESTROY key group] [DELCONSUMER key group consumer] [SETID key group id-or-$]  
XREADGROUP GROUP group consumer [COUNT count] [BLOCK milliseconds] STREAMS key [key ...] ID [ID ...]  
XACK key group ID [ID ...]  
XPENDING key group [start end count] [consumer]  
XCLAIM key group consumer min-idle-time ID [ID ...] [IDLE ms] [TIME ms-unix-time] [RETRYCOUNT count] [FORCE] [JUSTID]  
XAUTOCLAIM key group consumer min-idle-time start [COUNT count] [JUSTID]  
XINFO [CONSUMERS key group] [GROUPS key] [STREAM key] [HELP]  
>>> 127.0.0.1:6379> XADD customer * id "asdfdasf"
>>> "1647231099585-0"

>>> 127.0.0.1:6379> XADD customer * id "asdfdasf"
>>> "1647231112587-0"

>>> 127.0.0.1:6379> xlen customer
>>> (integer) 2

>>> 127.0.0.1:6379> xdel customer 1647231396280-0
>>> (integer) 1

Redis - Streams

XADD ์ปค๋งจ๋“œ๋ฅผ ์ด์šฉํ•ด mystream ์ด๋ผ๋Š” ํ‚ค์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.
* ๋ฌธ์ž๋Š” ์•„์ด๋””๊ฐ’์„ ์˜๋ฏธํ•˜๋ฉฐ ์•„์ด๋””๋ฅผ ์ง์ ‘ ์ง€์ •ํ• ์ˆ˜๋„ ์žˆ์ง€๋งŒ, * ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ•˜๋ฉด redis๊ฐ€ ์•Œ์•„์„œ ์ €์žฅํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ด์คŒ
๋ฐ˜ํ™˜๋œ ์•„์ด๋””๊ฐ’์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ์‹œ๊ฐ„์„ ์˜๋ฏธ

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ ์‚ฌ๋ก€

 

์‚ฌ๋ก€ 1 - ์ข‹์•„์š” ์ฒ˜๋ฆฌํ•˜๊ธฐ

๊ธฐ๋ณธ์ ์œผ๋กœ SNS์—์„œ ๊ฒŒ์‹œ๋ฌผ ์ข‹์•„์š” ์ฒ˜๋ฆฌ๋Š” ํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜๋‚˜์˜ ๋Œ“๊ธ€์— ์ข‹์•„์š”๋ฅผ ํ•œ๋ฒˆ์”ฉ๋งŒ ํ• ์ˆ˜ ์žˆ๋‹ค.

RDBMS์—์„œ ์œ ๋‹ˆํฌ ์กฐ๊ฑด์„ ๊ฑธ์–ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋ ‡๊ฒŒ insert์™€ update๊ฐ€ ์ž์ฃผ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ RDBMS ์„ฑ๋Šฅ ์ €ํ•˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ ˆ๋””์Šค์˜ Set์„ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋น ๋ฅธ ์‹œ๊ฐ„ ์•ˆ์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๋Œ“๊ธ€์˜ ๋ฒˆํ˜ธ๋ฅผ key๋กœ ํ•˜๊ณ , ํ•ด๋‹น ๋Œ“๊ธ€์— ์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅธ ํšŒ์› ID๋ฅผ ์•„์ดํ…œ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋ฉด ๋™์ผํ•œ ID๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ํ•œ ๋ช…์˜ ์‚ฌ์šฉ์ž๋Š” ํ•˜๋‚˜์˜ ๋Œ“๊ธ€์— ํ•œ๋ฒˆ ๋งŒ ์ข‹์•„์š”๋ฅผ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

 

 

์‚ฌ๋ก€ 2 - ์ผ์ผ ์ˆœ ๋ฐฉ๋ฌธ์ž์ˆ˜ ๊ตฌํ•˜๊ธฐ

์ˆœ ๋ฐฉ๋ฌธ์ž์ˆ˜ ๋ž€ ์„œ๋น„์Šค์— ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜๋ฃจ์— ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐฉ๋ฌธํ–ˆ๋”๋ผ๋„ ํ•œ๋ฒˆ๋งŒ ์นด์šดํŒ…๋˜๋Š” ๊ฐ’์ด๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์ค‘๋ณต ๋ฐฉ๋ฌธ์„ ์ œ๊ฑฐํ•œ ๋ฐฉ๋ฌธ์ž์˜ ์ง€ํ‘œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

์œ ์ €๋Š” ์ฒœ๋งŒ ๋ช…์ด๋ผ ๊ฐ€์ •ํ•˜๊ณ , ์ผ์ผ ๋ฐฉ๋ฌธ์ž ํšŸ์ˆ˜๋ฅผ ์ง‘๊ณ„ํ•˜๋ฉฐ ์ด ๊ฐ’์€ 0์‹œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ดˆ๊ธฐํ™” ๋œ๋‹ค๊ณ  ํ•˜์ž.

 

์‹ค์ œ ์„œ๋น„์Šค์—์„œ๋Š”  ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • Google Analytics์™€ ๊ฐ™์€ ์™ธ๋ถ€ ์„œ๋น„์Šค ์ด์šฉ
  • access log ๋ถ„์„
  • ์ ‘์† ์ •๋ณด๋ฅผ ๋กœ๊ทธํŒŒ์ผ๋กœ ์ž‘์„ฑํ•˜์—ฌ ๋ฐฐ์น˜ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

๊ทธ๋Ÿฌ๋‚˜ ๋ฐ‘์— ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

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

 

์‚ฌ์šฉ์ž ID๋Š” 0๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฆ๊ฐ€๋œ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ , string์˜ ๊ฐ bit๋ฅผ ํ•˜๋‚˜์˜ ์‚ฌ์šฉ์ž๋กœ ์ƒ๊ฐํ•ด ๋ณด์ž

๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์„œ๋น„์Šค์— ๋ฐฉ๋ฌธํ•  ๋•Œ ์‚ฌ์šฉ์ž ID์— ํ•ด๋‹นํ•˜๋Š” bit๋ฅผ 1๋กœ ์„ค์ •ํ•œ๋‹ค.

1๊ฐœ์˜ bit๊ฐ€ 1๋ช…์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ, ์ฒœ๋งŒ ๋ช…์˜ ์œ ์ €๋Š” ์ฒœ๋งŒ ๊ฐœ์˜ bit๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋Š” ๊ณง 1.2MB์ •๋„์˜ ํฌ๊ธฐ์ด๋‹ค.

๋ ˆ๋””์Šค string์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 512MB์ด๋ฏ€๋กœ ์ฒœ๋งŒ ๋ช…์˜ ์‚ฌ์šฉ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฑด ์ถฉ๋ถ„ํ•˜๋‹ค.

 

2020๋…„ 1์›” 29์ผ์— ID๊ฐ€ 7์ธ ์‚ฌ์šฉ์ž๊ฐ€ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ•˜์ž.

๊ทธ๋Ÿผ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ผ๊ณฑ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ 1๋กœ ์„ค์ •ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด ๋‚ ์— ์„œ๋น„์Šค์— ๋ฐฉ๋ฌธํ•œ ์ด๋ฐฉ๋ฌธ์ž ์ˆ˜๋ฅผ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด ๋ฌธ์ž์—ด์—์„œ 1๋กœ ์„ค์ •๋œ bit์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” BITCOUNT ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ• ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค..

Redis์˜ HyperLogLogs ํƒ€์ž…์œผ๋กœ ์ด์šฉํ•ด๋„ ๋œ๋‹ค.

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

์œ„ ๊ทธ๋ฆผ์„ ์„ค๋ช…ํ•˜์ž๋ฉด, ๋งŒ์ผ 15๋ช…์˜ ํšŒ์›์ด ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด ๋น„ํŠธ 15๊ฐœ๋ฅผ ์ค€๋น„ํ•˜๊ณ , ๊ทธ ํšŒ์›์ด ๋ฐฉ๋ฌธํ•˜๋ฉด ํšŒ์›์˜ id๋ฅผ ๊ตฌํ•ด ํ•ด๋‹น ์ˆœ์„œ ๋น„ํŠธ๋ฅผ 1๋กœ ๋ฐ”๊พธ๊ณ  ์นด์šดํŠธ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

 

์‚ฌ๋ก€ 3 - ์ถœ์„ ์ด๋ฒคํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ

์ •ํ•ด์ง„ ๊ธฐ๊ฐ„๋™์•ˆ ๋งค์ผ ๋ฐฉ๋ฌธํ•œ ์‚ฌ์šฉ์ž๋ฅผ ๊ตฌํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

์‚ฌ๋ก€ 2๋ฒˆ์„ ์ด์šฉํ•˜์—ฌ, ๊ตฌํ•ด๋†“์€ string ๊ฐ„์˜ ๋น„ํŠธ๋ฅผ ๋น„๊ตํ•˜๋Š” BITOP ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด ์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2020๋…„ 1์›” 29์ผ๋ถ€ํ„ฐ 31์ผ๊นŒ์ง€ ๋งค์ผ ์ ‘์†ํ•œ ์‚ฌ์šฉ์ž๋Š” id๊ฐ€ 7์ธ ์‚ฌ์šฉ์ž์™€ 11์ธ ์‚ฌ์šฉ์ž๋ผ๋Š” ๊ฒƒ์„ BITOP์„ ์ด์šฉํ•œ AND ์—ฐ์‚ฐ์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

BITOP AND๋กœ ๋น„ํŠธ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๊ฐ’์ด 1์ด ๋‚˜์˜ค๋ฉด ๊ทธ ํšŒ์›์€ ๋งค์ผ ๋ฐฉ๋ฌธ ํ•œ๋‹ค๋Š” ๋œป์ด๋œ๋‹ค.

 

 

์‚ฌ๋ก€ 4 - ์ตœ๊ทผ ๊ฒ€์ƒ‰ ๋ชฉ๋ก ํ‘œ์‹œํ•˜๊ธฐ

์‚ฌ๋‚ด ํ˜‘์—…๋„๊ตฌ์˜ ํ”„๋กœ์ ํŠธ์—์„œ ๋‹ด๋‹น์ž๋ฅผ ์„ ํƒํ•  ๋•Œ, ๋งค๋ฒˆ ๋ฉค๋ฒ„๋ฅผ ๊ฒ€์ƒ‰ํ•ด์„œ ์ž…๋ ฅํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๋‚ด๊ฐ€ ์ตœ๊ทผ ๊ฒ€์ƒ‰ํ–ˆ๋˜ ๋‹ด๋‹น์ž๊ฐ€ ๋“œ๋กญ๋‹ค์šด ๋ฉ”๋‰ด๋กœ ๋ณด์—ฌ์ง€๊ฒŒ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค.

redis์ตœ๊ทผ ๊ฒ€์ƒ‰ ๋ชฉ๋ก ํ‘œ์‹œํ•˜๊ธฐ

๋งŒ์ผ IDํ•„๋“œ๊ฐ€ 123 ์‚ฌ์šฉ์ž(๋‚˜) ๊ฐ€ ์ตœ๊ทผ ๊ฒ€์ƒ‰ํ•œ ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด, RDBMS๋กœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค์•ผ ํ•œ๋‹ค.

select * from KEYWORD 
where ID = 123 
order by reg_date desc 
limit 5;

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

 

๋”ฐ๋ผ์„œ ์• ์ดˆ์— ์ค‘๋ณต์ด ๋˜์ง€ ์•Š๊ณ , ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ๋ ˆ๋””์Šค์˜ sorted set ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

sorted set์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ€์ค‘์น˜๋กœ ์‹œ๊ฐ„์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ด ๊ฐ’์ด ๊ฐ€์žฅ ํฐ, ๋‚˜์ค‘์— ์ž…๋ ฅ๋œ ์•„์ดํ…œ์ด ๋งจ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ์ €์žฅ๋œ๋‹ค.

 

๋ฉค๋ฒ„ ID๊ฐ€ 123์ธ ์‚ฌ๋žŒ์ด ์ตœ๊ทผ ๊ฒ€์ƒ‰ํ•œ ์‚ฌ๋žŒ์€ ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ์ •๋ ฌ๋˜์–ด ์ €์žฅ๋œ๋‹ˆ.

์ด๋•Œ ๊ฐ€์ค‘์น˜(score)๋Š” ์ž…๋ ฅ ์ˆœ๊ฐ„์˜ ๋‚˜๋…ธ์‹œ๊ฐ„์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ, ๊ฐ€์žฅ ์ฒ˜์Œ ๊ฒ€์ƒ‰ํ•œ ์‚ฌ๋žŒ์˜ ID๋Š” 46, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฒ€์ƒ‰ํ•œ ์‚ฌ๋žŒ์€ 50์ด ๋˜๊ฒŒ ๊ตฌ์„ฑํ•œ๋‹ค.

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

 

๊ทธ๋Ÿฌ๋ฉด ๋งŒ์ผ ๋‚ด๊ฐ€ ID๊ฐ€ 51์ธ ์‚ฌ๋žŒ์„ ๊ฒ€์ƒ‰ํ•˜๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋งˆ์ง€๋ง‰์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋˜๊ฒŒ ๋œ๋‹ค.

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

 

๋‹จ, ํ•ญ์ƒ ๋‹ค์„ฏ ๋ช…๋งŒ ์ €์žฅํ•œ๋‹ค๊ณ  ์„ค๊ณ„ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ง€์›Œ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ฆผ์—์„œ ๋ณด๋ฉด 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์ง€์šฐ๋ฉด ๋˜๊ธฐ๋Š” ํ•˜๋Š”๋ฐ, ์•„์ดํ…œ ๊ฐœ์ˆ˜๊ฐ€ 6๋ณด๋‹ค ์ž‘์„ ๋•Œ์—๋Š” 0๋ฒˆ์งธ ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•˜๋ฉด ์•ˆ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋งค๋ฒˆ ์•„์ดํ…œ์˜ ์ˆ˜๋ฅผ ๋จผ์ € ํ™•์ธํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ๊ฒŒ ๋œ๋‹ค.

 

์ด๋•Œ sorted set์˜ ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

์Œ์ˆ˜ ์ธ๋ฑ์Šค๋Š” ์ธ๋ฑ์Šค์˜ ๋งˆ์ง€๋ง‰๋ถ€ํ„ฐ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ž‘์€ ๊ฐ’์œผ๋กœ ๋งค๊ฒจ์ง€๋Š”๋ฐ, ๋ฐ์ดํ„ฐ์— ๋ฉค๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•œ ๋’ค, ํ•ญ์ƒ -6๋ฒˆ์งธ ์•„์ดํ…œ์„ ์ง€์šด๋‹ค๋ฉด ํŠน์ • ๊ฐœ์ˆ˜ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

์ธ๋ฑ์Šค๋กœ ์•„์ดํ…œ์„ ์ง€์šฐ๋ ค๋ฉด ZREMRANGEBYRANK ์ปค๋งจ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

> ZREMRANGEBYRANK recent:member:123 -6 -6

Redis ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

์ด๋ ‡๊ฒŒ ๋ฐ์ดํ„ฐ์— ๋ฉค๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•œ ๋’ค, ํ•ญ์ƒ -6๋ฒˆ์งธ ์•„์ดํ…œ์„ ์ง€์šด๋‹ค๋ฉด ํŠน์ • ๊ฐœ์ˆ˜ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.


# ์ฐธ๊ณ ์ž๋ฃŒ

https://meetup.toast.com/posts/225

http://redisgate.kr/redisgate/ent/ent_intro.php

[NHN FORWARD 2021] Redis ์•ผ๋ฌด์ง€๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

https://redis.com/ebook/redis-in-action/