Quick revision
The short answer
We give each user a dense integer index and run SETBIT dau:<date> <index> 1 on activity; BITCOUNT on that key is the day's active users. Seven-day actives is BITOP OR of the seven daily keys followed by BITCOUNT, and day-1 retention is BITCOUNT of BITOP AND of two days divided by BITCOUNT of the first day. Memory depends on the highest index, not on activity: 100 million users need 12.5 MB per daily key.
What to remember
In this answer, we stored one bit per user per day with SETBIT on dense integer indexes, counted daily actives with BITCOUNT, weekly actives with BITOP OR followed by BITCOUNT, and day-1 retention as the BITCOUNT of a BITOP AND divided by the first day's count. Memory depends on the highest index, so 100 million users cost 12.5 MB per daily key. We map UUIDs to sequential indexes, never use huge offsets, run whole-key BITOP and BITCOUNT on replicas or off-peak, and give the keys TTLs.
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.
We give each user a dense integer index and run SETBIT dau:<date> <index> 1 on activity; BITCOUNT on that key is the day's active users. Seven-day actives is BITOP OR of the seven daily keys followed by BITCOUNT, and day-1 retention is BITCOUNT of BITOP AND of two days divided by BITCOUNT of the first day. Memory depends on the highest index, not on activity: 100 million users need 12.5 MB per daily key.
Why this question is asked
Daily and weekly active users and retention are among the first numbers any product team asks for, and they need to be fresh. Redis bitmaps answer them with one bit per user, which is both tiny and exact, as long as we prepare the ids correctly. Interviewers ask this to see whether we can do the arithmetic and know the traps: sparse ids, huge offsets and slow commands on the main thread. In this answer, we will recap what a bitmap is, record activity, compute active users and retention, work out the memory, deal with ids that are not small integers, and keep the expensive commands away from production traffic.
1A Quick Recap of Bitmaps
Redis has no separate bitmap type. A bitmap is a string, and the bit commands treat its bytes as an array of bits, so bit 0 is the first bit of the first byte. SETBIT key offset value sets one bit, growing the string as needed. GETBIT reads one. BITCOUNT counts the bits set to 1. BITOP AND, OR, XOR or NOT combines several keys into a destination key, bit by bit.
If bit n means 'user n was active', then one string per day records every user's activity for that day, and set operations on users become bit operations on strings.
2Recording Activity and Counting Daily Actives
On every meaningful action, we set the user's bit in today's key. Setting a bit that is already 1 changes nothing, so we can do it on every request without counting duplicates.
SETBIT returns the bit's previous value, so the first call returns 0 and the second returns 1; that return value tells us cheaply whether this is the user's first action of the day. BITCOUNT gives an exact count.
3Weekly Actives and Retention With BITOP
A user is weekly active if they were active on any of the seven days, which is an OR. A user is retained from day A to day B if they were active on both, which is an AND.
- User actsSETBIT dau:<day> <index> 1
- Daily activesBITCOUNT dau:<day>
- Weekly activesBITOP OR over 7 days, then BITCOUNT
- Retained usersBITOP AND day A, day B, then BITCOUNT
- Retention rateretained / BITCOUNT day A
5 steps in order. Play them, or step through them one at a time.
Notice that BITOP returns the size of the destination string, not a count; we always follow it with BITCOUNT. The results are exact, which is the big difference from HyperLogLog. For retention of a sign-up cohort, we AND the cohort's bitmap (users who signed up on day A) with the activity of day B in the same way.
4Working Out the Memory
A bitmap's size depends on the highest offset set, not on how many bits are 1. With 100 million users indexed from 0, the highest offset is about 100 million, so each daily key is 100,000,000 / 8 = 12,500,000 bytes, 12.5 MB, whether one user or all of them were active. Ninety days of daily keys take about 1.1 GB, plus weekly keys of the same size each.
That is cheap for 100 million users, but it means a low-traffic metric is not cheaper: a feature used by 1,000 users with high indexes still costs 12.5 MB per day. For sparse cases like that, a set or a HyperLogLog is smaller.
5Getting Dense Integer Indexes
Bit offsets must be integers, and they work well only when dense, meaning users are numbered 0, 1, 2 and so on without big gaps. Our user ids may be UUIDs, or numeric ids with large gaps. We map each user to a sequential index once, for example with INCR user:index:seq on registration and a lookup table, either a hash (HSET user:index <uuid> <n>) or a column in the users table.
Never use a large or hashed number as an offset. The maximum offset is 2^32 - 1, which means a 512 MB string, and Redis allocates the whole range up to the offset at once. A single SETBIT with offset 4,000,000,000 creates a 500 MB key and blocks the server while it does so.
6Running Heavy Commands Safely
SETBIT, GETBIT and BITCOUNT on a range are cheap, but BITCOUNT and BITOP over whole 12.5 MB keys are O(N) and run on Redis's single main thread. BITOP over seven such keys reads 87.5 MB and writes 12.5 MB; it takes milliseconds to tens of milliseconds, during which every other client waits. So we run reports on a replica or off-peak, cache their results (the weekly count does not change once the week is over), and give every daily and temporary key a TTL so old bitmaps do not pile up.
7Follow-up questions
When would a HyperLogLog be the better choice?
When ids are sparse or not integers and we do not want to maintain a mapping, when we need counts for many small groups such as unique visitors per article, where a bitmap per article would be mostly empty but full size, or when approximate counts are acceptable. HyperLogLog costs at most 12 KB per key regardless of id range, but it cannot do exact counts, intersections or per-user lookups.
How would you compute 'active on at least 5 of the last 7 days'?
Bit operations alone cannot count per position, so we would either read the bitmaps into the application in chunks with GETRANGE and count per user there, or run the calculation in the data warehouse. Redis bitmaps are best for OR, AND and total counts.
Can BITCOUNT count only part of a bitmap?
Yes. BITCOUNT key start end counts within a byte range, and from Redis 7 a BIT option lets the range be given in bits. That lets us count, for example, only the users with indexes in a range that corresponds to one region, if we assigned indexes that way.
8Conclusion
In this answer, we stored one bit per user per day with SETBIT on dense integer indexes, counted daily actives with BITCOUNT, weekly actives with BITOP OR followed by BITCOUNT, and day-1 retention as the BITCOUNT of a BITOP AND divided by the first day's count. Memory depends on the highest index, so 100 million users cost 12.5 MB per daily key. We map UUIDs to sequential indexes, never use huge offsets, run whole-key BITOP and BITCOUNT on replicas or off-peak, and give the keys TTLs.
Open with
Each user gets a dense integer index, say from INCR on a sequence at sign-up, because bit offsets must be integers. When a user is active I run SETBIT dau:2026-11-12 with their index and 1, and daily active users is BITCOUNT on that key, exact.
Then, if they want more
Weekly active users is BITOP OR wau:2026-W46 over the seven daily keys, then BITCOUNT on the result, since a user counts if they were active on any day. Day-one retention from day A to day B is BITOP AND into a temporary key, BITCOUNT of that, divided by BITCOUNT of day A.
For memory, a bitmap's size depends on the highest offset, not on how many users were active, so 100 million users is 100 million divided by 8, 12.5 megabytes per daily key, and around a gigabyte for 90 days.
The traps are ids: UUIDs or sparse ids need a mapping to sequential numbers, and one very large offset allocates the whole range at once, up to 512 megabytes at the 2 to the 32 bit limit.
Also BITOP and BITCOUNT over whole keys are O(N) on the main thread, so I'd run reports on a replica or off-peak, cache finished weeks, and put TTLs on the daily keys.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Redis 7
- 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 "Use SETBIT, BITCOUNT and BITOP to record daily active users and compute 7-day active...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/bc5c28578c/