Counting Web Page Visits with Redis: Hash, Bitset, and HyperLogLog Techniques
This article explains three Redis-based techniques—Hash, Bitset, and HyperLogLog—for efficiently counting daily page visits, detailing command usage, memory trade‑offs, and accuracy considerations, helping backend engineers implement scalable visitor statistics in high‑traffic environments like large e‑commerce platforms.
Pinduoduo offers high salaries and intense work, and its interview includes a question: how to use Redis to count website user visits?
Using Hash
Hash is a basic Redis data structure that maps keys to a hash table; on collisions it creates a linked list.
When a user visits, use the user ID if logged in, otherwise generate a random key to identify the user. Use the HSET command with a key composed of the URI and date, a field of the user ID or random identifier, and a value of 1.
To obtain the daily visit count for a page, use HLEN on the hash.
Pros: Simple, easy to implement, fast queries, high data accuracy.
Cons: High memory consumption; performance degrades as keys increase, making it unsuitable for billions of page views.
Using Bitset
A 32‑bit integer can represent 32 users when each bit denotes a user. Converting IDs to bits saves 32× memory. For large datasets, a bitset can drastically reduce memory usage; for 100 million users it requires about 12 MB.
Redis provides SETBIT to mark a user’s visit, GETBIT to check if a user visited, and BITCOUNT to count daily visits.
Pros: Very low memory usage, fast queries, can query specific users.
Cons: Slight data inaccuracies for anonymous users; requires mapping non‑logged‑in users to IDs, adding overhead.
Using Probabilistic Algorithm
When exact counts are unnecessary, HyperLogLog provides an approximate cardinality with minimal memory (about 12 KB per key) and a typical error of ~0.81%.
Use PFADD on each visit and later retrieve the estimate with PFCOUNT.
Pros: Extremely low memory, suitable for massive traffic sites.
Cons: Cannot query individual users; results have a small error margin.
These three methods—Hash, Bitset, and HyperLogLog—cover common approaches to counting website visits with Redis.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
