Mastering Redis: Key Design and Data Type Use Cases
This guide explains Redis's core characteristics, practical key‑naming conventions, and detailed application scenarios for each data type—including strings, hashes, lists, sets, and sorted sets—complete with code examples and performance tips.
Redis Overview
Redis stores all data in memory, providing fast read/write operations. It supports rich data types—string, hash, list, set, sorted set, bitmap, hyperloglog—and persistence via AOF and RDB. All commands are atomic, and transactions are available.
Key Design
Use a colon‑separated namespace to encode hierarchical meaning: table:primaryKeyName:primaryKeyValue:field. Example for caching a user's email:
SET user:id:1:email [email protected]String
Binary‑safe key‑value pairs. Suitable for storing single fields, JSON blobs, or binary data. Common operations:
Cache a column value (e.g., email).
Store entire objects as JSON (though hashes are often more efficient).
Auto‑increment counters with INCR when the value is numeric.
Hash
Represents a map of field‑value pairs under a single key, analogous to a relational table row. Example: HSET user:1 name zj email [email protected] Hashes reduce memory usage and avoid JSON parsing overhead when storing objects.
List
Ordered collection implemented as a doubly linked list. Insertion at head ( LPUSH) and removal at tail ( RPOP) are O(1). Typical uses:
Message queues.
Fetching the most recent items (e.g., news feed).
Set
Unordered collection of unique elements with O(1) add, remove, and membership checks. Supports set operations (intersection, union, difference). Example use cases:
Find mutual friends: SINTER user:wade user:james.
Identify common tags or interests across entities.
Sorted Set
Each element is associated with a score, automatically ordered by that score. Common commands: ZADD – add element with score. ZREVRANGE – retrieve elements in descending score order. ZINCRBY – increment an element's score.
Typical scenarios:
Rank friends by intimacy score.
Sort articles by view count or likes.
Practical Examples
Storing a field by primary key: SET user:id:1:email [email protected] Object storage with hash: HSET user:1 name zj email [email protected] Auto‑increment ID: INCR user:id:counter Message queue with list:
LPUSH queue:tasks "task1"
RPOP queue:tasksMutual friends using set intersection:
SADD user:wade james melo paul kobe
SADD user:james wade melo paul kobe
SINTER user:wade user:jamesFriend ranking with sorted set:
ZADD user:kobe 80 james 90 wade 85 melo 90 paul
ZREVRANGE user:kobe 0 -1 WITHSCORES
ZINCRBY user:kobe 15 jamesChoosing the appropriate Redis data type and designing clear, hierarchical keys maximizes performance, reduces memory consumption, and simplifies data access patterns.
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.
