In Redis, LIST, SET, and SORTED SET (ZSET) are different data structures, each with specific characteristics and use cases:
Description:
A Redis LIST is an ordered collection of string elements, like a linked list.
Characteristics:
LPUSH
, RPUSH
, LPOP
, RPOP
).LINDEX
, LRANGE
.Use cases:
Example:
RPUSH mylist "a" "b" "c"
LRANGE mylist 0 -1 # ["a", "b", "c"]
Description:
A Redis SET is an unordered collection of unique string elements.
Characteristics:
SUNION
, SINTER
, SDIFF
.Use cases:
Example:
SADD myset "a" "b" "c" "a"
SMEMBERS myset # {"a", "b", "c"}
Description:
A Sorted Set is like a SET but with a score associated with each element, and elements are sorted by score.
Characteristics:
ZRANGE
, ZREVRANGE
, ZADD
, ZSCORE
.Use cases:
Example:
ZADD myzset 100 "Alice" 200 "Bob" 150 "Charlie"
ZRANGE myzset 0 -1 WITHSCORES
# [("Alice", 100), ("Charlie", 150), ("Bob", 200)]
Feature | LIST | SET | SORTED SET (ZSET) |
---|---|---|---|
Order | Yes (insertion) | No | Yes (by score) |
Duplicates | Allowed | Not allowed | Not allowed |
Indexing | Yes (by position) | No | Yes (by rank/score) |
Main use | Queues/logs | Membership/tags | Rankings/leaderboards |