my redis keys do not expire

26.5k views Asked by At

My redis server does not delete keys when the time-to-live reaches 0.

Here is a sample code:

redis-cli
>SET mykey "ismykey"
>EXPIRE mykey 20
#check TTL
>TTL mykey
>(integer) 17
> ...
>TTL mykey
>(integer) -1
#mykey chould have expired:
>EXISTS mykey
>(integer) 1
>#oh still there, check its value
>GET mykey
>"ismykey"

If i check the info return by redis, it says 0 keys were expired.

Any idea?

thanks.

1

There are 1 answers

5
Joachim Isaksson On BEST ANSWER

Since you're doing a '...' it's hard to say for sure, but I'd say you're setting mykey during that part, which will effectively remove the expiration.

From the EXPIRE manual

The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands

Also, regarding the -1 reply from TTL

Return value

Integer reply: TTL in seconds or -1 when key does not exist or does not have a timeout.

EDIT: Note that this behaviour changed in Redis 2.8

Starting with Redis 2.8 the return value in case of error changed:
The command returns -2 if the key does not exist.
The command returns -1 if the key exists but has no associated expire.

In other words, if your key exists, it would seem to be persistent, ie not have any expiration set.

EDIT: It seems I can reproduce this if I create the key on a REDIS slave server, the slave will not delete the key without master input, since normally you would not create keys locally on a slave. Is this the case here?

However while the slaves connected to a master will not expire keys independently (but will wait for the DEL coming from the master), they'll still take the full state of the expires existing in the dataset, so when a slave is elected to a master it will be able to expire the keys independently, fully acting as a master.