API performance Level 2 This level · 1 of 20
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.

Could you say this in an interview?

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 ETagrfc-editor.org
  2. RFC 9111: HTTP Caching (Cache-Control)rfc-editor.org
  3. Spring: HTTP Cachingdocs.spring.io
Previous
Next