10-14 13:32
- Today
- Total
Link
개발하는 고라니
Redis Keyspace Notification playground 본문
반응형
Local 환경 keyspace notification
기본적으로 Redis는 성능을 이유로 이 설정이 off되어있다.
레디스에서 발생하는 이벤트를 발행하고 받으려면 아래의 설정값을 주어야한다.
notify-keyspace-events
이를 설정하는 2가지 방법을 간단하게 보자.
방법 1: redis-cli 로 즉시 적용
로컬이나 개발 환경에서 빠르게 테스트할 때 유용합니다.
redis-cli로 접속하여 간단히 즉시 적용할 수 있으나, redis 서버를 재시작하면 휘발됩니다.
$ redis-cli
# 'E'는 키스페이스 이벤트, 'x'는 만료 이벤트를 의미합니다.
> CONFIG SET notify-keyspace-events Ex
# 설정값 조회
> CONFIG GET notify-keyspace-events
방법 2: redis.conf 파일에 영구 설정 -> 재시작
운영 환경에서는 설정 파일에 직접 추가하여 서버가 재시작되어도 설정이 유지되도록 해야 합니다.
# redis.conf
notify-keyspace-events Ex
그런데 설정 값을 "Ex"로 주었다. 이것이 무엇을 의미할까?
설정값 별 의미
redis 공식 문서에 자세히 나와있습니다.
K Keyspace events, published with __keyspace@<db>__ prefix.
E Keyevent events, published with __keyevent@<db>__ prefix.
g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
$ String commands
l List commands
s Set commands
h Hash commands
z Sorted set commands
t Stream commands
d Module key type events
x Expired events (events generated every time a key expires)
e Evicted events (events generated when a key is evicted for maxmemory)
m Key miss events generated when a key that doesn't exist is accessed (Note: not included in the 'A' class)
n New key events generated whenever a new key is created (Note: not included in the 'A' class)
o Overwritten events generated every time a key is overwritten (Note: not included in the 'A' class)
c Type-changed events generated every time a key's type changes (Note: not included in the 'A' class)
A Alias for "g$lshztdxe", so that the "AKE" string means all the events except "m", "n", "o" and "c".
K, E 는 각각 keyspace, keyevent를 의미한다고 하는데, 이게 무엇일까? AI는 아래와 같이 알려주었습니다.
저는 특정 패턴의 레디스 키(Keyevent, E)에 관심이 있고, 만료(Expired, x)되었을 때 이벤트를 받고싶으므로 "Ex" 로 설정하였습니다.
구분 | Keyevent (추천) | Keyspace (부적절) |
구독 대상 | __keyevent@*__:expired (채널 1개) | __keyspace@*__:waiting:called:* (N개의 키) |
처리 방식 | 이벤트 종류를 먼저 정하고, 어떤 키인지 나중에 필터링 | 키를 먼저 정하고, 어떤 이벤트인지 나중에 필터링 |
효율성 | 매우 효율적 | 비효율적이고 구현이 복잡함 |
Run command in redis-cli
1. keyspace notifications 설정을 ON 합니다.
# redis-1
> CONFIG SET notify-keyspace-events Ex
2. 다른 redis session을 열어 keyevent 만료 이벤트에 대해 구독합니다.
# redis-2
> SUBSCRIBE __keyevent@0__:expired
3.”order:created:1” key로 아무 값이나 넣어 TTL을 5s 로 삽입합니다.
# redis-1
> SET order:created:1 true EX 5
4.키가 만료되면 출력되는 이벤트를 확인합니다.
# redis-2
> SUBSCRIBE __keyevent@0__:expired
1) "subscribe" -- listening 명령어에 대한 결과 출력
2) "__keyevent@0__:expired"
3) (integer) 1
1) "message" -- 만료 이벤트 발생에 대한 출력
2) "__keyevent@0__:expired"
3) "order:created:1"
반응형
'Restart' 카테고리의 다른 글
Delayed Event(지연 이벤트) (3) | 2025.10.12 |
---|
Comments