My little note on HTTP caching

Where or how do we handle the caching?

A (web) system have a lot of tier.

  1. Client-Side Caching Browsers cache the data in its local storage

  2. Intermediary Caching Intermediaries are something between client and server (RFC7230) like proxy or gateway. Cache mechanism is handled by Nginx, Varnish or another caching-proxy.

  3. Server-Side Caching Backends cache the database inquiry or communication to some vendors.

Can we control it?

The best practice is using cache-directive (Cache-Control header).

Using cache-directive, you can:

  • Modify the expiration time
  • Decide to store/cache or not
  • Revalidate/reload the data

Combining with question #1. Is Cache-Control for client, intermediary or server? The answer is mentioned in RFC2616

The Cache-Control general-header field is used to specify directives that MUST be obeyed by all caching mechanisms along the request/response chain.

Conditional Request

Additionally, we can use conditional request (ETag and If-Not-Match header). The client simply handle 304 Not Modified which is return by server when has no new data.

Learn More