HTTP Caching for APIs: Cache-Control, ETag, and Conditional Requests
A practical walkthrough of API caching headers, invalidation strategies, and the mistakes that leak private data.
Caching is one of the few changes that can improve latency, reduce upstream load, and lower cost at the same time. It is also easy to get wrong. This guide explains the headers that control caching and how to choose a policy for different kinds of API responses.
The two questions every cache asks
A cache needs to answer two questions: may I store this response, and for how long may I reuse it without asking the server again? Cache-Control answers both.
A response such as Cache-Control: public, max-age=60 allows shared caches to store the response and serve it without revalidation for 60 seconds. private, max-age=300 permits storage in a user's browser but not in a shared proxy. no-store asks caches not to save the response at all. no-cache is often misunderstood: it allows storage but requires revalidation before reuse.
Validation with ETag and Last-Modified
Instead of sending a full response every time, a server can send a validator. An ETag is an opaque identifier for a version of the resource. A client that has a cached copy sends If-None-Match: "v3". If the resource is unchanged, the server returns 304 Not Modified with no body.
Last-Modified works similarly with a timestamp and If-Modified-Since. ETags are usually preferable when resources can change more than once within a second or when timestamps are not reliable.
A 304 response is a success signal for caching, not an error. Your client should keep using the stored body.
Choosing a policy by resource type
- Immutable reference data: a long max-age works well. Include a version in the URL so a new version gets a new cache key.
- Frequently changing public data: a short max-age plus an ETag gives a good balance.
- User-specific data: use private or no-store. Never allow a shared cache to keep one user's response for another.
- Write responses: usually no-store, because the result is transient and may include sensitive fields.
- Error responses: often no-store so a failure is not replayed after the underlying problem is fixed.
Invalidation is a design decision
The hardest part of caching is deciding how stale data may be. Versioned URLs avoid invalidation entirely: /v1/catalog?version=42 is a new cache key when the catalog changes. Time-based expiry is simpler but means some users see old data until the TTL passes. Event-driven purging is precise but needs reliable infrastructure.
Pick one strategy per resource and document it. Mixing strategies silently is how teams end up debugging "random" stale content.
A worked response
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: public, max-age=30, stale-while-revalidate=60
ETag: "catalog-42"
Vary: Accept-EncodingThis says: shared caches may store the response for 30 seconds; for the next 60 seconds they may serve a stale copy while refreshing it in the background; validation uses the catalog ETag; and compressed and uncompressed variants must be stored separately.
Common mistakes
First, caching authenticated responses with public. If a response depends on a cookie or Authorization header, it must not be stored by a shared cache unless you have an explicit, tested mechanism for keying by identity.
Second, forgetting Vary. If your response changes based on Accept-Language or compression, caches need to know which request headers affect the representation.
Third, caching 302 redirects forever. A redirect that changes during a migration can trap clients on an old host. Use short TTLs for redirects you may revise.
Fourth, treating a CDN as a substitute for application caching. A CDN helps with repeated public requests. It does not fix a slow database query for a unique, uncached request.
Verify from outside
Use the API checker to inspect response headers and compare TTFB between a first and a repeated request. A large drop on the repeated request suggests a cache hit. For conditional requests, add If-None-Match with the ETag you observed and confirm that the server returns 304 with a much smaller body.
Caching works best when it is deliberate: choose the resource, choose the policy, document the staleness you accept, and verify the headers actually reach clients.
Related Tools
What is API Response Time? The Complete Guide to Measuring & Optimizing API Performance
Learn everything about API response time, why it matters for your business, and how to optimize your API performance wit...
How to Reduce API Latency: 10 Proven Strategies for 2026
Discover 10 proven strategies to reduce API latency and improve your application's performance. From caching to edge com...
API Monitoring Best Practices: The Complete Guide for 2026
Learn API monitoring best practices to ensure your services are reliable, fast, and always available. Covers uptime moni...
Understanding TTFB: Time to First Byte Explained — The Key to API Performance
A deep dive into TTFB (Time to First Byte), what it means for your API performance, and how to improve it with actionabl...
API Speed Test: How to Benchmark Your REST & GraphQL APIs in 2026
Complete guide to API speed testing. Learn how to benchmark REST and GraphQL API performance, interpret results, and opt...
How to Achieve 99.99% API Uptime: The Definitive Guide
Learn how to achieve 99.99% API uptime with proven strategies including redundancy, failover, monitoring, and incident r...