Medium java backend

Cache with ETag and Cache-Control

The 30 second answer

For responses that do not change often, I send Cache-Control headers so clients and CDNs can reuse them, and an ETag that identifies the version. The client then sends a conditional request with If-None-Match, and if nothing changed the server replies 304 Not Modified with no body. That saves bandwidth and serialization, though the server may still need to compute the version.

Cache-Control: max-age=60 lets clients reuse a response without asking again. ETag lets them revalidate with If-None-Match, and the server answers 304 Not Modified with no body. Spring's ShallowEtagHeaderFilter saves bandwidth, not server CPU.

Sources (3)
  1. RFC 9110 ยง8.8.3 ETag
  2. RFC 9111: HTTP Caching (Cache-Control)
  3. Spring: HTTP Caching