TTL for a set member

RedisSetTaggingTtl

Redis Problem Overview


Is it possible in Redis to set TTL (time to live) not for a specific key, but for a member for a set?

I am using a structure for tags proposed by Redis documentation - the data are simple key-value pairs, and the tags are sets containing keys corresponding to each tag, e.g.

> SETEX id:id_1 100 'Lorem ipsum'
OK
> SADD tag:tag_1 id:id_1
(integer) 1

The key id:id_1 will expire as expected but i don't see an efficient way to remove the corresponding member from the tag:tag_1 set.

One way I came up is using a cron job containing a script which would remove expired keys from sets periodically - by adding all the tag names to another set and then iterating through all the tags, then all the ids corresponding to each tag and checking if corresponding key exists - if not, calling SREM.

I don't think it will be an efficient way and I would possibly like to keep the tags as clean as possible, because the size of the sets will probably affect performance of searching by multiple tags (SINTER). Is there a more "internal" way?

Redis Solutions


Solution 1 - Redis

No, this isn’t possible (and not planned either). The recommended approach is to use an ordered set with score set to timestamp and then manually removing expired keys. To query for non-expired keys, you can use ZRANGEBYSCORE $now +inf, to delete expired keys, ZREMRANGEBYSCORE -inf $now will do the trick.

In my application, I simply issue both commands every time I query the set. I also combine this with (long) expiration time on the set itself to eventually purge unused sets.

This article walks through it in more detail.

Solution 2 - Redis

It is not possible to directly expire items in list, sets, or zsets.

You need to implement a mechanism to be notified when the master item expires, so that you can maintain the corresponding sets accordingly.

See the answer to this question, I think it applies to your use case (replace session by id, and user by tag):

https://stackoverflow.com/questions/16741476/redis-session-expiration-and-reverse-lookup/16747795#16747795

Solution 3 - Redis

You can not expire items in a list directly, but it's possible to register a lua script in your redis server and add this functionality to redis. https://stackoverflow.com/questions/55307425/redis-expiration-based-on-value

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrzemekView Question on Stackoverflow
Solution 1 - RedisVáclav SlavíkView Answer on Stackoverflow
Solution 2 - RedisDidier SpeziaView Answer on Stackoverflow
Solution 3 - RedisMiluxView Answer on Stackoverflow