Using Redis to Count Website Visits: Hash, Bitset, and Probabilistic Algorithms
The article explains three Redis-based techniques—Hash, Bitset, and HyperLogLog—for counting daily page visits on high‑traffic sites, detailing command usage, memory trade‑offs, and the pros and cons of each method.
During a Pinduoduo backend interview, candidates are asked how to use Redis to count the number of visits to a specific webpage for a site with hundreds of millions of users.
Using Hash
Redis hashes store key‑field‑value pairs; each user visit can be recorded with HSET where the Redis key combines the page URI and date, the field is the user ID (or a generated identifier for anonymous users), and the value is set to 1. The total daily visits are obtained with HLEN . Advantages: simple, easy to implement, accurate results. Disadvantages: high memory consumption and performance degradation as the number of keys grows, making it unsuitable for massive traffic.
Using Bitset
A bitset represents each user as a single bit, drastically reducing memory usage (e.g., 100 million users require only about 12 MB). Redis provides SETBIT to mark a user’s visit, GETBIT to query a specific user, and BITCOUNT to count the total set bits for the day. Advantages: very low memory usage and fast queries. Disadvantages: potential memory waste for sparse data and the need for an extra mapping for anonymous users.
Using Probabilistic Algorithm
For approximate counts, Redis’s HyperLogLog can be used. Each visit triggers PFADD to add the user identifier to the HLL structure, and the daily unique visitor count is retrieved with PFCOUNT . This method uses roughly 12 KB per key, making it ideal for extremely large traffic, though it introduces about a 0.81 % error margin. Advantages: minimal memory footprint, suitable for billions of users. Disadvantages: cannot query individual users and results have a small statistical error.
Source: https://url.cn/5tQPEQg
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.