Implementing Page UV Counting with Redis Set, Hash, Bitmap, and HyperLogLog Using Redisson

This article explains how to use Redis data structures—Set, Hash, Bitmap, and HyperLogLog—along with Redisson in Java to efficiently count unique page visitors (UV) in high‑traffic mobile internet scenarios, comparing memory usage, precision, and scalability.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Implementing Page UV Counting with Redis Set, Hash, Bitmap, and HyperLogLog Using Redisson

In mobile internet scenarios with massive data, we often need to associate a key with a collection and perform cardinality statistics such as daily active users (DAU), unique visitors (UV), search term counts, and registration IP counts.

The article demonstrates four Redis data structures—Set, Hash, Bitmap, and HyperLogLog—to implement UV counting for a page, gradually introducing each method and its trade‑offs.

Using Set

Because a user visiting multiple times in a day should be counted once, a Redis Set can store unique user IDs. Example commands:

SADD Redis为什么这么快:uv 肖菜鸡 谢霸哥 肖菜鸡
SCARD Redis为什么这么快:uv

SCARD returns the number of unique IDs.

Using Hash

Store each user ID as a hash field with value 1; HLEN gives the UV count.

HSET Redis为什么这么快 肖菜鸡 1
HLEN Redis为什么这么快

Using Bitmap

Bitmap uses a bit array where each bit represents a user. SETBIT writes a bit, BITCOUNT counts bits set to 1. User IDs must be converted to numeric offsets.

SETBIT 巧用Redis数据类型实现亿级数据统计 6 1
BITCOUNT 巧用Redis数据类型实现亿级数据统计

HyperLogLog

HyperLogLog provides probabilistic cardinality estimation with fixed 12 KB memory regardless of set size, suitable for millions‑to‑billions of elements. Commands PFADD, PFCOUNT, and PFMERGE are used.

PFADD Redis主从同步原理:uv userID1 userID2 userID3
PFCOUNT Redis主从同步原理:uv
PFMERGE database Redis数据 MySQL数据
PFCOUNT database

Redisson Integration

Java code shows how to add elements, addAll, merge, and count using Redisson's RHyperLogLog API.

public <T> void add(String logName, T item) {
    RHyperLogLog<T> hyperLogLog = redissonClient.getHyperLogLog(logName);
    hyperLogLog.add(item);
}

public <T> void addAll(String logName, List<T> items) {
    RHyperLogLog<T> hyperLogLog = redissonClient.getHyperLogLog(logName);
    hyperLogLog.addAll(items);
}

public void merge(String logName, String... otherLogNames) {
    RHyperLogLog<T> hyperLogLog = redissonClient.getHyperLogLog(logName);
    hyperLogLog.mergeWith(otherLogNames);
}

public long count(String logName) {
    RHyperLogLog<T> hyperLogLog = redissonClient.getHyperLogLog(logName);
    return hyperLogLog.count();
}

Summary

Set offers simple deduplication but high memory for large cardinalities; Hash gives high precision for moderate data; Bitmap is efficient for binary‑state statistics; HyperLogLog achieves near‑constant memory with acceptable error (~0.81 %).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HyperLogLogredisHashredissonSetUV counting
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

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.