Introduction
Redis is a high-performance, in-memory data store used for caching, real-time analytics, messaging, and event-driven systems. Understanding Redis key types and their behavior is essential for building fast, reliable applications.
What Is a Redis Key?
A Redis key is a unique string that maps to a value. The value can be stored using different data structures such as strings, hashes, lists, sets, sorted sets, or streams. Keys can also have expiration times (TTL).
SET user:123:name "Alice"
GET user:123:nameOutput:
"Alice"1. String Keys
Strings store a single value—text, numbers, or serialized data. They are commonly used for caching, counters, and rate limiting.
SET cache:product:1001 "{"id":1001,"name":"LandOfBytes"}" EX 300
GET cache:product:1001
INCR rate_limit:user:42
EXPIRE rate_limit:user:42 60
GET rate_limit:user:42Explanation: INCR increments a numeric value, while EXPIRE ensures the key automatically expires—an essential rate-limiting pattern.
2. Hash Keys
Hashes store multiple field-value pairs under a single key, making them ideal for representing objects.
HSET user:1001 name "Alice" plan "Pro" status "Active"
HGETALL user:1001Hashes allow partial updates without overwriting the entire object, saving memory and bandwidth.
3. List Keys
Lists are ordered collections, commonly used for queues and background job processing.
LPUSH queue:email "send_welcome_email:user_123"
LPUSH queue:email "resize_image"
LRANGE queue:email 0 -14. Set Keys
Sets store unordered, unique values—perfect for feature flags and membership checks.
SADD feature:beta_users 123 456 123
SMEMBERS feature:beta_users5. Sorted Set Keys (ZSETs)
Sorted sets store unique elements ordered by score, making them ideal for leaderboards and rankings.
ZADD leaderboard 980 alice
ZADD leaderboard 1200 bob
ZREVRANGE leaderboard 0 1 WITHSCORES6. Stream Keys
Streams are append-only logs designed for event-driven architectures and real-time pipelines.
XADD orders * order_id 8821 user_id 123 amount 49.99
XREAD COUNT 1 STREAMS orders 0Key Expiration & TTL
Redis supports automatic expiration of keys, which is essential for sessions, caches, and temporary tokens.
SET session:abc "active" EX 3600
TTL session:abcConclusion
Redis key types directly impact performance, scalability, and memory efficiency. Understanding how each structure behaves allows teams to build faster, more reliable systems using Redis.