Monitor key deletion and expiry events with keyspace notifications
RedisMaintenanceCommand
CONFIG GET notify-keyspace-events
# E: keyevent channel, x: expire events, g: generic commands (DEL, EXPIRE, RENAME):
CONFIG SET notify-keyspace-events Exg
# Listen to expire and del events in db 0:
redis-cli -h redis1 -p 6379 --csv PSUBSCRIBE '__keyevent@0__:expired' '__keyevent@0__:del'
# Turn off:
CONFIG SET notify-keyspace-events ""What it does
Publishes events on keys (expire, del, set, etc.) to pub/sub channels so you can watch live which key was deleted or expired and when.
When to use it
To prove which command or expiry removes keys when they 'disappear on their own', or to feed workflows that depend on expiry events (session close).
Caution
Events add CPU and network load; 'A' (all classes) is especially expensive on busy systems, so enable only the classes you need and turn them off afterwards. Pub/sub is fire-and-forget: a subscriber that is not connected misses the event, and nothing is persisted. The expired event is generated when the key is actually deleted (on access or by the background expire cycle), not at the TTL moment.