Master Redis: Essential Commands for Strings, Lists, Sets, Sorted Sets, and Hashes
This guide presents a comprehensive overview of Redis core data structures—String, List, Set, Sorted Set, and Hash—detailing their most common commands, practical code examples, and typical application scenarios such as counters, distributed sessions, locks, ranking lists, and shopping‑cart storage.
Redis String Commands
Common commands for manipulating simple key‑value pairs:
set key value # Set a key‑value pair
get key # Retrieve the value of a key
incr key # Increment an integer value by 1
decr key # Decrement an integer value by 1
strlen key # Get the length of a string value
incrby key increment # Increment by a specific integerWhen the stored value is numeric, Redis treats it as an integer and supports incr, decr, incrby, and decrby operations.
String Application Scenarios
Counters – e.g., page views, likes, inventory counts (atomic because Redis processes commands single‑threaded).
Shared session storage in distributed systems.
Distributed locks using SET key value NX to ensure the key is set only when absent.
Redis List Commands
Lists are ordered collections of strings.
lpush key v1 v2 … # Push elements to the left (head)
rpush key v1 v2 … # Push elements to the right (tail)
lrange key start stop # Retrieve a range of elements
lpop key # Pop element from the left
rpop key # Pop element from the right
lindex key index # Get element by index
llen key # Get list length
lrem key count value # Remove occurrences of a value
ltrim key start stop # Trim list to the specified range
rpoplpush source dest # Pop from source right and push to destination left
lset key index value # Set element at a specific index
linsert key BEFORE|AFTER pivot value # Insert before or after a pivot elementInternally, Redis uses a quicklist (a hybrid of ziplist and linked list) to store lists efficiently.
List Application Scenarios
User, product, or comment lists.
Message queues.
Stack implementations.
Redis Set Commands
Sets store unique, unordered elements.
sadd key member1 member2 … # Add members to a set
smembers key # Retrieve all members
sismember key member # Check if a member exists
scard key # Get the number of members
srem key member # Remove a member
srandmember key # Return a random member (without removal)
spop key # Remove and return a random member
smove source dest member # Move a member from one set to anotherTypical uses include deduplication, computing intersections/unions/differences, and tracking unique visitors.
Set Application Scenarios
Website unique visitor (UV) counting.
Finding mutual friends.
Redis Sorted Set Commands
Sorted sets associate a score with each member, keeping them ordered by score.
zadd key score1 member1 score2 member2 … # Add members with scores
zrem key member1 member2 … # Remove members
zcard key # Number of members
zcount key min max # Count members with scores in range
zincrby key increment member # Increment a member's score
zscore key member # Get a member's score
zrank key member # Rank (ascending)
zrevrank key member # Rank (descending)
zrange key start stop [WITHSCORES] # Get members in score order (asc)
zrevrange key start stop [WITHSCORES] # Get members in score order (desc)Sorted sets are ideal for ranking systems such as sales leaderboards or click‑through rankings.
Redis Hash Commands
Hashes map fields to values within a single key, perfect for representing objects.
hset key field value # Set a field
hget key field # Get a field
hmset key field1 val1 field2 val2 … # Set multiple fields
hmget key field1 field2 … # Get multiple fields
hgetall key # Retrieve all fields and values
hdel key field1 field2 … # Delete fields
hlen key # Number of fields
hkeys key # List all field names
hincrby key field increment # Increment integer field
hincrbyfloat key field increment # Increment floating‑point field
hsetnx key field value # Set if field does not existTypical scenario: shopping cart storage where the key is cart:{user_id}, each field is a product ID, and the value is the quantity.
HSET cart:123 1001 1 # Add product 1001 with quantity 1
HINCRBY cart:123 1001 1 # Increment quantity
HLEN cart:123 # Number of distinct products
HDEL cart:123 1001 # Remove a product
HGETALL cart:123 # Retrieve the whole cartAfter fetching the cart, the application can query the product database for detailed information.
For further learning, refer to official Redis documentation and advanced tutorials.
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.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
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.
