Skip to content
tezvyn:

Trade-offs: Pre-aggregation vs. Querying Raw Data

Source: docs.embeddable.comHardHow cards are made

Trade-offs: Pre-aggregation vs. Querying Raw Data

Tests your grasp of data system trade-offs. A great answer weighs pre-aggregation (fast, cheap, stale) against querying raw data (slow, costly, fresh, flexible) and proposes a hybrid solution. A red flag is declaring one method universally superior.

What's really being asked

This tests your senior-level ability to analyze and articulate the fundamental trade-offs in analytical system design. The interviewer wants to see if you can move beyond simple definitions and apply concepts like latency, cost, data freshness, and flexibility to a real-world problem like a high-traffic dashboard. They are evaluating your practical judgment in balancing business needs (fast dashboards) with technical and financial constraints (database load, query costs).

The full answer

A good answer hits four points in order. First, define the two approaches. Pre-aggregation involves pre-computing and storing results, like a materialized view, to serve queries quickly. Querying raw data hits the underlying database directly for every request. Second, detail the trade-offs across four axes. Pre-aggregation offers low latency and cost but sacrifices data freshness (due to refresh schedules) and analytical flexibility (limited to pre-defined dimensions/measures). Querying raw data provides perfect freshness and flexibility but at high latency and cost, especially with many concurrent users on OLTP or OLAP databases. Third, propose a hybrid solution as the best practice for most high-traffic scenarios. Use pre-aggregations for the main dashboard widgets that 95% of users see. Then, provide "drill-down" functionality that queries the raw, granular data for users who need to explore specific details. Fourth, mention the mechanism for managing pre-aggregations, like a refresh key that triggers updates, which directly controls the freshness vs. cost balance.

The mistakes people make

A common red flag is presenting a one-sided argument, for instance, stating "pre-aggregation is always better for performance" without discussing the significant loss of flexibility and data freshness. Another mistake is failing to quantify the trade-offs. A junior answer says "it's faster"; a senior answer says "it can reduce query times from minutes to milliseconds but the data might be an hour stale." Also, candidates often forget the impact on the source database. High-frequency raw queries can cripple an OLTP database not designed for analytical workloads or run up huge bills on an OLAP database like BigQuery or Snowflake. Finally, not suggesting a hybrid approach shows a lack of practical experience.

What usually comes next

Expect follow-ups like: "How would you decide which queries to pre-aggregate?" (Answer: based on usage patterns, P95/P99 latency requirements, and query complexity). Or, "Describe a situation where you would never use pre-aggregation." (Answer: real-time fraud detection, where millisecond-level freshness is critical). Another could be: "How do you handle a cache miss in a pre-aggregation system?" (Answer: The query falls back to the raw data, but this should be monitored as it indicates a poorly designed aggregation or a new query pattern).

A concrete example

Imagine a sales dashboard for an e-commerce site with 1,000 concurrent sales reps. A key metric is "Total Sales Today." Querying the raw orders table (billions of rows) for every user's dashboard refresh would be incredibly slow and expensive. Instead, we create a pre-aggregation that calculates SUM(sales) grouped by hour. This table might have only 24 rows for the day. We can set it to refresh every 5 minutes. A user's dashboard query now hits this tiny summary table, returning in under 50ms. The data is at most 5 minutes stale, which is an acceptable trade-off. If a sales manager wants to see the individual orders for a specific hour, they can click to drill down, which then triggers a more expensive, targeted query against the raw data.

Interview question

For a high-traffic analytics dashboard, what is the best strategy to balance low latency, cost, data freshness, and analytical flexibility?

  • a.Vertically scale the source OLTP database to handle the increased analytical query load from the dashboard.
  • b.Query the raw data directly for all requests to ensure users always have the freshest, most flexible data available.
  • c.Implement pre-aggregation for all dashboard components to guarantee the lowest possible latency and minimize database load.
  • d.Use pre-aggregated data for summary views and allow drill-downs that query raw data for specific, detailed analysis.Correct
Why?

The correct hybrid approach serves common queries quickly with pre-aggregations while allowing drill-downs to raw data for flexibility. Relying solely on pre-aggregation (Option C) sacrifices essential flexibility and data freshness.

Just read this? Test yourself on what you have been reading.

Read the original → docs.embeddable.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on data engineering — each one lists the topics its interview covers.

See open roles