Mastering Redis: When to Use Strings, Lists, Sets, Hashes, and Zsets
This article explains Redis' core data types—strings, lists, sets, hashes, sorted sets, and more—detailing their characteristics, common commands, practical use‑cases such as caching, counters, distributed locks, and ranking, and provides best‑practice tips for key design and performance.
Redis is an open‑source in‑memory data structure store that can be used as a database, cache, and message broker.
It supports multiple data structures, built‑in replication, Lua scripting, LRU eviction, transactions, and various persistence levels. High availability is provided via Sentinel and Cluster automatic sharding.
Basic Data Types
String
Hash
List
Set
Sorted Set
Bitmap
HyperLogLog
Geospatial
String Details
1. Expiration time of a string is cleared when the value is reset.
127.0.0.1:6379> set hello 3
OK
127.0.0.1:6379> get hello
"3"
127.0.0.1:6379> ttl hello
(integer) -1
127.0.0.1:6379> expire hello 3000
(integer) 1
127.0.0.1:6379> set hello 4
OK
127.0.0.1:6379> ttl hello
(integer) -12. Setting a string value can overwrite keys of other types.
127.0.0.1:6379> sadd settest 1,2
(integer) 1
127.0.0.1:6379> type settest
set
127.0.0.1:6379> set settest hello
OK
127.0.0.1:6379> type settest
string
127.0.0.1:6379> sadd settest a,b
(error) WRONGTYPE Operation against a key holding the wrong kind of valueApplication Scenarios
String
Cache data (e.g., activity titles, product info, configuration)
Simple counters (e.g., total participants of an event)
Timed expiration (e.g., daily sign‑in flag)
Distributed lock (e.g., SET lockkey 1 NX)
List
Queueing users (push/pop)
Ordered messages (push/pop)
Producer‑consumer model (push/pop, BRPOP/BLPOP for blocking reads)
Set
Deduplication lists
User tags, merchant tags
Set operations: intersection (common tasks), union (any task), difference (remaining tasks), random element (random gift)
Hash
Store multiple attributes of a single resource (e.g., user’s different gift counts)
Directly increment fields (e.g., HINCRBY giftA)
Sorted Set (Zset)
Leaderboards (user consumption, likes)
Key format: active:spring:star:rank → member=UserID, score=likes
Query top N, score range, or specific user score
Composite scoring using integer part for primary score and fractional part for secondary ranking
Best Practices
Assign reasonable TTL to every key.
String expiration is overwritten when the value is reset.
String SET can overwrite keys of other types; use appropriate data structures.
Plan key naming and quantity carefully; separate business domains (e.g., business Redis vs. activity Redis).
Leverage pipelines, Lua scripts, and transactions for performance.
Avoid using KEYS * on production clusters with many keys.
Do not store large volumes of data in a list and then search it.
Use a set to track user participation instead of creating a separate key per user.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
