Advertisement
Intermediate .NET, Caching (In-Memory, Distributed & Redis)

What HTTP caching headers (Cache-Control, ETag) can a client and CDN use, and how do they relate to server-side caching strategies?

Short answer

Cache-Control directives (max-age, no-cache, private/public) tell browsers and intermediary caches/CDNs how long a response can be reused without re-requesting.

Save .NET mock interview
Advertisement
AModel answer
Cache-Control directives (max-age, no-cache, private/public) tell browsers and intermediary caches/CDNs how long a response can be reused without re-requesting. ETag provides a version identifier for a resource; clients can send If-None-Match with a previously seen ETag, and the server responds with a lightweight 304 Not Modified if unchanged, avoiding retransmitting the full response body -- these HTTP-layer mechanisms complement server-side application caching by reducing load even before a request reaches your application code.
[HttpGet("{id}")]
public IActionResult GetProduct(int id) {
    var product = _service.GetProduct(id);
    var etag = ComputeETag(product);
    if (Request.Headers.IfNoneMatch == etag) return StatusCode(304);
    Response.Headers.ETag = etag;
    Response.Headers.CacheControl = "public, max-age=300";
    return Ok(product);
}
Real-world example
A public API serving rarely-changing reference data (like country lists) sets long Cache-Control max-age values and ETags, letting a CDN and client browsers serve repeat requests without ever reaching the origin server.
Likely follow-ups
  • How does a CDN's cache differ from browser caching in terms of who benefits?
  • What's the risk of overly aggressive Cache-Control settings for frequently-changing data?
Related topics