Getting Started
Redis Data Types
Master Redis core data types: Strings, Lists, Hashes, Sets, and Sorted Sets — and when to use each.
Redis Data Types
Redis is not just a key-value store. It supports a rich set of data structures, each optimized for specific use cases. Choosing the right data type is the key to getting the most out of Redis.
Strings
The most basic type. A Redis string can hold a text string, integer, or binary blob (up to 512 MB).
Use for: Simple caching, counters, feature flags, session tokens.
SET name "Alice"
GET name # "Alice"
APPEND name " Smith"
GET name # "Alice Smith"
# Integers
SET views 100
INCR views # 101
INCRBY views 50 # 151
DECRBY views 10 # 141
# Expire after 60 seconds
SETEX token:abc 60 "user:42"Lists
An ordered collection of strings, implemented as a doubly linked list. O(1) push/pop from both ends.
Use for: Message queues, activity feeds, recent items, job queues.
RPUSH queue "task1" "task2" "task3" # Push to right
LPUSH queue "urgent" # Push to left (front)
LRANGE queue 0 -1 # Get all items
# 1) "urgent"
# 2) "task1"
# 3) "task2"
# 4) "task3"
LPOP queue # Remove and return first item ("urgent")
RPOP queue # Remove and return last item ("task3")
LLEN queue # Length: 2Hashes
A map of field-value pairs. Like a dictionary or object. Efficient for representing structured objects.
Use for: User profiles, product data, configuration objects.
HSET user:1 name "Alice" email "alice@example.com" age 30
HGET user:1 name # "Alice"
HMGET user:1 name email # Multiple fields
HGETALL user:1 # All fields and values
HEXISTS user:1 email # 1 (true)
HDEL user:1 age
HINCRBY user:1 loginCount 1Sets
An unordered collection of unique strings. Supports set operations (union, intersection, difference).
Use for: Tags, unique visitors, friend lists, access control lists.
SADD tags:post:1 "redis" "database" "caching"
SADD tags:post:2 "redis" "python" "ai"
SMEMBERS tags:post:1 # All members
SISMEMBER tags:post:1 "redis" # 1 (true)
SCARD tags:post:1 # 3 (count)
# Set operations
SINTER tags:post:1 tags:post:2 # Intersection: {"redis"}
SUNION tags:post:1 tags:post:2 # Union: all tags
SDIFF tags:post:1 tags:post:2 # Difference: {"database","caching"}Sorted Sets
Like Sets, but each member has an associated floating-point score. Members are ordered by score.
Use for: Leaderboards, priority queues, time-series events, rate limiting.
ZADD leaderboard 1500 "alice"
ZADD leaderboard 2200 "bob"
ZADD leaderboard 1800 "charlie"
# Get rank (0-indexed, ascending)
ZRANK leaderboard "alice" # 0 (lowest score)
ZREVRANK leaderboard "bob" # 0 (highest score)
# Get top 3 with scores
ZREVRANGE leaderboard 0 2 WITHSCORES
# Members in score range
ZRANGEBYSCORE leaderboard 1000 2000Streams (Redis 5+)
An append-only log structure for event streaming. Like a lightweight Kafka.
XADD events * action "login" userId "42"
XADD events * action "purchase" userId "42" amount "99.99"
XLEN events # Number of entries
XRANGE events - + # All entriesExample
# Practical: User session with Hash
HSET session:token123 userId 42 role "admin" createdAt "2025-01-01"
EXPIRE session:token123 86400
# Practical: Rate limiting with Sorted Set
ZADD ratelimit:user:42 1700000000 "req1"
ZADD ratelimit:user:42 1700000001 "req2"
# Count requests in last 60 seconds
ZCOUNT ratelimit:user:42 (NOW-60) NOW
# Practical: Leaderboard
ZINCRBY scores 10 "alice"
ZINCRBY scores 25 "bob"
ZREVRANGEBYSCORE scores +inf -inf WITHSCORES LIMIT 0 10Want to run this code interactively?
Try in Compiler