KatInterview

Real-Time Sliding Window Visit Counter

System DesignModel answer
HardModel answerSystem Designstreamingrediskafka

Last updated

Design a system that tracks website page views over a rolling 60-second window and serves the current count with low latency at high traffic volumes.

Real-Time Sliding Window Visit Counter

Your task is to design a backend system that answers one question at any moment: how many page views has a website received in the last 60 seconds?

The site handles heavy traffic with sharp spikes. The count must be queryable in near real time — a few seconds of lag is acceptable, but not minutes.

What to cover

Define the metric precisely What counts as a page view? How do you handle bots, retries, and duplicate beacons? When does a unique-visitor count make more sense than a raw page-view count, and what does that change in the design?

Ingestion pipeline How do browser events reach your system? What happens when the event bus is temporarily unavailable? Walk through the path from browser beacon to durable storage.

Maintaining the rolling window Describe the core data structure and algorithm you use to compute the count for the most recent 60 seconds. Justify why you chose it over the alternatives.

Storage choices What do you write to, and why? Consider both the hot path (serving live counts) and the cold path (reconciliation and historical analysis).

Accuracy, deduplication, and late events How do you avoid double-counting retried events? What timestamp do you use for bucketing — client time or server time — and why? What is your policy for events that arrive after their window has closed?

Scaling and fault tolerance Walk through the back-of-envelope numbers. Where are the bottlenecks? How does the system behave when a consumer crashes and replays a batch of events?

Monitoring What signals tell you the count is drifting or the pipeline is falling behind?

Constraints and assumptions to state

You are expected to state your own assumptions about traffic volume, acceptable staleness, and accuracy tolerance before designing. A strong answer picks concrete numbers and defends them.

Hints

Hint 1

Before drawing any architecture, nail down the definition of 'visit'. Asking whether unique visitors or raw page views are needed changes the data structure entirely — a counter versus a cardinality estimator like HyperLogLog.

Hint 2

Think about why scanning raw timestamps per query is impractical at high traffic, and how pre-aggregating into one-second buckets reduces the query to a fixed-cost O(60) sum regardless of event rate.

Hint 3

Idempotency is the key to crash safety. Ask yourself: if a consumer replays 10 seconds of events after a restart, does the count stay correct? That question should drive your choice of how you write to your store.