Anatomy of a scalable web request

DNS, CDN, load balancer, stateless app servers, cache and database: the boxes almost every HLD answer starts from.

Time O(1) per hopSpace O(1)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

Split the work so each layer can scale on its own. DNS points users to the nearest edge, a CDN serves static files, a load balancer spreads API calls over identical stateless servers, a cache absorbs repeated reads, and the database stays the single source of truth.

Code

// One API instance. It keeps no user state in memory, so any instance can serve any request.
app.MapGet("/products/{id}", async (int id, IDistributedCache cache, AppDb db) =>
{
    var key = $"product:{id}";
    var hit = await cache.GetStringAsync(key);
    if (hit is not null) return Results.Content(hit, "application/json");
    var p = await db.Products.FindAsync(id);
    if (p is null) return Results.NotFound();
    await cache.SetStringAsync(key, JsonSerializer.Serialize(p),
        new() { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) });
    return Results.Ok(p);
});
// Static files (images, JS) never reach this code: the CDN serves them.
// The client found the CDN and the load balancer through DNS.

Tips

In the .NET library

In Azure this is Front Door or a CDN, then Application Gateway or a load balancer, then App Service or AKS instances, Azure Cache for Redis, and Azure SQL.