System design interview

Sessionization System for User Activity

HardLocked55 min read

Group user events into sessions with a 30-minute inactivity gap; handle out-of-order events, late data, and large keyed state.

system-designstreamingsessionizationevent-time

Start here

The problem in plain words

When you browse an app, your actions come in bursts: a few taps, a pause, a few more taps. Analysts group these bursts into sessions. A common rule is that a session ends after 30 minutes of inactivity. If you were quiet for 29 minutes and then tapped, it is the same session. At 31 minutes it is a new one.

On a sorted file this is easy: walk down the list and start a new session whenever the gap is too long. On a live stream it is hard. Phone events arrive late, shuffled, and sometimes in the wrong order, and you cannot wait forever to be sure nothing else is coming.

The design question is how to decide when a session is really over, without grading the network speed and without letting memory fill up with sessions that will never close.

Words you will meet

Session
A group of one user's events close together in time, ended by a long enough pause.
Inactivity gap
The length of silence that ends a session, here 30 minutes.
Event time
The time the event happened on the user's device.
Processing time
The time your system happened to receive the event. It depends on the network, so it is a poor basis for sessions.
Out-of-order event
An event that arrives after another event that actually happened later.
Watermark
The stream job's running estimate of "events up to this time have probably all arrived".
Allowed lateness
Extra time the job waits after the watermark before finally closing a session, to catch stragglers.
Keyed state
Memory the job keeps per user, such as the time of the last event and the current session id.
Side output
A separate stream for events that arrived too late, so they can be fixed later instead of being lost.
Idempotent sink
A destination where writing the same result twice leaves the same final data.
Why shuffled events can split a session wrongly
4 events10:55splitUser's phoneevents at 10:00,10:20, 10:40, 10:55Networkdelays and shufflesStream jobdecides sessionsTwo sessions?should have been one
Press Play to watch it move

Each arrow is one thing happening. Play it step by step, or read the list below.

  1. 1.The user taps at 10:00, 10:20, 10:40 and 10:55. Each gap is under 30 minutes, so this is one session.
  2. 2.Because of the network, the 10:55 event arrives before the 10:40 event.
  3. 3.If the job closes the session eagerly, it sees 35 minutes between 10:20 and 10:55 and splits into two sessions. When 10:40 shows up it is too late to fix.

The numbers, as things you can picture

  • Millions of open sessions at onceEvery active user has an open session, each holding some memory. The memory for open sessions is the scarce resource.
  • 30-minute gapThe rule that decides when one session ends and the next begins.

What the interviewer wants

Design a streaming system that assigns sessions by event time, copes with out-of-order and late events, scales to millions of open sessions, and writes results so that reprocessing does not create duplicates.

The interview setup

Design a streaming system that groups user events into sessions using a 30-minute inactivity gap. You must handle out-of-order events, late arrivals, and state for millions of concurrent users.

This is one of the classic stream processing interviews. Candidates who quietly sessionize on processing time invent fiction. Candidates who never say "watermark" get a follow-up that hurts. Candidates who ignore state size design a demo, not a system.

What the interviewer wants:

  • A crisp event-time definition of a session
  • A watermark and lateness policy
  • A plan for keyed state and TTL
  • A late-data story after sessions close
  • Deterministic identifiers so sinks can be idempotent

What is a session?

A session is a burst of activity by one user separated by idle time. If the gap between consecutive events in event time exceeds 30 minutes, start a new session.

Example: events at 1:00, 1:10, and 1:35 stay one session because the gaps are under 30 minutes. An event at 2:10 starts a new session.

Product uses sessions for funnel analysis, engagement time, attribution, and personalization. Wrong splits corrupt all of that.

Event time versus processing time

Event time is when the action happened according to the producer clock (or a trusted server timestamp assigned at the edge). Processing time is when your job saw the event.

Mobile phones go offline. Partitions lag. Retries reorder. If you close sessions with wall-clock now, you are measuring your pipeline health, not user behavior.

Watermarks

A watermark is the engine's working belief about event-time progress: "I think I have seen most data up to time T." Windows and sessions use that belief to decide when they can close.

Watermarks are heuristics. They can be wrong. Your design should admit that.

Allowed lateness

Allowed lateness keeps session state around after the watermark passes so late events can still join for a bounded time. Too large and state explodes. Too small and you split sessions incorrectly or drop updates.

Keyed state

Open sessions live in state keyed by user_id. Millions of open sessions means RocksDB-sized state, checkpoint costs, and a TTL story after close.

The expanded problem

Build a pipeline that:

  • Assigns session ids using a 30-minute inactivity gap in event time
  • Handles out-of-order events within a stated policy
  • Emits session aggregates when sessions close
  • Scales to millions of concurrent open sessions
  • Supports reprocessing after bugs

Constraints and scale prompts

  • State is the scarce resource, not raw bytes
  • Checkpoint frequency versus recovery time is a real trade-off
  • Mobile clock skew can poison event_time if unchecked
  • Downstream BI often wants stable session grains

What good looks like

  • Define the session in one sentence with event time
  • Draw watermark plus lateness
  • Estimate state for millions of keys
  • Explain what happens to very late events
  • Mention deterministic session ids and replay

Clarifying questions

  • Is event_time from the client clock or assigned at the ingest server?
  • How late is acceptable: minutes, hours, or a day?
  • Should late events revise closed sessions or only land in a corrections path?
  • Which aggregates are required on close: duration, event count, revenue?
  • Do we need exact replay for a bad day?

Out of scope

  • Full cross-device identity graph stitching (mention as a sequel)
  • Designing the mobile SDK in detail
  • Pixel-perfect dashboard layout
  • Exactly-once theology without a sink discussion (keep it practical)

How to use your clarifying questions

Ask two or three high-value questions early, not 12. Prioritize questions that change the diagram: latency SLA definition, source of truth, retention, and whether approximate answers are allowed. Park niche questions until the deep dive.

If the interviewer refuses to answer and says "you decide," state your assumption explicitly and proceed. Write the assumption on the board so it stays visible.