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 (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 a conditional request (ETag
and If-Not-Match
headers). The client simply handle 304 Not Modified
which is return by the server when it has no new data.
Learn More
- https://tools.ietf.org/html/rfc7234
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests
- https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#cache-control
- https://devcenter.heroku.com/articles/increasing-application-performance-with-http-cache-headers