Redis

내가 보려고 정리하는 Redis 자료구조 (Strings / Hashes / Sets / Sorted Sets / Lists)

Luti 2026. 1. 24. 06:26

 

Strings

텍스트, 직렬화된 객체, 바이너리 배열을 포함한 바이트 시퀀스를 저장하는 자료구조

Redis 키에 연결할 수 있는 가장 간단한 값 유형

활용

  • 캐시
  • 카운터
  • 토큰

command

SET & GET

  • `SET A B`
    • A 키에 대한 문자열 B 값 설정
  • `SET A B nx`
    • A 키가 있을 경우에만 SET
  • `MSET A a B b C c`
    • A → a, B → b, C → c 한 번에 삽입
  • `GET A`
    • A 키에 대한 문자열 Value 반환
  • `MGET A B C`
    • A, B, C 키에 대한 문자열 Value 한 번에 반환
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> set hello redis nx
(nil)
127.0.0.1:6379> set hi redis nx
OK
127.0.0.1:6379> get hi
"redis"
127.0.0.1:6379> set love redis xx
(nil)
127.0.0.1:6379> set hello redis xx
OK
127.0.0.1:6379> get hello
"redis"
127.0.0.1:6379> mset A a B b C c
OK
127.0.0.1:6379> mget A B C
1) "a"
2) "b"
3) "c"

 

INCR & INCRBY

  • `INCR A`
    • A 키에 저장된 카운터를 원자적으로 1 증가
  • `INCRBY A B`
    • A 키에 저장된 카운터를 B 만큼 원자적으로 증가
127.0.0.1:6379> set counter 0
OK
127.0.0.1:6379> incr counter
(integer) 1
127.0.0.1:6379> incr counter
(integer) 2
127.0.0.1:6379> incrby counter 10
(integer) 12
127.0.0.1:6379> incrby counter 100
(integer) 112
127.0.0.1:6379>

 

Hashes

필드-값 쌍의 컬렉션으로 구조화된 레코드 유형

해시를 사용하여 기본 객체를 표현하거나 카운터 저장하는 등 다양한 용도로 사용 가능

활용

  • 객체 표현
  • 부분 업데이트

Command

HSET & HGET

  • `HSET key A a B b C c`
    • key 키에 대해 A → a, B → b, C → c 삽입
  • `HGET key A`
    • key 키에 대한 A 필드의 값 조회
  • `HGETALL key`
    • key 키에 대한 모든 필드와 값 조회
127.0.0.1:6379> hget Pikachu Number 25  Type Electric Generation 1
(integer) 3
127.0.0.1:6379> hget Pikachu Type
"Electric"
127.0.0.1:6379> hgetall Pikachu
1) "Number"
2) "25"
3) "Type"
4) "Electric"
5) "Generation"
6) "1"

 

HMGET

  • `HMGET key A B C`
    • key 키에 대한 A, B, C 필드 각각의 값 조회
127.0.0.1:6379> hmget Pikachu Number Type Generation
1) "25"
2) "Electric"
3) "1"
127.0.0.1:6379>

 

HINCRBY

  • `HINCRBY key A a`
    • key 키에 대한 A 필드의 값을 a 만큼 증가
127.0.0.1:6379> hincrby Pikachu Number 100
(integer) 125
127.0.0.1:6379> hget Pikachu Number
"125"

 

Sets

고유한 멤버들의 정렬되지 않은 자료구조

활용

  • 방문 여부
  • 유니크 체크

Command

SADD

  • `SADD A a b c`
    • A 키에 멤버 a, b, c SET
127.0.0.1:6379> sadd MyPokemon Pikachu
(integer) 1
127.0.0.1:6379> sadd MyPokemon Pikachu
(integer) 0
127.0.0.1:6379> sadd MyPokemon Arcanine Lapras Diglett
(integer) 3

 

SISMEMBER

  • `SISMEMBER A a`
    • A 키에 대해 a가 멤버에 존재하는지 확인
127.0.0.1:6379> sismember MyPokemon Pikachu
(integer) 1
127.0.0.1:6379> sismember MyPokemon Charmander
(integer) 0

 

SREM

  • `SREM A a`
    • A 키에 대해 멤버 a 제거
127.0.0.1:6379> sismember MyPokemon Pikachu
(integer) 1
127.0.0.1:6379> srem MyPokemon Pikachu
(integer) 1
127.0.0.1:6379> sismember MyPokemon Pikachu
(integer) 0

 

SINTER

  • `SINTER A B`
    • A, B 키에 대한 공통 멤버 반환
127.0.0.1:6379> sadd pokemon:gen1 Pikachu Bulbasaur Charmander Squirtle
(integer) 4
127.0.0.1:6379> sadd pokemon:electric Pikachu Raichu
(integer) 2
127.0.0.1:6379> sinter pokemon:gen1 pokemon:electric
1) "Pikachu"

 

SCARD

  • `SCARD A`
    • A 키에 SET의 크기 반환
127.0.0.1:6379> scard pokemon:gen1
(integer) 4

 

Sorted Sets (ZSets)

점수에 따라 정렬된 고유한 멤버들을 담는 자료구조

활용

  • 랭킹
  • 정렬

Command

ZADD

  • `ZADD key a A`
    • key 키에 a 점수를 붙여 A 멤버 삽입
