Advertisement

Data Types interview questions and answers

7 Data Types interview questions for Redis roles, each with a model answer, examples and likely follow-ups.

7 questions
Advertisement

7 questions

Q1 Beginner Save

What are the core data types supported by Redis, and what is each one typically used for?

Redis supports strings for simple text or numeric values, hashes for storing objects with multiple fields, lists for ordered collections that support fast additions at either end, sets for unordered unique collections, and sorted sets for collections ordered by a score, each suited to different kinds of application needs.
Example

SET user:1:name 'John' HSET user:1 name 'John' age 30 LPUSH recent:views 'product:456' SADD tags:post:1 'redis' 'database' ZADD leaderboard 100 'player1'

Real-world example
A gaming application uses strings for simple session tokens, hashes for player profile data, lists for a recent activity feed, sets for tracking unique achievements, and sorted sets for a live leaderboard, choosing the right type for each specific need.
Likely follow-ups
  • How do you decide which data type is right for a specific use case?
  • Can a single Redis key hold more than one data type at the same time?
Strings & Bitmaps in Redis Hashes in Redis
Q2 Beginner Save

Why does choosing the right Redis data type for your data matter, rather than just storing everything as a string?

Using the appropriate data type gives you access to specialized, efficient operations designed for that structure, such as incrementing a counter atomically, adding to a list without reading and rewriting the whole thing, or finding the highest scoring items in a sorted set instantly, all of which would be much more complicated and slower if everything were stored as a single generic string.
Example

-- Storing a list as a string requires reading, parsing, -- modifying, and rewriting the entire value -- A native Redis list supports direct, efficient operations LPUSH mylist 'newitem'

Real-world example
A team initially stored a user's recent orders as a JSON string, then switched to a proper Redis list, gaining the ability to efficiently add new orders and trim old ones without needing to read and rewrite the entire value each time.
Likely follow-ups
  • What performance problems can arise from misusing strings for structured data?
  • How do you migrate existing string based data to a more appropriate data type?
Lists in Redis Redis Memory Optimization
Q3 Intermediate Save

What is the difference between a Redis set and a Redis sorted set, and when would you choose one over the other?

A set stores unique, unordered values and is ideal for membership checks like determining if an item exists in a collection, while a sorted set also stores unique values but each one has an associated numeric score that determines its order, making it the right choice whenever you need to rank items, such as a leaderboard or a list ordered by recency.
Example

SADD unique_visitors:today 'user123' ZADD leaderboard 1500 'user123'

Real-world example
An analytics system uses a regular set to track which unique visitors came to the site today, while using a sorted set for a leaderboard that ranks users by their accumulated points.
Likely follow-ups
  • Can you convert a set into a sorted set later if you decide you need ordering?
  • How does adding a score to every value affect memory usage compared to a plain set?
Sets & Sorted Sets in Redis Leaderboards with Sorted Sets
Q4 Intermediate Save

What is a Redis hash, and why is it often a more efficient choice than storing separate keys for each field of an object?

A hash stores multiple field value pairs under a single key, similar to a small object or dictionary, and is often more memory efficient than storing each field as its own separate top level key, since Redis can store small hashes very compactly, and it also lets you retrieve or update specific fields without touching the rest of the object.
Example

HSET user:123 name 'Alice' email 'alice@example.com' age 28 HGET user:123 email

Real-world example
A user profile system stores a user's name, email, and age together in a single hash, rather than as three separate top level keys, saving memory and making it easy to fetch the entire profile with one command.
Likely follow-ups
  • At what point does storing many fields in a hash become less efficient than separate keys?
  • Can you set an expiration time on an individual field within a hash?
Hashes in Redis Redis Memory Optimization
Q5 Advanced Save

How would you model a many to many relationship, such as users following other users, using Redis's native data types?

You could use a set for each user storing the ids of users they follow, and a separate set for each user storing the ids of their followers, letting you efficiently check if one user follows another, count followers, or find the intersection of two users' follow lists, all using Redis's native set operations.
Example

SADD following:user1 'user2' 'user3' SADD followers:user2 'user1' SINTER following:user1 following:user4 -- mutual follows

Real-world example
A social networking application models its follow relationships using sets for each user's following and followers lists, taking advantage of Redis's fast set intersection command to quickly find mutual connections between two users.
Likely follow-ups
  • How do you keep both the following and followers sets consistent when a follow relationship changes?
  • What are the memory implications of this approach for users with millions of followers?
Sets & Sorted Sets in Redis Redis Memory Optimization
Q6 Advanced Save

What are the internal memory encoding optimizations Redis applies to small collections, and how do they affect performance and memory usage?

Redis automatically uses more compact internal encodings, such as a listpack, for small hashes, lists, sets, and sorted sets that stay below certain configurable size thresholds, which significantly reduces memory usage compared to the standard encoding, though operations can become slightly slower once a collection grows past that threshold and converts to its regular, more memory intensive encoding.
Example

-- Check the internal encoding currently used for a key OBJECT ENCODING myhash CONFIG GET hash-max-listpack-entries

Real-world example
A team storing many small hashes for user preferences notices significant memory savings by ensuring their hashes stay below the configured listpack entry threshold, taking advantage of Redis's automatic compact encoding.
Likely follow-ups
  • How do you adjust the size thresholds that control when Redis switches encodings?
  • What happens to performance when a collection crosses this encoding threshold?
Redis Memory Optimization Data Types
Q7 Intermediate Save

How do you check which specific data type and internal encoding a given Redis key is currently using?

You use the TYPE command to see the basic data type of a key, such as string, hash, list, set, or zset, and the OBJECT ENCODING command to see the specific internal representation Redis is using to store that particular value, which can be helpful for understanding memory usage and performance characteristics.
Example

TYPE user:123 OBJECT ENCODING user:123

Real-world example
A developer troubleshooting unexpected memory usage checks the OBJECT ENCODING of several large hashes, discovering they had grown past the compact encoding threshold and switched to a more memory intensive internal representation.
Likely follow-ups
  • What are all the possible encoding values you might see for a hash or list?
  • Does checking a key's type or encoding count as a read operation?
Redis CLI & Basic Commands Redis Memory Optimization