Topic 214 / Specialist
Real-time analytics
Chapter 13.6 · Real-Time Analytics
What and why
The warehouse pattern (→ 213) assumes data lands, then gets queried later — batch latency measured in minutes to a day, bounded by the ELT load cycle. Real-time analytics shrinks that gap to seconds: a dashboard or alert needs to reflect an event almost as soon as it happens, which a batch-loaded warehouse cannot do, because its load step is itself batched. Builds on: → 213 (data lake and data warehouse).
Diagram
Lambda Kappa
────── ─────
events ──► batch layer (accurate, slow) ─┐ events ──► single streaming layer
└──► speed layer (fast, approx) ──┼─► serving "batch" = replay the stream
┘ through the same code
How it works
- Streaming OLAP stores (Druid, Pinot, ClickHouse as examples) ingest events continuously, often straight from a message log (→ 111), and make them queryable within seconds — the same near-real-time refresh trick a search engine uses (→ 209), applied to analytical aggregates instead of documents.
- Pre-aggregation: instead of computing counts and sums from raw events at query time, these stores maintain rolling aggregates as events arrive — trading flexibility (you must decide what to pre-aggregate in advance) for query speed.
- Approximate algorithms (→ 141): an exact "distinct users in the last hour" requires tracking every unique value seen; a structure like HyperLogLog trades a small, bounded error for an answer computable in close to constant memory regardless of how many distinct values exist — the trade real-time analytics makes because exact counting doesn't fit the latency and memory budget.
- Lambda architecture runs two pipelines over the same events: a batch layer (accurate, slower, reprocesses everything — the → 213-style warehouse) and a speed layer (fast, approximate, recent data only), merging both at serving time — recent data arrives quickly, corrected numbers arrive once the batch layer catches up.
- Kappa architecture simplifies this to one streaming pipeline; "batch" results are produced by replaying the stream from a checkpoint through the same processing code, instead of maintaining two separate codepaths that must agree with each other.
- The trade-off: lambda avoids reprocessing cost by keeping a separate batch path, at the cost of two codepaths that can silently drift apart; kappa has one codepath, at the cost of needing to retain and be able to replay the full stream history for reprocessing.
Practical example
(illustrative). A dashboard tracks unique visitors in a trailing 5-minute window on a high-traffic site. Exact counting would store every visitor ID seen in that window; a HyperLogLog-style estimator instead maintains a small, fixed-size structure that answers the same query with a small, bounded error, updated as each event arrives rather than recomputed by a batch job — the structure's size does not grow with how many distinct visitors have been seen, only its error margin changes with how it's configured.
When to use it
- Use when: a dashboard, alert, or decision must reflect events within seconds to minutes, and the aggregate needed tolerates a small, known approximation error.
- Avoid when: an hourly or daily refresh is actually acceptable — the batch warehouse (→ 213) is simpler to run — or when exact results are mandatory, such as billing or financial reporting, which cannot use an approximate count.
- Failure scenarios: pre-aggregating the wrong rollups and discovering a question the system can't answer because nobody chose to pre-aggregate it; a lambda setup's speed and batch layers silently disagreeing because their processing logic drifted apart; a kappa replay from the beginning taking too long during recovery because retention wasn't sized for it.
- Simpler alternative: scheduled batch queries against the warehouse (→ 213), refreshed every few minutes, when "real-time" really just means "fairly recent."
Practice
(Numeric kind — drills; 15–20 min, no lab services). For 3 synthetic sets of IDs with known overlap and known true distinct counts, write a small program implementing a simple probabilistic counting technique of your own (for example, tracking the minimum observed hash value as a cheap cardinality estimator) and compare its estimate against each set's true count, recording the percentage error. Done when the error stays small and bounded even as set size grows 10×.
Key takeaway
Real-time analytics trades the warehouse's batch latency for seconds-level freshness, using pre-aggregation and approximation to afford it. Done when you can: explain the lambda-vs-kappa trade-off in one sentence and compute an approximate-count error. Used next in: nowhere else in this book — apply it directly once a dashboard's freshness requirement is measured, not assumed.
References
None — this topic makes no external claims.
Source directory