Core Data Types

Difficulty: Beginner

Question

What are the primary data types supported by Redis, and when would you use each one?

Answer

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.

The five core types at a glance

TypeWhat it holdsTypical commandsClassic use
StringText, numbers or binary up to 512 MBSET, GET, INCR, MSETCached JSON, counters, session tokens
HashField-value map under one keyHSET, HGET, HINCRBY, HGETALLA user profile or cart as one object
ListOrdered sequence by insertionLPUSH, RPOP, LRANGE, BLPOPQueues, recent activity feeds
SetUnordered unique membersSADD, SISMEMBER, SINTERTags, unique visitors, mutual friends
Sorted SetUnique members each with a float scoreZADD, ZRANGE, ZRANK, ZINCRBYLeaderboards, rate limit windows, priority queues

How to choose between them

  • String vs Hash for objects: storing a whole user as a JSON string is simple, but updating one field means reading, parsing and rewriting the blob. A Hash lets you do HINCRBY cart:42 items 1 without touching other fields. Small hashes are also stored in a compact listpack encoding, so they are memory efficient.
  • List vs Sorted Set for ordering: a List keeps insertion order and is O(1) at both ends, which is perfect for a queue. If you need ordering by a value you control (score, timestamp, price), use a Sorted Set, where inserts and rank lookups are O(log N).
  • Set for uniqueness and set maths: SADD ignores duplicates for free, and SINTER, SUNION and SDIFF let you compute things like "users who liked both posts" on the server.
  • Strings as counters: INCR and INCRBY are atomic, so a Swiggy-style "orders placed today" counter needs no locking.

The specialised types worth naming

  • Streams: an append-only log with consumer groups, used for event pipelines and job queues that need acknowledgements.
  • Bitmaps: not a separate type but bit operations on Strings (SETBIT, BITCOUNT), great for daily active user flags.
  • HyperLogLog: approximate unique counts in at most 12 KB per key.
  • Geo: longitude/latitude stored inside a Sorted Set, queried with GEOSEARCH.
  • JSON, time series, Bloom filters and vector sets: these were separate modules (RedisJSON, RedisTimeSeries, RedisBloom) and are bundled into Redis Open Source from Redis 8.

Common mistakes

  • Using KEYS or HGETALL on huge collections. Large values ("big keys") block the single command thread while they are read or deleted.
  • Storing everything as JSON strings and doing read-modify-write in the app, which creates race conditions that a Hash or INCR would avoid.
  • Forgetting that a key has exactly one type. Running LPUSH on a String key returns a WRONGTYPE error. TYPE key tells you what is stored.
  • Assuming Sorted Set scores are integers. They are double precision floats, so very large integer IDs (above 2^53) lose precision.
Ordering and uniqueness by type. The three collection types differ in whether they allow duplicates and how they order members.
ListSetSorted Set
DuplicatesAllowedNot allowedNot allowed
OrderInsertion orderNo orderBy score, then lexically
Add costO(1) at head or tailO(1)O(log N)
Typical useQueue or feedTags, membershipLeaderboard

Code examples

One example of each core type in redis-cli

# 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.

Key points

  • Five core types: String, Hash, List, Set, Sorted Set; each key holds exactly one type.
  • Strings are binary safe and capped at 512 MB; INCR on them is atomic.
  • Sorted Set operations are O(log N) because they use a skiplist plus a hash table.
  • Small Hashes, Lists, Sets and Sorted Sets use compact listpack or intset encodings to save memory.
  • Redis 8 bundles JSON, time series and probabilistic types into the open source server.

Common follow-up questions

  • How is a Sorted Set implemented internally?
  • Would you store a user object as a JSON string or a Hash? Why?
  • How would you build a top-10 leaderboard that updates in real time?
  • What is a big key and why is it dangerous?

Interview tips

  • Pair each type with a concrete use case; interviewers care more about choosing the type than listing it.
  • Mention time complexity for Sorted Sets (O(log N)) and Lists (O(1) at ends, O(N) for index access).
  • Bring up Streams and HyperLogLog briefly to show you know Redis is more than a string cache.

Concepts covered

Strings, Hashes, Lists, Sets, Sorted Sets, Streams

More Redis & Caching questions