15 Essential Redis Commands Every Engineer Should Know
This article provides a detailed walkthrough of the 15 most commonly used Redis commands—including key, hash, list, set, sorted‑set, and monitoring operations—showing syntax, return values, typical use cases, performance characteristics, and cautions for production environments.
Key Operations
Basic key‑value manipulation commands:
SET stores a value. Example: SET user:1 "mikechen" returns OK. Typical scenarios: user info cache, session cache, token cache.
GET retrieves a value. Example: GET user:1 returns mikechen. The operation runs in O(1), which is a key reason for Redis' fast reads.
DEL deletes one or more keys. Example: DEL user:1 user:2 user:3 returns 3, indicating three keys were removed.
EXPIRE sets a TTL. Example: EXPIRE user:1 3600 makes the key expire after one hour. TTL user:1 returns the remaining seconds (e.g., 3599).
KEYS lists keys matching a pattern, e.g., KEYS user:*. Warning: it scans the entire dataset and can block the server when the key space is large; SCAN is recommended instead.
Hash Operations
Hashes are ideal for storing objects.
HSET sets fields in a hash. Example: HSET user:1 name "Mike" age 18.
HGETALL retrieves all fields. Example returns name and mikechen.
HGET fetches a single field, e.g., HGET user:1 name returns mikechen. Use cases include user profiles, product details, and configuration storage.
List Operations
Lists are implemented as doubly linked lists.
LPUSH inserts elements at the head. Example sequence inserts "消息1", "消息2", "消息3" into news.
LRANGE reads a range of elements, e.g., LRANGE news 0 -1 returns the three messages in insertion order.
Set Operations
Sets store unique, unordered elements.
SADD adds members: SADD tags java redis mysql.
SMEMBERS returns all members, yielding java, redis, mysql. Typical scenarios: user tags, mutual friends, lottery pools.
Sorted‑Set (ZSet) Operations
ZSets combine set semantics with a score for automatic ordering.
ZADD adds members with scores, e.g., ZADD rank 100 Tom, ZADD rank 95 Jack, ZADD rank 88 Mike.
ZRANGE retrieves members in ascending order with scores: ZRANGE rank 0 -1 WITHSCORES returns Mike 88, Jack 95, Tom 100.
ZREVRANGE gets members in descending order, useful for top‑N queries.
Operations & Monitoring
INFO shows server statistics such as connected_clients, used_memory, total_commands_processed, keyspace_hits, and keyspace_misses. Sub‑commands like INFO memory focus on specific sections.
MONITOR streams every command executed in real time. Example output shows a SET, GET, and DEL sequence. Warning: it incurs significant overhead and should only be used briefly for debugging.
Overall, the guide categorizes Redis commands into key, hash, list, set, sorted‑set, and operational groups, provides concrete syntax and return values, highlights typical application scenarios, and warns about commands that can impact performance in production.
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.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
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.
