What core data structures does Redis support beyond simple string keys?
Redis is an in-memory remote dictionary that supports rich native data structures:
• Strings: Text or binary data up to 512MB (can be used as integers for atomic INCR/DECR). • Lists: Linked lists sorted by insertion order (`LPUSH`, `RPOP` for queues). • Sets: Unordered collections of unique strings (`SADD`, `SINTER` for intersections). • Sorted Sets (ZSet): Unique members ordered by a floating-point score (`ZADD`, `ZRANGEBYSCORE` for leaderboards). • Hashes: Maps between string fields and string values (`HSET`, `HGETALL` for objects). • Bitmaps & HyperLogLogs: Space-efficient structures for bit manipulation and cardinal counting.
# Leaderboard using Sorted Sets (ZSet)
ZADD game_leaderboard 4500 "PlayerAlpha"
ZADD game_leaderboard 6200 "PlayerBeta"
ZADD game_leaderboard 3100 "PlayerGamma"
# Get top 2 players descending with their scores
ZREVRANGE game_leaderboard 0 1 WITHSCORES
# Output:
# 1) "PlayerBeta"
# 2) "6200"
# 3) "PlayerAlpha"
# 4) "4500"Because elements in a Sorted Set are kept ordered in a skip-list with O(log N) insertion and retrieval, leaderboards update in real-time even with millions of active players.
Ready for Caching Strategies?
Take a quick test to evaluate yourself, or move straight to the next module.