Databases 6 min read

How to Use Redis to Count Website Visits: Hash, Bitset, and Probabilistic Algorithms

This article explains three Redis-based approaches—using Hashes, Bitsets, and the HyperLogLog probabilistic algorithm—to count daily website visits, detailing command usage, advantages, disadvantages, and implementation considerations for high‑traffic sites, including handling logged‑in and anonymous users, memory consumption, and accuracy trade‑offs.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
How to Use Redis to Count Website Visits: Hash, Bitset, and Probabilistic Algorithms

Using Hash

Redis hash is a basic data structure where each key maps to a hash table; on collisions a linked list is used.

When a user visits, use their user ID (or a randomly generated key for anonymous users) as the field and a composite key of URI and date as the Redis key; the HSET command stores a value of 1.

To get the daily visit count for a page, the HLEN command returns the number of fields, i.e., unique visitors.

Advantages: Simple, easy to implement, fast queries, high data accuracy.

Disadvantages: High memory usage; performance degrades as the number of keys grows, making it unsuitable for billions of page views.

Using Bitset

By representing each user ID as a single bit in a bitmap, memory usage can be reduced dramatically; for example, 100 million users require only about 12 MB.

Redis provides the SETBIT command to mark a user as having visited a page and GETBIT to check the status; BITCOUNT aggregates the total number of set bits for the day.

Advantages: Very low memory consumption, fast queries, can target individual users.

Disadvantages: May consume more memory than the hash method when user IDs are sparse, and requires a mapping for anonymous users.

Using Probabilistic Algorithm

For extremely large traffic where exact counts are unnecessary, Redis’s HyperLogLog ( PFADD / PFCOUNT ) offers cardinality estimation with about 0.81 % error and uses roughly 12 KB per key.

Advantages: Minimal memory footprint, suitable for massive user bases.

Disadvantages: Cannot retrieve individual user information and introduces a small counting error.

These three methods provide trade‑offs between accuracy, memory usage, and scalability for counting website visits.

backend developmentRedisdata structuresweb-analyticsProbabilistic Algorithms
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.