Mastering Redis Data Types: Practical Tips and Real-World Use Cases
This article introduces Redis as an open‑source in‑memory store, outlines its core data structures—strings, hashes, lists, sets, sorted sets, bitmaps, hyperloglogs, and geospatial indexes—and provides concrete command examples, application scenarios, and best‑practice guidelines for effective usage.
Background
Redis is an open‑source in‑memory data‑structure store that can serve as a database, cache, and message broker. It supports many data types and provides replication, Lua scripting, LRU eviction, transactions, persistence, and high availability via Sentinel and Cluster.
Basic Data Types
String
Hash
List
Set
Sorted Set (zset)
Bitmap
HyperLogLog
Geospatial index
String
Expiration is cleared when the value is reset. Example commands:
127.0.0.1:6379> set hello 3
OK
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) -1Setting a string can overwrite any existing key type, which may raise a WRONGTYPE error if the key previously held a different data structure.
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 valueList
Use for user queues, ordered messages, producer‑consumer models.
Operations: push, pop, blocking BRPOP/BLPOP.
Set
Deduplication, tags, membership checks.
Examples: storing participants of a Spring 2019 activity, user tags, computing intersections, unions, differences, and random elements.
Hash
Store multiple attributes of a single resource.
Example: per‑user gift counts stored as a JSON‑like hash, allowing direct incr on individual fields.
Sorted Set (zset)
Ideal for leaderboards, ranking by score.
Typical commands: add member with score, retrieve top N, query a member’s score, range queries.
Complex scoring can combine primary score and secondary rank using decimal parts or large offsets.
Application Scenarios
Cache static or rarely changing data such as product info, activity configuration.
Cache hot data like game leaderboards updated every second.
Simple counters (e.g., total participants) using INCR.
Time‑limited actions (e.g., daily sign‑in) using EXPIRE.
Distributed locks via SET NX (note: the example is not robust for concurrency).
Best Practices / Cautions
Assign reasonable TTL to every key.
String expiration is overwritten when the value is reset.
String SET can overwrite other types; avoid accidental type changes.
Choose the appropriate Redis data structure for the problem.
Do not store massive data in a List when you need fast look‑ups.
Plan key namespace to separate business domains (e.g., business‑redis vs activity‑redis).
Use pipelines, Lua scripts, or transactions for performance‑critical batches.
Avoid running KEYS * on production clusters with many keys.
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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
