Is Cache-Control Header for Client or Server?
My little note on HTTP caching
Where or how do we handle the caching?
A (web) system has a lot of tiers.
- Client-Side Caching
Browsers cache the data in its local storage
- 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.
- Server-Side Caching
Backends cache the database inquiry or communication to some vendors.
Can we control it?
The best practice is using cache-directive (
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.
Additionally, we can use a conditional request (
If-Not-Match headers). The client simply handle
304 Not Modified which is return by the server when it has no new data.