wasAsked.

Design a dashboard API that aggregates millions of records without running expensive queries on every page load.

BrowserStack · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, SQL, Redis

Community answers

Posted by wasAsked editorial collection

Avoid loading transactions and aggregating them in memory. Push grouping and projection to SQL, bound the time range, and cache only when the freshness requirement allows it. Example: var totals = await db.Orders.AsNoTracking() .Where(x => x.RestaurantId == tenant.RestaurantId && x.OutletId == tenant.OutletId && x.CreatedAt >= from) .GroupBy(x => x.Status) .Select(g => new { Status = g.Key, Count = g.Count(), Sales = g.Sum(x => x.Total) }) .ToListAsync(ct); Index tenant and date filters, inspect the generated SQL and execution plan, and consider a summary table for expensive historical dashboards. Return all widgets from one consistent snapshot where consistency matters.

Backbencher Developer · · 0 likes

Avoid computing aggregates from raw rows on every request. Precompute and cache instead. 1. Pre-aggregate - Maintain summary tables (daily totals by customer, region or status) updated incrementally when data changes, or by a scheduled job every few minutes. - In SQL Server, an indexed view can keep simple aggregates up to date automatically; in PostgreSQL, a materialized view refreshed on a schedule. - For very large analytical workloads, use columnstore indexes or a separate analytics store (data warehouse, ClickHouse) fed by ETL or change data capture. 2. Cache the results - Cache dashboard responses in Redis keyed by tenant, filters and time range, with a TTL matching how fresh the data must be (for example 1–5 minutes). - Use stale-while-revalidate so users never wait for a rebuild. 3. Design the API around the dashboard - One endpoint per widget or a batched endpoint, returning only the numbers needed, not raw rows. - Limit time ranges and granularity (hourly for the last day, daily for the last year). - Paginate any drill-down lists with keyset pagination. 4. Query and index tuning - Index the columns used for filtering and grouping (tenant, date); use covering indexes. - Read from a replica so dashboard load does not affect transactional traffic. - Use asynchronous queries with timeouts and cancellation. 5. Freshness and UX - Show "last updated at" on the dashboard. - For near real-time counters, update them from events (increment counters in Redis) instead of re-counting. Trade-off to state clearly: precomputation gives fast, predictable reads in exchange for slightly stale data and extra write or batch work. Agree the acceptable staleness with the business first.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics