Quick revision
The short answer
Strings hold cached values and counters (GET, SET, INCR, all O(1)); hashes hold an object's fields (HSET, HGET O(1), HGETALL O(n)); lists act as queues or recent-item lists (LPUSH, RPOP O(1), LRANGE O(s+n)); sets hold unique members (SADD, SISMEMBER O(1), SINTER); sorted sets power leaderboards and time indexes (ZADD and ZRANK O(log n)); streams are append-only logs with consumer groups (XADD, XREADGROUP); bitmaps store one flag per user id (SETBIT, BITCOUNT); and HyperLogLog counts unique items approximately, with about 0.81 percent error in 12 KB (PFADD, PFCOUNT). In production we avoid O(n) commands on large keys and use SCAN and HSCAN instead.
What to remember
In this answer, we saw that Redis executes commands on one main thread, so each data type's command costs matter. Strings serve cached values and counters, hashes serve object fields, lists serve queues and recent items, sets serve unique members, sorted sets serve leaderboards and time indexes, streams serve event logs with consumer groups, bitmaps serve one flag per id, and HyperLogLog serves approximate unique counts in about 12 KB. We also saw why KEYS and O(n) commands on large keys should be replaced by SCAN and HSCAN in production.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Strings hold cached values and counters (GET, SET, INCR, all O(1)); hashes hold an object's fields (HSET, HGET O(1), HGETALL O(n)); lists act as queues or recent-item lists (LPUSH, RPOP O(1), LRANGE O(s+n)); sets hold unique members (SADD, SISMEMBER O(1), SINTER); sorted sets power leaderboards and time indexes (ZADD and ZRANK O(log n)); streams are append-only logs with consumer groups (XADD, XREADGROUP); bitmaps store one flag per user id (SETBIT, BITCOUNT); and HyperLogLog counts unique items approximately, with about 0.81 percent error in 12 KB (PFADD, PFCOUNT). In production we avoid O(n) commands on large keys and use SCAN and HSCAN instead.
Why this question is asked
Redis is often introduced as a cache, but its real strength is that it gives us ready-made data structures that live in memory and are changed by single, fast commands. Choosing the right structure turns a problem that would need several round trips and application logic into one command. Interviewers ask this question to see whether we know the toolbox and, just as important, whether we know the cost of each command, because a single slow command blocks the whole server. In this answer, we will recap how Redis runs commands, walk through each data type with a realistic use and its commands, and then look at the commands to avoid in production.
1A Quick Recap: One Thread, Many Structures
Redis keeps all data in memory and executes commands one at a time on a single main thread. That design makes each command atomic, since nothing else runs while it executes, and it makes small commands extremely fast. But it also means the cost of every command matters: a command that walks a million elements holds up every other client until it finishes.
That is why time complexity is part of the answer. O(1) means the cost does not depend on the size of the key, O(log n) grows slowly with size, and O(n) grows with the number of elements, which is fine for small keys and dangerous for large ones.
2Strings and Hashes
A string is the simplest type: a key holding a value of up to 512 MB, which can be text, JSON, or a number. Its typical uses are cached values and counters. INCR adds one atomically, so many app instances can count page views or rate-limit hits without any locking.
A hash is a small map of fields inside one key, which suits an object such as a user profile. We can read or change a single field with HGET or HSET in O(1), without rewriting the whole value as we would with a JSON string. HGETALL returns every field and is O(n), harmless for a profile with ten fields and harmful for a hash with millions.
3Lists and Sets
A list is an ordered sequence, efficient at both ends. LPUSH and RPOP in O(1) make a simple queue, and LPUSH with LTRIM keeps the last 100 items of an activity feed. LRANGE reads a range in O(s+n), where s is the distance from the start and n the number of elements returned, so reading the first ten is cheap and paging deep into a huge list is not.
A set holds unique members with no order. SADD and SISMEMBER are O(1), which makes sets ideal for tags, unique visitors of a small page, or the members of a group. SINTER and SUNION combine sets on the server; SINTER costs roughly the size of the smallest set times the number of sets, so it is fast when one set is small.
4Sorted Sets and Streams
A sorted set is like a set where every member has a numeric score, and members are kept ordered by that score. ZADD inserts or updates in O(log n), and ZRANK finds a member's position in O(log n). It is the natural structure for leaderboards, and for time-ordered indexes where the score is a timestamp, such as jobs scheduled to run at a given time.
A stream is an append-only log of entries, each with an id based on time. XADD appends an entry. Consumer groups let several workers share the entries, with each entry delivered to one worker and tracked until it is acknowledged with XACK. That makes streams a light event log or work queue that, unlike a list, keeps entries after reading and survives a worker crash.
5Bitmaps and HyperLogLog
A bitmap is not a separate type but a string treated as an array of bits. With SETBIT we can store one bit per user id, for example whether user 1,000,000 was active today, in about 125 KB for a million users. BITCOUNT counts the set bits, giving daily active users, and BITOP AND across days gives users active on every day.
| Type | Realistic use | Main commands | Cost |
|---|---|---|---|
| String | cached value, counter | GET, SET, INCR | O(1) |
| Hash | user profile fields | HSET, HGET / HGETALL | O(1) / O(n) |
| List | queue, recent items | LPUSH, RPOP / LRANGE | O(1) / O(s+n) |
| Set | unique tags, members | SADD, SISMEMBER, SINTER | O(1), SINTER by sizes |
| Sorted set | leaderboard, time index | ZADD, ZRANGE REV, ZRANK | O(log n) per update |
| Stream | event log, work queue | XADD, XREADGROUP, XACK | O(1) append |
| Bitmap | daily active flag per id | SETBIT, GETBIT, BITCOUNT | O(1) / O(n) count |
HyperLogLog | approximate unique count | PFADD, PFCOUNT | about 12 KB, 0.81% error |
HyperLogLog answers how many distinct items we have seen, approximately, in at most about 12 KB per key, whatever the count, with a standard error of about 0.81 percent. PFADD adds items and PFCOUNT returns the estimate. For counting unique visitors per page per day across millions of pages, it uses a tiny fraction of the memory a set would need, at the cost of not being able to list the members.
6Commands to Avoid on Large Keys
Because of the single thread, the dangerous commands are the O(n) ones on big data: KEYS, which walks every key in the database, and HGETALL, SMEMBERS, LRANGE 0 -1 or DEL on keys with millions of elements. Instead we iterate in pages with SCAN for keys and HSCAN, SSCAN or ZSCAN inside a key. These return a cursor and a small batch each call, so other clients get their turn between calls.
7Follow-up questions
Why is KEYS dangerous in production?
KEYS scans every key in the database in one command, which is O(n) in the total number of keys. On an instance with 50 million keys it can block the main thread for seconds, and every other client, including the application's cache reads, waits for it. SCAN does the same job incrementally with a cursor, so it never blocks for long, at the cost of possibly returning a key twice.
When would we choose a hash over a JSON string?
When we read or update individual fields often. A hash lets us change one field with HSET or HINCRBY without reading and rewriting the whole object, which also avoids races. A JSON string is simpler when we always read and write the object as a whole, as in a typical cache entry.
Is a list or a stream better for a job queue?
A list with LPUSH and BRPOP is simple, but an entry is gone once popped, so a crashed worker loses its job unless we use the LMOVE pattern. A stream with a consumer group keeps entries, tracks which are pending for each consumer and lets another worker claim them, which makes it more robust for work that must not be lost.
8Conclusion
In this answer, we saw that Redis executes commands on one main thread, so each data type's command costs matter. Strings serve cached values and counters, hashes serve object fields, lists serve queues and recent items, sets serve unique members, sorted sets serve leaderboards and time indexes, streams serve event logs with consumer groups, bitmaps serve one flag per id, and HyperLogLog serves approximate unique counts in about 12 KB. We also saw why KEYS and O(n) commands on large keys should be replaced by SCAN and HSCAN in production.
Open with
Redis gives us data structures that live in memory and are changed by single atomic commands on one main thread, so I always think about both the use and the cost.
Then, if they want more
Strings are for cached values and counters: GET, SET with an expiry, INCR, all O(1). Hashes hold an object's fields, like a user profile: HSET and HGET are O(1), but HGETALL is O(n). Lists are simple queues or recent-item lists: LPUSH and RPOP are O(1), LRANGE is O(s plus n).
Sets hold unique members like tags, with SADD and SISMEMBER in O(1) and SINTER to combine sets. Sorted sets keep members ordered by a score, which is perfect for leaderboards or time-based indexes: ZADD, ZRANGE with REV, ZRANK, with updates in O(log n). Streams are append-only logs with consumer groups, using XADD and XREADGROUP, for event logs or work queues.
Bitmaps store one bit per id, like daily active users with SETBIT and BITCOUNT. HyperLogLog gives approximate unique counts with about 0.81 percent standard error in at most 12 KB per key, using PFADD and PFCOUNT. In production I avoid O(n) commands on large keys, especially KEYS, and use SCAN, HSCAN and friends instead.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Name the core Redis data types and give one realistic use for each: strings, hashes...": https://scrivio-interview-library.netlify.app/databases/redis/d96d37ee58/