Difficulty: Beginner
What are the primary data types supported by Redis, and when would you use each one?
Redis is a key-value store where every value has a type, and the five core types are Strings, Hashes, Lists, Sets and Sorted Sets. On top of these Redis offers Streams, Bitmaps, HyperLogLog and Geospatial indexes, and Redis 8 ships JSON, time series and probabilistic structures (Bloom, Cuckoo, Top-K) as part of the open source server. Picking the right type is the main design decision in Redis, because each one comes with its own commands and time complexity.
| Type | What it holds | Typical commands | Classic use |
|---|---|---|---|
| String | Text, numbers or binary up to 512 MB | SET, GET, INCR, MSET | Cached JSON, counters, session tokens |
| Hash | Field-value map under one key | HSET, HGET, HINCRBY, HGETALL | A user profile or cart as one object |
| List | Ordered sequence by insertion | LPUSH, RPOP, LRANGE, BLPOP | Queues, recent activity feeds |
| Set | Unordered unique members | SADD, SISMEMBER, SINTER | Tags, unique visitors, mutual friends |
| Sorted Set | Unique members each with a float score | ZADD, ZRANGE, ZRANK, ZINCRBY | Leaderboards, rate limit windows, priority queues |
HINCRBY cart:42 items 1 without touching other fields. Small hashes are also stored in a compact listpack encoding, so they are memory efficient.SADD ignores duplicates for free, and SINTER, SUNION and SDIFF let you compute things like "users who liked both posts" on the server.INCR and INCRBY are atomic, so a Swiggy-style "orders placed today" counter needs no locking.SETBIT, BITCOUNT), great for daily active user flags.GEOSEARCH.KEYS or HGETALL on huge collections. Large values ("big keys") block the single command thread while they are read or deleted.INCR would avoid.LPUSH on a String key returns a WRONGTYPE error. TYPE key tells you what is stored.| List | Set | Sorted Set | |
|---|---|---|---|
| Duplicates | Allowed | Not allowed | Not allowed |
| Order | Insertion order | No order | By score, then lexically |
| Add cost | O(1) at head or tail | O(1) | O(log N) |
| Typical use | Queue or feed | Tags, membership | Leaderboard |
# String: cache and atomic counter
SET product:101 '{"name":"Kurta","price":79900}' EX 300
INCR orders:today
# Hash: an object whose fields change independently
HSET cart:42 items 3 total_paise 149700
HINCRBY cart:42 items 1
# List: simple FIFO queue
LPUSH email:queue job:1 job:2
RPOP email:queue
# Set: unique tags
SADD post:7:tags redis caching backend
SISMEMBER post:7:tags redis
# Sorted Set: leaderboard
ZADD contest:leaderboard 1450 asha 1320 ravi
ZREVRANGE contest:leaderboard 0 9 WITHSCORES
TYPE cart:42
Each type has its own command family. Prices are stored in paise as integers so that `HINCRBY` and `INCRBY` stay exact.
Strings, Hashes, Lists, Sets, Sorted Sets, Streams