Skip to content

Sets & Sorted Sets: Overview

Redis gives you two collection types that guarantee every member appears at most once:

TypeOrderExtra dataBest for
SetNone (hash-table)NoneTags, membership checks, set algebra
Sorted SetBy score (float64)Score per memberLeaderboards, ranked feeds, range queries

Both types store unique strings. The difference is that a Sorted Set attaches a floating-point score to each member, and keeps them sorted by that score at all times.

A Set is an unordered collection of unique strings. Adding the same value twice has no effect.

127.0.0.1:6379> SADD lang:tags "python" "go" "rust" "go"
(integer) 3
127.0.0.1:6379> SMEMBERS lang:tags
1) "rust"
2) "go"
3) "python"
127.0.0.1:6379> SCARD lang:tags
(integer) 3
SADD lang:tags "python" "go" "rust" "go"
SMEMBERS lang:tags
SCARD lang:tags

A Sorted Set pairs each member with a score. Members are always retrieved in score order.

127.0.0.1:6379> ZADD scores 1500 "alice" 2200 "bob" 980 "carol"
(integer) 3
127.0.0.1:6379> ZRANGE scores 0 -1 WITHSCORES
1) "carol"
2) "980"
3) "alice"
4) "1500"
5) "bob"
6) "2200"
127.0.0.1:6379> ZRANK scores "alice"
(integer) 1
ZADD scores 1500 "alice" 2200 "bob" 980 "carol"
ZRANGE scores 0 -1 WITHSCORES
ZRANK scores "alice"
  • Set — you need to answer “is X a member?” or “what do A and B have in common?” quickly.
  • Sorted Set — you need ordering, ranking, or range queries (e.g., “players with scores between 1000 and 2000”).
LessonTopic
Sets & Sorted Sets: OverviewThis page — types and a quick taste
SetsSADD, SREM, SMEMBERS, SISMEMBER, SCARD, SRANDMEMBER, SPOP
Set OperationsSINTER, SUNION, SDIFF and their STORE variants
Sorted SetsZADD, ZSCORE, ZRANGE, ZREVRANGE, ZRANK, ZRANGEBYSCORE, ZINCRBY, ZCARD, ZREM
LeaderboardsBuilding a real leaderboard with Sorted Sets
What guarantee do both Sets and Sorted Sets share?
What extra piece of data does a Sorted Set store per member?
What happens when you ZADD a member that already exists in a Sorted Set?