I'm looking for a solution to reduce server load on my API. I thought it's a good idea to cache the content for 1 second, so the server will get only 1 requests each second instead of many. However, I wasn't able to make it work:
In Use origin settings based on Cache-Control headers
mode: the Google CDN ignores the 'Cache-Control', 'public, max_age=1'
header. It looks like I can't cache JSON in this mode, probably because of the missing Content-Range
or Transfer-Encoding: chunked
header?
In Force cache all content
mode: it actually works, but the minimum TTL is 1 minute, which is just too much for me. I'd like to update the content every second.
I'm quite surprised no one uses short-lived caches for API-s, since it can theoretically reduce load and maybe save costs when using with external APIs (eg. you don't need to pay twice if you request the same thing from an expensive API endpoint). Any ideas?
When using the "Use origin settings based on Cache-Control headers" cache mode, you can instruct Google Cloud CDN to cache a response for 1 second by including a Cache-Control: public, max-age=1
response header.
The problem with your earlier attempt appears to be a typo: the correct directive is max-age
(with a hyphen), not max_age
(with an underscore).