Redis Data Types
Strings, lists, sets, sorted sets, hashes, and bitmaps
Skills taught: redis.data-types
The Six Core Data Types
Redis is not just a string cache. It supports six native data structures with dedicated commands. Choosing the right type for your use case is the most important Redis design decision.
- String — bytes, integers, floats. Up to 512 MB per value
- List — ordered sequence, O(1) push/pop from both ends
- Set — unordered collection of unique strings
- Sorted Set (ZSet) — set where every member has a floating-point score
- Hash — a field-value map (like a mini-document)
- Stream — append-only log of messages with consumer groups
Strings
Strings are the simplest type — a key maps to a binary-safe byte sequence. Use them for caching, counters, and flags.
SET user:1:name "Alice"
GET user:1:name
# "Alice"
# Atomic counter
SET page:views 0
INCR page:views # 1
INCRBY page:views 5 # 6
# String with expiry (cache)
SET cache:homepage "<html>..." EX 300 # 5-minute cache
# Set only if not exists (mutex / lock)
SET lock:resource1 "locked" NX EX 30 # returns OK or nil
Lists
Lists are ordered sequences with O(1) push and pop at both ends — ideal for queues (RPUSH + BLPOP) and stacks (LPUSH + LPOP).
# Queue: enqueue at right, dequeue at left
RPUSH jobs "send-email" "resize-image" "notify-user"
LPOP jobs # "send-email"
# View without removing
LRANGE jobs 0 -1 # all items
# Blocking pop (worker waits for work)
BLPOP jobs 30 # blocks up to 30 seconds
# Trim to last 100 items
LTRIM activity 0 99
Sets and Sorted Sets
# Set — unique members
SADD tags "redis" "cache" "nosql"
SISMEMBER tags "redis" # 1 (true)
SMEMBERS tags # all tags
# Set operations
SADD user:1:friends "alice" "bob"
SADD user:2:friends "bob" "carol"
SINTER user:1:friends user:2:friends # common: "bob"
SUNION user:1:friends user:2:friends # all: alice, bob, carol
# Sorted Set — members ranked by score
ZADD leaderboard 1500 "alice" 2000 "bob" 1800 "carol"
ZRANGE leaderboard 0 -1 WITHSCORES REV # top to bottom
ZRANK leaderboard "alice" # 2 (0-indexed from low to high)
Hashes
Hashes store field-value pairs under one key — like a mini-object. More memory-efficient than storing each field as a separate string key.
# Store a user object
HSET user:1 name "Alice" email "alice@example.com" age 30
# Get specific fields
HGET user:1 name # "Alice"
HMGET user:1 name email # ["Alice", "alice@example.com"]
HGETALL user:1 # all fields
# Atomic increment of a field
HINCRBY user:1 loginCount 1
# Check field existence
HEXISTS user:1 email # 1
Hash vs Separate String Keys
- Hash (HSET user:1 name Alice age 30): One key, multiple fields
- Separate strings (SET user:1:name Alice): One key per field
Tip: Pick the data type that matches your read pattern. If you need to read the whole object, use a hash. If you need to do range queries by score, use a sorted set.
Frequently Asked Questions
What is the maximum size of a Redis string?
512 MB per string value. In practice, keep values small — large values increase serialization cost and network transfer time.
Can I expire individual fields in a hash?
No. TTL is set on the key, not individual fields. If you need per-field expiry, use separate string keys instead.
When should I use a List vs a Stream?
Use a List for simple FIFO queues where messages are consumed once. Use Streams when you need message history, consumer groups, or the ability to replay messages.