Conditional requests let a client ask whether a representation has changed before downloading it again. They can reduce bandwidth for frequently read resources and provide a concurrency check for updates. The mechanism is based on HTTP validators such as ETag and Last-Modified, not on an application-specific timestamp convention invented for each endpoint.
Cache validation with If-None-Match
Return an ETag that identifies the selected representation. A client can send it in If-None-Match on a later GET. If the representation is unchanged, respond with 304 Not Modified and no response body. Ensure the validator changes when any data that affects the representation changes. If responses vary by authorization, language or content negotiation, configure cache behavior and Vary headers carefully so one user's representation is not served to another.
Protect updates with If-Match
For editable resources, a client can send the ETag it previously received in If-Match. Apply the update only if the current representation still matches. If another request changed the resource, return 412 Precondition Failed and let the client reload and reconcile. This is an HTTP-level approach to preventing lost updates; database constraints or version columns may still be needed to make the check atomic.
Choose validator strength
A strong ETag indicates byte-for-byte equivalence of the representation. A weak ETag indicates semantic equivalence for cache validation and is not suitable for every update precondition. Do not generate validators from only a database ID if the resource content can change.
Test edge cases
Test missing validators, stale validators, concurrent updates, representation variants and private data. Verify that a 304 response includes appropriate cache headers and that sensitive API responses are not accidentally stored by shared caches.
Further reading
- RFC 9110: Conditional Requests — https://www.rfc-editor.org/rfc/rfc9110
- MDN: ETag — https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag