ASP.NET Core Web API Interview Question #182

How should a Web API handle very large result sets?

Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced

Quick Interview Answer

Do not load and return an unbounded table. Apply server-side filtering and pagination with deterministic ordering, return only required fields, and consider keyset pagination for large sequential datasets.

Detailed Explanation

Do not load and return an unbounded table. Apply server-side filtering and pagination with deterministic ordering, return only required fields, and consider keyset pagination for large sequential datasets.

For bulk exports, a separate asynchronous export job or streaming approach may be more appropriate than a normal JSON collection endpoint.

Code Example

GET /api/orders?afterId=50000&pageSize=100

var items = await query
    .Where(x => x.Id > afterId)
    .OrderBy(x => x.Id)
    .Take(100)
    .Select(x => new OrderSummaryDto(...))
    .ToListAsync(token);