How does HTTP caching work in REST APIs with ETag and Cache-Control?
Answer
HTTP caching reduces server load and latency. Cache-Control headers set caching policy: Cache-Control: public, max-age=3600 allows any cache to store the response for one hour. ETag is a hash of the response body (e.g., ETag: "abc123"). On subsequent requests, the client sends If-None-Match: "abc123"; if the resource has not changed, the server returns 304 Not Modified (no body), saving bandwidth. Last-Modified is a timestamp alternative to ETag, used with If-Modified-Since. For user-specific data, use Cache-Control: private to prevent shared caches from storing it. Cache-Control: no-store prevents all caching (for sensitive data). Proper caching can dramatically reduce API load — up to 90% of traffic can be served from cache for read-heavy APIs.
Previous
What is rate limiting and how is it communicated in REST APIs?
Next
What are the OAuth 2.0 grant types and when do you use each?
More REST API Design Questions
View all →- Intermediate What is HATEOAS and how is it implemented?
- Intermediate What are the main API versioning strategies in REST and what are their tradeoffs?
- Intermediate What are the pagination strategies in REST APIs?
- Intermediate What is rate limiting and how is it communicated in REST APIs?
- Intermediate What are the OAuth 2.0 grant types and when do you use each?