What are the core data types in Redis and when do you use each?
Understand the core Redis data types — strings, hashes, lists, sets and sorted sets — with clear use cases, commands and interview-ready answers.
Expected Interview Answer
Redis's core data types are strings, hashes, lists, sets, and sorted sets, with each optimised for a different access pattern so you pick the structure that matches your problem.
Strings hold text, numbers or binary blobs and back caches and counters; hashes store field-value maps ideal for objects like a user record; lists are ordered sequences used as queues or stacks; sets hold unique unordered members for membership and deduplication; and sorted sets add a score to each member for ranking, making them perfect for leaderboards and priority queues. Redis also offers specialised types like bitmaps, HyperLogLog, streams and geospatial indexes, but the five core types cover most workloads.
- Right structure means fewer round trips and less client-side logic
- Atomic commands per type avoid race conditions
- Sorted sets give O(log n) ranking out of the box
- Hashes store objects compactly without JSON parsing
- Sets handle deduplication and intersection natively
AI Mentor Explanation
The five types are like different scorekeeping tools: a string is the total runs figure, a hash is a batter's full stat card of fields, a list is the ordered sequence of deliveries, a set is the roster of unique players, and a sorted set is the batting averages table ranked highest to lowest.
Step-by-Step Explanation
Step 1
Start with strings
Use strings for simple caches, flags, tokens and numeric counters with INCR/DECR.
Step 2
Model objects with hashes
Store an entity's fields in a single hash key so you update one field without rewriting the whole object.
Step 3
Queue with lists
Use LPUSH/RPOP for FIFO queues or job pipelines where order matters.
Step 4
Deduplicate with sets
Use sets for unique membership, tags, and set operations like intersection and union.
Step 5
Rank with sorted sets
Attach a score to each member and use ZADD/ZRANGE for leaderboards and priority ordering.
What Interviewer Expects
- Naming the five core types accurately
- Matching each type to a realistic use case
- Awareness of the atomic commands each type provides
- Understanding why sorted sets suit leaderboards
- Knowing hashes are better than strings for structured objects
Common Mistakes
- Storing an object as one JSON string when a hash fits better
- Confusing sets (unordered, unique) with lists (ordered, duplicates allowed)
- Thinking sorted sets keep insertion order rather than score order
- Ignoring specialised types like streams when they fit the job
Best Answer (HR Friendly)
“Redis offers a few different ways to store data, each suited to a task: simple values, object-like records, ordered lists, collections of unique items, and ranked lists. Choosing the right one keeps the app fast and the code simple, like a leaderboard using the ranked type.”
Code Example
# String counter
INCR visits
# Hash as an object
HSET user:1 name "Ada" age 36
# List as a queue
LPUSH jobs "email"
RPOP jobs
# Set of unique tags
SADD post:1:tags redis nosql
# Sorted set leaderboard
ZADD scores 100 "ada"
ZREVRANGE scores 0 2 WITHSCORESFollow-up Questions
- How would you build a leaderboard in Redis?
- When is a hash preferable to storing JSON in a string?
- How do you implement a reliable queue with Redis lists?
- What are Redis streams and how do they differ from lists?
- How does Redis handle expiry (TTL) on these data types?
MCQ Practice
1. Which Redis type is best suited for a leaderboard ranked by score?
A sorted set associates a score with each member and keeps them ordered, ideal for leaderboards.
2. Which type would you use to store a user object's named fields compactly?
A hash stores field-value pairs under one key, perfect for object-like records.
3. What distinguishes a Redis set from a Redis list?
Sets store unique, unordered members while lists preserve insertion order and permit duplicates.
Flash Cards
Redis core data types? — Strings, hashes, lists, sets and sorted sets.
Best type for a leaderboard? — Sorted set (ZADD/ZRANGE) — members ordered by an attached score.
Hash vs string for objects? — A hash stores named fields you can update individually without rewriting the whole value.
Set vs list? — Sets hold unique unordered members; lists keep order and allow duplicates.
Continue Learning
Related Interview Questions
What is Redis and what makes it different from a traditional relational database?
easy
What Data Structures Does Redis Support and When to Use Them?
medium
How does Redis achieve its high performance as an in-memory store?
hard
What is a Redis hash and how does it differ from storing a serialized object?
medium