my redis keys do not expire
Asked Answered
B

1

19

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.

Blunger answered 7/8, 2012 at 15:26 Comment(0)
S
22

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.

Symposiac answered 7/8, 2012 at 15:31 Comment(5)
Thanks for your so quick reply. The '...' in my snippet only meant that I wait 20s until the TTL returns -1. I don't type any command here. So the expiration should go til its end, no?Blunger
thanks Joachim! That is exactly the point, I unintentionally set my key on the slave instead of the master, which is inconsistent. If i switch to my master everything works fine.Blunger
@JoachimIsaksson "TTL in seconds or -1 when key does not exist or does not have a timeout." Does it not return -2 if the key does not exist?Streeto
@Djack Apparently the behavior changed after my answer was written, added an edit with the new behavior, thanks.Symposiac
I also faced similar issue once I create keys on slave. In my case redis TTL was giving 0 and it was not deleting the key. My redis version is 2.8Turnout

© 2022 - 2024 — McMap. All rights reserved.