Chris
Chris

Reputation: 392

A passive way of the Redis Expires

I have a question while I'm studying about Redis with docs.
Here is the description in official Redis document about expiration : https://redis.io/commands/expire

Specifically this is what Redis does 10 times per second:
1. Test 20 random keys from the set of keys with an associated expire.
2. Delete all the keys found expired.
3. If more than 25% of keys were expired, start again from step 1.

After I read this, I have a question about 25% of keys above.
What is the keys meaning in 25% of keys exactly?
The keys are the all of keys in Redis? or 20 random keys in description above?

I think the keys are looks like the all of keys in Redis.
I couldn't find any description about this in document.

Thank you.

Upvotes: 1

Views: 285

Answers (1)

Ryan Walker
Ryan Walker

Reputation: 3286

By default, a key in Redis does not have a time to live (ttl) set. The 20 random keys in this algorithm are sampled from the set of all keys where an explicit ttl has been set. If more than 5 of the keys in this sample have expired then it's likely that the portion of expired keys in Redis is high, and so the algorithm repeats to try to drive the portion of expired keys down below the threshold.

It's important also to note that keys that have lived beyond their ttl will be expired automatically on access (meaning if the user tries to request them). So a heuristic algorithm like this simply needs to ensure that Redis doesn't pile up a bunch of expired keys, while making sure that Redis is not spending too many resources cleaning up expired keys.

See the source code: https://github.com/antirez/redis/blob/unstable/src/expire.c

Upvotes: 1

Related Questions