Active Users in the Last N Minutes

Sliding window of distinct users

Design a counter that answers how many distinct users were active in the last N minutes, with an exact version for one fixed window and a bucketed version for any window.

Overview

Every dashboard has a number like 'users active in the last 5 minutes'. Events arrive all the time: a page view, a click, a heartbeat, each with a user id and a time. The system must answer, at any moment, how many distinct users sent at least one event inside the window. Counting events is easy; counting distinct users in a window that keeps sliding is the real problem.

Interviewers use it to see whether you pick the data structure from the question. If the window is one fixed size, you only need each user's last active time, kept in time order, and the count is almost free. If callers may ask for any window up to an hour, you need the history split into time buckets. If there are hundreds of millions of users, you stop storing ids and estimate with a HyperLogLog sketch. A strong answer asks which case applies before writing code.

Scoping the problem

Ask these questions before you write code; each answer changes the data structure. The usual answers set the course for the rest of the chapter.

  • One fixed window, or any window up to a maximum? A fixed window needs only one ordered map; any window needs time buckets. Start with a fixed 5 minutes, then any window up to an hour.
  • Distinct users or events? Distinct users need a set or an ordered map; events need only counters. The question is about distinct users.
  • Exact, or is about 1% error fine? Exact stores every active id; approximate uses a fixed-size sketch. Exact for now, approximate at scale.
  • Do events arrive in time order? The ordered-map design needs it; buckets tolerate late events. Usually they arrive in order from one server's clock.
  • Do you also need the ids, or only the count? A sketch gives only a count. Usually the count, sometimes the ids.
  • One process, or many servers? Many servers need a merge step that does not double count. Start with one process.

Functional requirements

  • record(user, ts) notes that a user was active at time ts
  • count(now) returns the number of distinct users with an event in the window ending at now
  • The fixed-window counter answers one window size; the bucketed counter answers any whole number of buckets up to max_window
  • users(now, window) returns the ids themselves, for the bucketed counter
  • Refuse what the design cannot answer correctly: timestamps that go backwards in the fixed-window counter, windows that are not whole buckets

Non-functional requirements

  • Fixed window: record O(1), count O(1) amortized, memory O(users active in the window)
  • Buckets: record O(1), count O(total ids in the window buckets), memory bounded by max_window
  • Thread safe: each counter guards its state with one lock
  • Time is injected, so tests are exact and repeatable

Interview questions

  • Why not keep a list of every event and scan it?
  • How do you pick the bucket size?
  • There are 200 million users. What changes?
  • Ten servers each see some of the traffic. How do you get one number?
  • Is one lock per counter enough? What about the clock?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio