Design a Real-Time Analytics Platform
Understanding the Problem
📊 What is a real-time analytics platform? A product analytics platform collects events from websites and mobile apps (page views, clicks, sign-ups, purchases) and lets teams ask questions about them: how many people used a feature today, where users drop out of the sign-up flow, how many come back a week later. Google Analytics, Amplitude, Mixpanel and PostHog are examples. "Real-time" means the numbers on the dashboard move within seconds of the events happening, not after a nightly batch job.
This question comes in several forms: "design clickstream analytics," "design Google Analytics," "design a system with high ingestion and low-latency analytics," "show a histogram of clicks per hour," or "design the client SDK so events survive the device going offline." They're the same system. The histogram is the simplest query it has to answer, and the SDK is its first component. We'll design the whole pipeline, from the SDK on a phone in airplane mode to a funnel chart over a billion events. If you've seen the ad click aggregator, the ingest side will feel familiar; what's different here is that queries are open-ended, not a fixed set of counters.
Functional Requirements
Core Requirements
- Web and mobile apps should be able to send events through an SDK, including from devices that are offline for minutes or days.
- Users should be able to watch real-time dashboards: active users right now, events per minute, clicks per hour as a histogram, updating within seconds.
- Users should be able to run ad-hoc analyses over history: event counts broken down by any property, unique users, funnels and retention.
Below the line (out of scope)
- Session replay and heatmaps.
- Exporting raw data to a customer's warehouse (we'll mention where it would plug in).
- Billing, user management and the SDK's UI-autocapture features.
Non-Functional Requirements
Ask for scale. We'll assume a multi-tenant SaaS: 1 million events per second on average, 3 million at peak (evenings, big launches, sales events), from hundreds of millions of devices. Events average about 1 KB as JSON on the wire.
Core Requirements
- The system should accept 3M events per second at peak, and never lose an event once it has acknowledged it.
- Dashboards should reflect new events within about 10 seconds; ad-hoc queries should see them within a minute.
- Dashboard tiles should load in under 1 second; ad-hoc queries over 30 days for one project in under 5 seconds at p95.
- Counts should not double-count retried events. Unique-user counts over large ranges may be approximate (within about 1%) unless the user asks for exact.
- Events that arrive late, from devices that were offline, must be counted in the hour they actually happened.
Below the line (out of scope)
- Sub-second freshness. A few seconds is fine for product analytics.
- Strict ordering of events across devices.
The math. 1M events per second is 86 billion events a day, about 86 TB a day of raw JSON. In a columnar store with dictionary encoding and compression, an event typically shrinks to on the order of 100 bytes, so it's more like 8-9 TB a day, and a year is around 3 PB. That is a lot, but object storage and columnar engines handle it. The harder number is query load: a popular dashboard with 20 tiles, refreshed every 10 seconds by 50 viewers, is 100 queries a second, each of which would scan billions of rows if run against raw events. Dashboards have to read pre-aggregated data; only ad-hoc exploration should touch raw events.
There are two very different query patterns here, and the design follows from separating them. Dashboards ask the same questions over and over and need answers in milliseconds, so precompute them. Exploration asks new questions every time and can wait a few seconds, so make raw scans fast.