What are Caches?
At a high-level, there are two popular types of caching:
- In-app caching (APC, Memcache, Redis, other memory stores)
- HTTP caching - proxy, gateway, and private
In-app caching lives in your infrastructure and interacts with your application code. It allows you to store objects in memory for quick retrieval. It takes action when a request makes its way to your server. Your code checks your in-app caches and determines if it should use them to retrieve data in order to fulfill the request.
HTTP Caching is mostly done by third-parties. They can potentially save requests from ever reaching your server by responding to a request itself.
- Proxy - A proxy cache is a public, shared cache often employed by an ISP or large corporation. Because they are employed at a high-level, they can (and do) cache thousands of various websites.
- Gateway - Similar to in-app caches, Gateway caches live as part of your infrastructure. They sit in front of your web-server and act much in the same way of proxy servers - except they are for your application(s) only. They are technically "reverse proxies".
- Private - Private caches are caches that are unique to a specific user. They live on the client-end. Your browser is a private cache; it will cache responses unique to the sites you visit.
Making a response (web page, api-response, etc) cacheable is part of the HTTP cache mechanism. Which cache mechanisms you use depends on your use case.
Note: I'll use the term
Types of HTTP caching
The HTTP spec defines 2 methods of HTTP caching:
- Validation -
save bandwidthby not having an origin server reply with a full message body (your origin server returns a header-only response)
- Expiration -
save round-trips to the origin server- a cache can potentially serve a response directly, saving the origin server from even knowing about the request
Done with if-* headers (
If-Modified-Since, and so forth), validation caching is primarily used for 2 things (most useful for an API, in my opinion).
- Conditional GET requests - a server can tell the request 'nothing has changed since you last checked'. This is good for mobile APIs where the bandwidth of re-sending a message body can be saved via conditional requests.
- Concurrency Control - in a POST or, more likely, PUT request, a server can check if the resource being updated was changed since the requester last checked (solves the Lost Update Problem). This is good for APIs with a lot of writes (updates) to resources.
Date and related headers, expiration caching can aid in caching a response for the next user (or even for one specific user) on subsequent page loads, saving your server(s) from traffic load
- If you have a Gateway cache such as Varnish, you can potentially cache responses to end points per user A gateway cache gives you a lot of cache control since its part of your stack.
- Using the
Cache-Controlheader to set a response to 'public' will allow Proxy and Gateway caches to cache your site content
- Requests behind authentication and/or SSL are usually not cached. You may be able to with a Gateway cache, or with a private cache (aka, your client can figure out caching based on your expiration headers).
Note: Validation caching typically over-rides Expiration caching. If you use Validation caching, and the cache (such as your browser!) supports it, the cache will likely ignore the expiration directives. This means all requests will make its way to the origin server. You likely want to disable
ETagsfor your static assets (js, css, images) and selectively use it for your application responses.