How to Use Redis Bitmap for Efficient User Sign‑In Tracking
This article explains why storing daily sign‑in data in a traditional database can cause performance issues at scale and demonstrates how Redis bitmap, a memory‑efficient bit‑level structure, can record and compute continuous user sign‑ins both by date and by user.
Understanding Redis Bitmap
Bitmap is composed of individual binary bits, each storing either 0 or 1. Redis implements bitmap as a string type, which can hold up to 512 MB, equivalent to about 4.2 billion bits, making it suitable for billions of daily active users.
Implementing User Continuous Sign‑In with Bitmap
Two storage dimensions are used: date‑based and user‑based.
1. Date‑Based Statistics
For a given month (e.g., December), each day is a separate Redis key, and a user's numeric ID is used as the bit offset. If the ID is non‑numeric, a hash of the ID combined with a random number can be used. To record a sign‑in, set the corresponding bit to 1. Counting sign‑ins for a specific user involves scanning the bits of the month’s keys from the last day backward, incrementing a counter for each 1 encountered (resetting on 0 for continuous streaks).
This method efficiently aggregates daily sign‑ins for all users, but counting a single user's streak still requires iterating over each day's key.
2. User‑Based Statistics
Each user receives an individual bitmap where the key is the user ID and each bit position represents a date. For example, if user ID 8 signs in for five consecutive days, the bitmap will have five consecutive 1s.
This approach is ideal for quickly retrieving a single user's continuous sign‑in streak, though aggregating sign‑ins across all users requires scanning every user's bitmap, which is less efficient.
Summary
Bitmap stores binary data (0/1) at the bit level, offering extremely low memory consumption and fast statistics. It is well‑suited for binary scenarios such as login, sign‑in, or likes when dealing with massive datasets.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
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.