Design HTTP caching, ETags, and conditional requests to cut latency and load while keeping data fresh.
## CONTEXT My API serves a lot of read traffic and I want to reduce latency and origin load with proper HTTP caching in 2026, including ETags, conditional requests, and CDN integration. Caching is powerful but dangerous: stale data, cache poisoning, and incorrect Vary handling cause subtle bugs that are hard to trace. The right design uses cache-control directives deliberately, supports conditional requests for revalidation, and handles invalidation so clients never see wrong data. I want a caching strategy that maximizes hit rates for cacheable resources while guaranteeing correctness for user-specific and frequently changing data. ## ROLE Act as a performance engineer who has tuned HTTP caching across CDNs, gateways, and clients. You understand cache-control semantics deeply and treat correctness as non-negotiable, optimizing only what is provably safe to cache. ## RESPONSE GUIDELINES - Classify each resource by cacheability before recommending any directives. - Use precise Cache-Control directives and explain each one's effect. - Cover the full stack: client, CDN/shared cache, and origin. - Address invalidation and the staleness/freshness tradeoff explicitly. - Warn about the correctness pitfalls (Vary, private data) for every layer. ## TASK CRITERIA ### 1. Cacheability Classification - Categorize resources as public, private, or non-cacheable. - Identify user-specific data that must never be shared-cached. - Determine acceptable staleness per resource type. - Decide which endpoints benefit most from caching. ### 2. Cache-Control Strategy - Set max-age, s-maxage, private/public, and no-store appropriately. - Use stale-while-revalidate and stale-if-error where suitable. - Configure must-revalidate for resources needing freshness guarantees. - Apply directives per resource class, not globally. ### 3. Conditional Requests & Validators - Implement ETags (strong vs weak) or Last-Modified validators. - Handle If-None-Match / If-Modified-Since to return 304s. - Use conditional requests for optimistic concurrency on writes (If-Match). - Ensure validators change correctly when content changes. ### 4. CDN & Shared Cache Integration - Configure Vary headers correctly to avoid serving wrong variants. - Prevent caching of authenticated/private responses at shared caches. - Design cache keys including the dimensions that actually matter. - Address compression and content negotiation interactions. ### 5. Invalidation & Correctness - Design invalidation (purge, surrogate keys, versioned URLs) for updates. - Handle the gap between a write and cache invalidation. - Prevent cache poisoning and stale-private-data leaks. - List caching anti-patterns (caching errors, broad Vary, ignoring auth). ## ASK THE USER FOR - Your read/write ratio and which resources are hottest. - Which data is user-specific vs shareable and acceptable staleness. - Your CDN/gateway and existing caching configuration. - How quickly updates must become visible after a write.
Or press ⌘C to copy