Redis Basic Data Types: Strings, Lists, Hashes, Sets, and Sorted Sets with Commands and Examples
This article explains Redis's five basic data types—String, List, Hash, Set, and Sorted Set—detailing their typical use cases, common commands, and providing concrete CLI examples that demonstrate how to manipulate each type.
Redis provides a rich set of data structures, including five basic types and five extended types (non‑official classification). This article introduces the five basic types, lists common commands, and provides example operations and typical use cases.
1. String
String is the most fundamental and widely used data type. It stores a key‑value pair where the key is a string and the value can be a string or binary data (e.g., serialized objects, images). Common scenarios include caching, counters, distributed locks, and session sharing.
# Set key-value pair, add if key does not exist
SET key value
# Set expiration time for key
SET key value EX
SETEX key
value
# Get value of key
GET key
# Check if key exists
EXISTS key
# Delete key
DEL key
# Get TTL of key
TTL key
# Increment numeric value stored at key
INCR key
# Decrement numeric value stored at key
DECR keyExperiment shows that resetting a key without specifying an expiration removes the previous TTL, making the key persistent.
2. List
List stores ordered strings, allowing insertion at both the head and tail. It is useful for message queues, stacks, and other ordered collections.
# Add new elements to the head of the list
LPUSH key element [element ...]
# Add new elements to the tail of the list
RPUSH key element [element ...]
# Remove one or more elements from the head
LPOP key [count]
# Remove one or multiple elements from the tail
RPOP key [count]
# Get length of the list
LLEN key
# Get elements in specified range
LRANGE key start stop
# Insert new value before/after a pivot
LINSERT key
pivot element
# Remove specified number of a given element
LREM key count elementExample: push three order IDs, list them, remove one, and verify the remaining elements.
3. Hash
Hash stores field‑value pairs, similar to a map, suitable for representing objects such as a user profile.
# Set one or multiple field values
HSET key field value [field value ...]
# Get value of a specific field
HGET key field
# Get values of multiple fields
HMGET key field [field ...]
# Get all fields and values
HGETALL key
# Check if a field exists
HEXISTS key field
# Delete one or more fields
HDEL key field [field ...]
# Get all field names
HKEYS key
# Get all field values
HVALS keyExample: store a user's name, age, and height, retrieve individual fields, fetch multiple fields, and delete a field.
4. Set
Set stores unique strings and supports typical set operations such as union, intersection, and difference.
# Add one or more members to the set
SADD key member [member ...]
# Retrieve all members
SMEMBERS key
# Check if a member exists
SISMEMBER key member
# Get number of members
SCARD key
# Get intersection of multiple sets
SINTER key [key ...]
# Get union of multiple sets
SUNION key [key ...]
# Get difference of multiple sets
SDIFF key [key ...]Example: add elements to two sets, compute their intersection.
5. Sorted Set
Sorted Set stores unique strings each associated with a score, ordered by score (and lexicographically for equal scores). It is ideal for leaderboards and rate limiting.
# Add one or more members with scores
ZADD key score member [score member ...]
# Get elements in a range (ascending)
ZRANGE key start stop
# Get elements in a score range (ascending)
ZRANGEBYSCORE key min max
# Increment score of a member
ZINCRBY key increment member
# Remove one or more members
ZREM key member [member ...]
# Get count of members in a score range
ZCOUNT key min max
# Get rank of a member (ascending)
ZRANK key member
# Get score of a member
ZSCORE key memberExample: add players with scores, retrieve the leaderboard, query by score range, increment a score, and get a player's rank.
Conclusion
Redis offers five powerful basic data types that cover a wide range of scenarios, far surpassing simple key‑value stores like Memcached. The upcoming articles will cover the five extended data types.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
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.