Databases 7 min read

Implementing Like Functionality with Redis Bitmaps

This article explains how to use Redis bitmap operations to efficiently implement like, sign‑in, and other binary state features, covering bitmap fundamentals, common use cases, essential commands, Java code examples with Jedis, and range query techniques.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Implementing Like Functionality with Redis Bitmaps

Redis bitmaps are not a separate data type but a string (byte array) that can store up to 512 MB (2^32 bits), making them suitable for representing binary states such as likes, sign‑ins, online status, and custom Bloom filters.

Typical scenarios include user daily sign‑in tracking, online presence, active user statistics, various status flags, custom Bloom filters, and implementing like functionality for high‑traffic posts.

For example, storing a year's sign‑in record for a user requires only one bit per day; 365 bits occupy just 46 bytes, dramatically reducing storage compared with storing separate key/value entries for each day.

Basic Redis bitmap commands:

SETBIT key offset value      // set or clear a bit at the given offset
GETBIT key offset            // get the value of a bit at the given offset
BITCOUNT key                // count bits set to 1 in the string
BITPOS key bit [start] [end] // find the first occurrence of the given bit
BITOP operation destkey key [key ...] // perform AND/OR/NOT/XOR on one or more bitmaps
BITFIELD ...                // advanced bitfield operations (get/set/incrby)

Simple interactive usage:

# add bits
127.0.0.1:6379> setbit a 2 1
(integer) 0
127.0.0.1:6379> setbit a 3 1
(integer) 0
# read bits
127.0.0.1:6379> getbit a 2
(integer) 1
# count bits
127.0.0.1:6379> bitcount a
(integer) 2

Like feature implementation (Java + Jedis):

@Override
public boolean giveLike(String userId, Long postId) {
    try (Jedis jedis = redisUtil.getJedis()) {
        // set user‑to‑post like bit
        jedis.setbit(userId, postId, true);
        // set post‑to‑user like bit
        jedis.setbit(String.valueOf(postId), Long.valueOf(userId), true);
        threadPool.submit(new Runnable() {
            @Override
            public void run() {
                // asynchronously persist like info to the relational DB
                updateGiveLikeInfo(userId, postId);
            }
        });
        return true;
    } catch (Exception e) {
        return false;
    }
}

Canceling a like simply sets the corresponding bit to false . Counting likes for a user or a post uses jedis.bitcount(key) , and the total can be displayed directly on the page.

Range statistics (e.g., sign‑in periods):

Use BITPOS key bit start to find the first set/unset bit after a given offset.

Use BITPOS key bit start end for bounded searches.

Combine multiple bitmaps with BITOP (AND, OR, NOT, XOR) to aggregate statistics across days or users.

In short, a simple bitmap in Redis can efficiently implement likes, sign‑ins, and many other binary‑state features; adapt the patterns to your own requirements and enjoy the performance and storage benefits.
backendJavadatabaseRedisbitmaplike-system
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.