Backend Development 12 min read

Implementing Sign‑In and Statistics with Redis BitMap in Spring Boot

This article explains how to use Redis BitMap to build an efficient sign‑in feature and monthly statistics in a Spring Boot microservice, covering basic BitMap commands, key design, code implementation, testing procedures, and a cache‑penetration mitigation strategy.

Top Architect
Top Architect
Top Architect
Implementing Sign‑In and Statistics with Redis BitMap in Spring Boot

In many projects a sign‑in and statistics feature is needed; using Redis BitMap can store each user's daily sign‑in status with minimal memory consumption.

Redis BitMap Basic Usage

The article first introduces the BitMap data structure and its memory advantage compared with traditional relational tables, then lists the essential Redis commands:

SETBIT : set a bit at a given offset to 0 or 1.

GETBIT : retrieve the bit value at a specific offset.

BITCOUNT : count bits set to 1.

BITFIELD : query or modify a range of bits.

BITFIELD_RO : read bit fields in decimal.

BITOP : perform bitwise operations on multiple BitMaps.

BITPOS : find the first occurrence of 0 or 1.

Spring Boot Integration – Sign‑In Feature

The implementation uses the current year‑month as the BitMap key (e.g., sign:5:202301 ) and stores a user’s sign‑in by setting the bit corresponding to the day of month to 1.

@PostMapping("sign")
public Result sign() {
    return userService.sign();
}
public Result sign() {
    Long userId = UserHolder.getUser().getId();
    LocalDateTime now = LocalDateTime.now();
    String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
    String key = RedisConstants.USER_SIGN_KEY + userId + keySuffix;
    int dayOfMonth = now.getDayOfMonth();
    stringRedisTemplate.opsForValue().setBit(key, dayOfMonth - 1, true);
    return Result.ok();
}

After deploying, the API can be tested with tools such as ApiFox, and the stored BitMap can be inspected directly in Redis.

Statistics – Continuous Sign‑In Days

To calculate the number of consecutive sign‑in days up to today, the article retrieves the month’s BitMap as an unsigned integer using BITFIELD , then iterates from the least‑significant bit, counting consecutive 1s until a 0 is encountered.

public Result signCount() {
    Long userId = UserHolder.getUser().getId();
    LocalDateTime now = LocalDateTime.now();
    String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyyMM"));
    String key = RedisConstants.USER_SIGN_KEY + userId + keySuffix;
    int dayOfMonth = now.getDayOfMonth();
    List
result = stringRedisTemplate.opsForValue().bitField(
        key,
        BitFieldSubCommands.create()
            .get(BitFieldSubCommands.BitFieldType.unsigned(dayOfMonth)).valueAt(0));
    if (result == null || result.isEmpty() || result.get(0) == null) {
        return Result.ok(0);
    }
    long num = result.get(0);
    int count = 0;
    while (true) {
        if ((num & 1) == 0) break;
        count++;
        num >>>= 1;
    }
    return Result.ok(count);
}

Testing shows the correct consecutive count (e.g., 2 days) and verifies the Redis values.

Using BitMap to Prevent Cache Penetration

The article also proposes a bitmap‑based solution for cache‑penetration attacks: store a bitmap where each bit represents the existence of a database record (e.g., id % bitmapSize ). A request for a missing ID will hit a 0 bit, allowing the system to reject the request early, while a 1 bit indicates the record may exist.

Conclusion

By leveraging Redis BitMap, developers can implement a lightweight sign‑in system, efficiently compute monthly statistics, and mitigate cache‑penetration risks, all within a Spring Boot microservice architecture.

backendRedisbitmapSpringBootCache PenetrationSign-in
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.