Allan
Allan

Reputation: 1715

How to quickly retrieve only a subset of a Set in Redis

I have need to store an unordered set of items in a manner that allows for fast

Redis seems like a great candidate for this kind of storage, but as I read the docs, there's not one data type that fits this perfectly. Having a SUBSET command for the Set type would be perfect.

What's the best way to store and query this kind of data structure?

Upvotes: 1

Views: 1541

Answers (1)

Ted Naleid
Ted Naleid

Reputation: 26791

In what way does the regular Redis set not meet your criteria? Inserts and membership testing/intersection are obviously built in. Sets also have SRANDMEMBER to retrieve a random member of a set. You could call it multiple times to retrieve a subset of items (though there is the potential to get the same member back multiple times.

If the size of the set is large, and the size of the subset is small, this likely would not be that big of a deal. It gets trickier as the size of the subset grows compared to the size of the overall set (though eventually it gets cheaper to just randomly select the items that you don't want in the subset and then just do a set difference on it).

Upvotes: 1

Related Questions