127.0.0.1:6379> zadd pokemon:pokedex 1 Bulbasaur
(integer) 1
127.0.0.1:6379> zadd pokemon:pokedex 4 Charmander
(integer) 1
127.0.0.1:6379> zadd pokemon:pokedex 7 Squirtle
(integer) 1
127.0.0.1:6379> zadd pokemon:pokedex 25 Pikachu
(integer) 1

 

ZRANGE & ZREVRANGE

  • `ZRANGE key a b`
    • key 세트의 인덱스 a 부터 b 까지의 멤버를 오름차순으로 반환
  • `ZRANGE key 0 -1`
    • key 세트의 모든 멤버를 오름차순으로 반환
  • `ZRANGE key a b WITHSCORES`
    • key 세트의 인덱스 a 부터 b 까지의 멤버를 점수와 함께 오름차순으로 반환
  • `ZREVRANGE key a b`
    • key 세트의 인덱스 a 부터 b 까지의 멤버를 내림차순으로 반환
  • `ZREVRANGE key 0 1`
    • key 세트의 모든 멤버를 내림차순으로 반환
  • `ZREVRANGE key a b WITHSCORES`
    • key 세트의 인덱스 a 부터 b 까지의 멤버를 점수와 함께 내림차순으로 반환
127.0.0.1:6379> zrange pokemon:pokedex 1 2
1) "Charmander"
2) "Squirtle"
127.0.0.1:6379> zrange pokemon:pokedex 0 -1
1) "Bulbasaur"
2) "Charmander"
3) "Squirtle"
4) "Pikachu"
127.0.0.1:6379> zrange pokemon:pokedex 0 2 withscores
1) "Bulbasaur"
2) "1"
3) "Charmander"
4) "4"
5) "Squirtle"
6) "7"
127.0.0.1:6379> zrevrange pokemon:pokedex 0 1
1) "Pikachu"
2) "Squirtle"
127.0.0.1:6379> zrevrange pokemon:pokedex 0 -1
1) "Pikachu"
2) "Squirtle"
3) "Charmander"
4) "Bulbasaur"
127.0.0.1:6379> zrevrange pokemon:pokedex 0 1 withscores
1) "Pikachu"
2) "25"
3) "Squirtle"
4) "7"

 

ZRANGESBYSCORE

  • `ZRANGEBYSCORE key a b`
    • key 세트에 대해 점수가 a에서 b까지의 모든 멤버 반환
127.0.0.1:6379> zrangebyscore pokemon:pokedex 4 10
1) "Charmander"
2) "Squirtle"

 

ZREM & ZREMRANGEBYSCORE

  • `ZREM key A`
    • key 세트에 대해 A 멤버 제거
  • `ZREMRANGEBYSCORE key a b`
    • key 세트에 대해 점수가 a, b 사이인 멤버들 제거
127.0.0.1:6379> zremrangebyscore pokemon:pokedex 1 5
(integer) 2
127.0.0.1:6379> zrange pokemon:pokedex 0 -1
1) "Squirtle"
127.0.0.1:6379>

 

ZRANK & ZREVRANK

  • `ZRANK key A`
    • key 세트에 대해 A 멤버의 오름차순 점수 순위 반환
  • `ZREVRANK key A`
    • key 세트에 대해 A 멤버의 내림차순 점수 순위 반환
127.0.0.1:6379> zadd pokemon:pokedex 1 Bulbasaur 4 Charmander 7 Squirtle 25 Pikachu 143 Snorlax
(integer) 4
127.0.0.1:6379> zrank pokemon:pokedex Charmander
(integer) 1
127.0.0.1:6379> zrevrank pokemon:pokedex Snorlax
(integer) 0
127.0.0.1:6379>

 

Lists

문자열 값들의 연결 리스트

활용

  • 작업 큐
  • 순서 있는 데이터

Command

LPUSH

  • `LPUSH key A`
    • 키의 왼쪽(앞)에 A 멤버 삽입
127.0.0.1:6379> lpush pokemon:list Pikachu
(integer) 1
127.0.0.1:6379> lpush pokemon:list Charmander
(integer) 2
127.0.0.1:6379> lpush pokemon:list Bulbasaur
(integer) 3

 

LPOP

  • `LPOP key`
    • 키의 왼쪽(앞)에서 멤버 하나를 꺼내고 제거
127.0.0.1:6379> lpop pokemon:list
"Bulbasaur"

 

LLEN

  • `LLEN key`
    • 키에 저장된 리스트의 전체 길이를 반환
127.0.0.1:6379> llen pokemon:list
(integer) 2

 

LMOVE

  • `LMOVE source destination LEFT|RIGHT LEFT|RIGHT`
    • 원본 리스트의 한쪽 끝에서 요소를 꺼내 대상 리스트의 한쪽 끝으로 이동
127.0.0.1:6379> lpush pokemon:captured Squirtle
(integer) 1
127.0.0.1:6379> lmove pokemon:list pokemon:captured left right
"Charmander"

 

LRANGE

  • `LRANGE key a b`
    • 리스트의 인덱스 a 부터 b 까지의 멤버를 반환
127.0.0.1:6379> lrange pokemon:list 0 -1
1) "Pikachu"

 

LTRIM

  • `LTRIM key a b`
    • 리스트를 인덱스 a 부터 b 까지의 범위만 남기고 나머지는 모두 제거
127.0.0.1:6379> lpush pokemon:history Pikachu Charmander Squirtle Bulbasaur
(integer) 4
127.0.0.1:6379> ltrim pokemon:history 0 1
OK
127.0.0.1:6379> lrange pokemon:history 0 -1
1) "Bulbasaur"
2) "Squirtle"