Notification of key expiration in redis python

Each Answer to this Q is separated by one/two green lines.

I would like to be notified when a volatile key expires in my redis store. The redis website provides some description of how this might be achieved in, but I am wondering if it can be done using the python redis api.

After setting:notify-keyspace-events Ex in my redis.conf file

and running this as a test:

import redis
import config

client = redis.StrictRedis.from_url(config.REDIS_URI) 
client.set('a', 1)

callback() only gets called when client.expire('a',5) gets called, but not five seconds later as expected

The surprise (no expiration events seen when time to live for a key reaches zero) is not bound to Python, but rather to the way, Redis is expiring keys.

Redis doc on Timing of expired events

Timing of expired events

Keys with a time to live associated are expired by Redis in two ways:

  • When the key is accessed by a command and is found to be expired.
  • Via a background system that looks for expired keys in background, incrementally, in order to be able to also collect keys that are never accessed.

The expired events are generated when a key is accessed and is found to be expired by one of the above systems, as a result there are no guarantees that the Redis server will be able to generate the expired event at the time the key time to live reaches the value of zero.

If no command targets the key constantly, and there are many keys with a TTL associated, there can be a significant delay between the time the key time to live drops to zero, and the time the expired event is generated.

Basically expired events are generated when the Redis server deletes the key and not when the time to live theoretically reaches the value of zero.

Small test on console

when Redis running ($ sudo service redis-server start)

I started one console and have subscribed:

$ redis-cli
PSUBSCRIBE "__key*__:*"

Then, in another console:

$ redis-cli
> config set notify-keyspace-events AKE

what shall subscribe to all kinds of events

Then I continued with experiments in this second console:

> set aaa aaa
> del aaa
> set aaa ex 5
> get aaa

All the activities were seen in subscribed console. Only the key expiration was sometime few seconds delayed, sometime came just in time.

Note alse, there are subtle differences in messages, one message [email protected]__:expire another [email protected]__:expired.

Sample listener

import redis
import time

r = redis.StrictRedis()
pubsub = r.pubsub()
for msg in pubsub.listen():
    print time.time(), msg

This code registers to all existing channels in default redis and prints whatever gets published.

Run it:

$ python

and in another console try to set a key with an expiration. You will see all the events.

For following redis-cli input.

$ redis-cli> set a aha
OK> set b bebe ex 3
OK> set b bebe ex 3

we get spy output:

1401548400.27 {'pattern': None, 'type': 'psubscribe', 'channel': '*', 'data': 1L}
1401548428.36 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:a', 'data': 'set'}
1401548428.36 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:set', 'data': 'a'}
1401548436.8 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'set'}
1401548436.8 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:set', 'data': 'b'}
1401548436.8 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'expire'}
1401548436.8 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:expire', 'data': 'b'}
1401548439.82 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'expired'}
1401548439.82 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:expired', 'data': 'b'}
1401548484.46 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'set'}
1401548484.46 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:set', 'data': 'b'}
1401548484.46 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'expire'}
1401548484.46 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:expire', 'data': 'b'}
1401548487.51 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:b', 'data': 'expired'}
1401548487.51 {'pattern': '*', 'type': 'pmessage', 'channel': '[email protected]__:expired', 'data': 'b'}

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .