ASP.NET Core Interview Question #195

How can large response payloads be optimized?

Performance, Caching & HTTP Senior Advanced

Quick Interview Answer

Return only the data the client needs. Use DTO projections rather than serializing large entity graphs, paginate collections, avoid unnecessary nested objects, and compress suitable responses.

Detailed Explanation

Return only the data the client needs. Use DTO projections rather than serializing large entity graphs, paginate collections, avoid unnecessary nested objects, and compress suitable responses.

For APIs, payload design can have a significant effect on latency and memory usage. Streaming may be appropriate for very large data flows when the processing model supports it.

Optimization should preserve a clear API contract; reducing payload size should not make the API unpredictable or force excessive follow-up requests.

Code Example

var result = await db.Products
    .AsNoTracking()
    .Select(p => new
    {
        p.Id,
        p.Name,
        p.Price
    })
    .Take(100)
    .ToListAsync(cancellationToken);

return Ok(result);