Take the 2-minute tour ×

In Redis is it possible to get a range of items from a Set?

I only notice SMEMBERS which gives you all the members. http://redis.io/commands#set

For example, if I have to use SMEMBERS in millions of items everytime, I just want 10 of them from index 33,456 to 33,466.

SMEMBERS will have to generate a full list of millions of items everytime I ask for 10 of them. Isn't that a performance killer? Or is it okay because Redis is fast and meant to be used that way?

share|improve this question
A set is a non ordered data structure. Fetching items by range in a set is meaningless. There is no concept of index. Perhaps you should use a list or a sorted set? –  Didier Spezia Oct 8 '12 at 18:49
FYI: as of Redis 2.6.0-RC8 there is now a SRANDMEMBER <count> command that returns multiple random elements that you may find useful. –  deltheil Oct 8 '12 at 20:56
You might achieve your goal using the relatively feature-rich sort command: redis.io/commands/sort –  Ofer Zelig Oct 8 '12 at 23:11

2 Answers 2

up vote 2 down vote accepted

ZRange might help you using Sorted Sets in redis

In redis-cli

ZRANGE your_key 33455 33465
share|improve this answer

No, it is not possible, because the concept of an index does not exist in the Set data type. In fact, SMEMBERS does not guarantee you a specific order; most likely the order of the elements will be random every time you call it. Think of Sets as unordered collections of things: great if you need to store a bunch of ids that share something in common, but definitively not the data type to use if you need to implement pagination. Perhaps you are looking for Lists or Sorted Sets instead?

I recommend reading the following to understand the available data types in redis and when to use each:

share|improve this answer

Your Answer


