Binary State Statistics with Redis Bitmap: Efficient User Login and Sign‑In Tracking
This article explains how to use Redis Bitmap for binary state statistics such as user login status and daily sign‑in tracking, demonstrating memory‑efficient storage, relevant commands, and techniques for counting continuous sign‑ins across massive user bases.
In mobile application scenarios we often need to store a key associated with a large collection of binary state data, such as user login status or daily sign‑in flags.
Four statistical types are introduced, and this article focuses on the binary state type, using Redis Bitmap instead of traditional String, Set, Zset or List.
Redis String stores data with SDS overhead (len, alloc, buf) and a RedisObject header, which makes per‑key memory consumption large when handling millions of users.
Bitmap stores each element as a single bit, so one hundred million users require only about 12 MB of memory. The underlying storage still uses the SDS structure, but each byte holds eight bits.
Bitmap commands SETBIT and GETBIT allow setting and reading a bit at a given offset. By using the user ID as the offset, we can record login status with a single key, e.g. SETBIT login_status 10086 1 and query it with GETBIT login_status 10086 .
For monthly sign‑in tracking, a key pattern like uid:sign:{userId}:{yyyyMM} is used, with the day‑of‑month minus one as the offset. SETBIT records a sign‑in, GETBIT checks a specific day, BITCOUNT counts total sign‑ins, and BITPOS finds the first sign‑in day.
To count users who have signed in for seven consecutive days, store each day’s bitmap under a separate key, then apply BITOP AND to combine them into a new bitmap and use BITCOUNT to obtain the total.
The article concludes that when the statistic only requires a binary state, Bitmap provides a memory‑efficient solution for massive data sets.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.