Real-Time Metrics Dashboard Architecture
System DesignModel answerLast updated
Build a dashboard that shows time-series metrics with clear separation between data ingestion delay and query response time.
You need to architect a dashboard that visualizes time-series metrics in real time. The system must handle two distinct latency concerns: how fast newly arrived data points appear on screen, and how quickly users can render or modify their visualizations.
Scope
Your system receives numbered data points, each tagged with a timestamp, metric identifier, and key-value labels (e.g., cpu_usage{host="web-1", region="us-east"}). Dashboard users select a metric, apply label filters, and choose a time window to generate a chart.
Design the complete flow: how data enters the system, where and how it's stored, what transformations occur, how queries execute, and how browsers stay current. You do not need to specify exact throughput numbers or retention policies, but your design should accommodate realistic scale.
Key Challenges
- Latency semantics: Define what "low latency" means for both data freshness and interactive performance, and explain how you'd measure each.
- Out-of-order arrival: Data points may arrive late or out of sequence.
- Query efficiency: A chart spanning hours or days shouldn't scan millions of individual measurements.
- Staleness detection: Browsers must recognize when displayed data is outdated.
- Overload scenarios: Handle ingestion spikes, slow queries, and high-cardinality label explosions.
Describe your architecture with enough detail to reason about where delays occur and how you'd verify latency targets are met.
Hints
Hint 1
Think about pre-aggregation: raw points work for short windows, but wider time ranges need summary buckets.
Hint 2
Freshness and query speed are independent—optimize them separately with different techniques.
Hint 3
Consider how a browser knows its chart is stale vs. current, especially after network interruptions.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
Defining the Two Latencies
Data freshness latency: Time from when a producer generates a measurement to when that point influences what a user sees on screen. Measure this end-to-end: producer timestamp → ingestion → storage write → query execution → browser render.
Query latency: Time from when a user requests a chart (or changes filters/time range) to when the browser displays results. Measure from API request received to HTTP response completed.
These are independent. A system might ingest data in under a second but take five seconds to render a wide historical chart, or vice versa.
Architecture Overview
Ingestion tier: Producers send measurements to a high-throughput write path (Kafka, Kinesis, or a direct write API). A stream processor or ingestion service batches writes and appends them to time-series storage.
Storage layer: Use a time-series database (Prometheus, InfluxDB, TimescaleDB, or a columnar store like ClickHouse). Data is partitioned by time and indexed by metric name and label combinations. Raw points are retained for recent windows (e.g., last hour), while older data is pre-aggregated into rollups (1-minute, 5-minute, 1-hour buckets).
Query tier: An API service receives dashboard requests, determines the appropriate resolution (raw vs. rollup), fetches matching series, and returns aggregated points. For a 7-day chart, the service queries 1-hour rollups instead of millions of raw samples.
Browser updates: Use WebSockets or Server-Sent Events to push incremental updates when new data arrives. The browser maintains a local chart state and appends new points without full re-queries. Include a staleness indicator: if no update arrives within 2× the expected interval, mark the chart as potentially stale.
Storage and Aggregation Strategy
Store raw measurements in time-sharded partitions. Every N seconds (e.g., 60), a background process computes rollups: for each unique metric + label set, aggregate raw points into buckets (min, max, avg, p50, p99, count). Write these summaries to separate rollup tables.
Resolution selection: The query planner chooses data granularity based on the requested time range. A 5-minute window uses raw points; a 24-hour window uses 1-minute rollups; a 30-day window uses 1-hour rollups. This bounds query work regardless of range width.
Late data: When a delayed measurement arrives, update the corresponding rollup bucket if it still exists in writable storage. If the rollup is already finalized (older than a cutoff window), either drop the late point or flag it for manual review. Display a warning in the UI when late-arriving data changes a visible chart.
Query Path and Performance
- Parse metric name, label filters, time range.
- Estimate cardinality: query the index for matching label combinations. Reject queries that would scan excessive series (e.g., >10,000 unique combinations).
- Determine rollup level from time span.
- Fetch data in parallel across time shards and series.
- Aggregate and downsample results to fit browser rendering limits (~1,000 points per chart).
- Cache query results for 5–10 seconds to serve multiple users viewing the same chart.
Query latency SLO: Target p95 < 2 seconds for typical charts, with circuit breakers that time out expensive queries after 10 seconds.
Browser Update Mechanism
After initial load, the browser subscribes to updates for the displayed metric and filters. The backend pushes new data points as they arrive. To handle disconnections:
- On reconnect, the browser sends its last-seen timestamp.
- The backend either streams missing points (if the gap is small) or returns a full re-query response (if the gap exceeds a threshold or rollups have changed).
- Show a visual indicator when the chart is stale: "Last updated 45s ago" or "Reconnecting…"
Handling Overload
Ingestion backpressure: If write throughput exceeds capacity, buffer in Kafka with increased retention, then scale ingestion workers horizontally. Monitor queue depth and alert when lag exceeds the freshness SLO (e.g., >5 seconds behind real-time).
High-cardinality explosion: Impose limits on label cardinality per metric. If a metric's unique label combinations exceed a threshold (e.g., 100K), either reject new label sets or sample them. This prevents unbounded index growth.
Expensive queries: Implement query cost estimation (cardinality × time range). Reject or rate-limit queries exceeding a budget. Offer query optimization hints: "This filter matches 50K series; add more specific labels."
Measuring Latency
Freshness: Producers embed generation timestamps. Ingestion workers record write timestamps. A synthetic canary producer emits known measurements and measures the delay until they appear in query results. Track p50, p95, p99.
Query performance: Log query execution time in the API tier. Break down into: index lookup, data fetch, aggregation, serialization. Dashboard histograms show per-component latency.
Common Pitfalls
- Conflating the two latencies: Optimizing query speed doesn't improve data freshness, and vice versa.
- Scanning raw data for wide ranges: Without rollups, a 30-day chart queries billions of points and times out.
- Ignoring late data: Silently dropping late arrivals can hide real issues; surfacing them helps users trust the dashboard.
- No staleness signal: Users assume live data when the backend has stopped updating, leading to incorrect decisions.
- Unbounded cardinality: Allowing arbitrary labels causes memory and index explosions under load.
A strong design explicitly trades off freshness, query speed, storage cost, and accuracy, with instrumentation to verify each latency target independently.