How to solve the problem of storing empty set?

So I primarily make use of Redis sets for a lot of use cases. Where I primarily sort of want to maintain the elements. But as Redis does not allow to store empty sets it becomes tricky.

Similar thread on GitHub here:

Wanted to start this thread to get a view of how others have been solving such a use case?

Is it similar to what’s mentioned in the GitHub issue, or some other ways?


Redis is key-value data store, every key you need to store a value. we simply choose to prefer client side logic if it returns (empty list or set)

redis> smembers my
(empty list or set)

better thoughts?

@suyog My concern here was an empty set , is a valid value for my case that i want to put in cache.

As redis indicates this as an empty set, i.e. i cannot say that is it empty or not defined on the first place.

Hence that becomes tricky.

1 Like

There are a few ways to do circumvent this from the application, for example: You could build an index of keys that shows that a redis set has values (or ready for use). Prior to getting the values, you’d have to check from this index first.

Another way is to create a dummy value that your application would ignore during loading, for example inserting a empty string “” as the first object.

It sounds like the best approach is to just add another layer in to track your keys. Anytime you add a key-value, add the key to your used key data structure. If the key exists here, then (empty list or set) is a legit value. If the key doesn’t exist here, then the (empty list or set) return is really undefined. I’m probably missing something, but that seems like the best approach without odd hacks to get around a foundational aspect of redis.