I would like to implement some caching on my REST API server so that clients can get faster responses from cached data that is shared among them.
However, I would still like to give clients the option of getting fresh data if they prefer it.
Most examples of the HTTP cache headers I have seen assume that the data is being cached locally on the client, but I will be storing it on my server in Redis.
What is the preferred method to expose cache control functionality to clients when the cache is server-side and shared?
Thank you.
Have you looked at the Cache-Control
Cache-Control: max-age=
Cache-Control: max-stale[=]
Cache-Control: min-fresh=
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: no-transform
Cache-Control: only-if-cached
Related
I am implementing an HTTP polling mechanism to detect device network status. I am planning to make a periodic GET request to a static file /static/byte.txt to validate the device's internet access.
I am using the Cache-Control: no-cache request header to make sure I am not served with a cached copy of the file on the device (which defeats the purpose). But I would like to still use any cached copy of the file on the CDN, as there is no need to download the file from the origin (my servers) every time. Does anyone know of a way to set the cache control headers to achieve that? Thanks!
The Cache-Control request header is a poor fit for this use case as both the client HTTP library and the CDN will assign the same meaning to whatever cache control directive you choose.
Instead, I recommend using a Cache-Control response header. In the response, you can use something like Cache-Control: max-age=0, s-maxage=604800, which indicates that the client should not cache the response but the CDN can cache it for up to a week (604,800 seconds).
I want to be able to support Brotli and Gzip encoding for static assets hosted with Google Cloud Storage. To do this I want to encode files before they are uploaded as <filename>, <filename>.gz and <filename>.br. The issue is, I can't find a way to redirect a request with Accept-Encoding to the correct file.
I have looked into using:
Cloud Functions to somehow redirect incoming requests (similar to AWS Cloudfront Lambda), but it does not seem to be supported
Load Balancer to redirect requests to different buckets, but as far as I could see it can only redirect based on hostname/path to different buckets
Cloud CDN, but it does not seem to have any functionality that helps with this
Requests/Response examples
Assume bucket example-bucket contains the following files:
library.js
library.js.gz
library.js.br
Example 1
GET http://storage.googleapis.com/example-bucket/library.js
Accept-Encoding: gzip, deflate, br
Content-Encoding: br
<Contents of http://storage.googleapis.com/example-bucket/library.js.br>
Example 2
GET http://storage.googleapis.com/example-bucket/library.js
Accept-Encoding: gzip, deflate
Content-Encoding: gzip
<Contents of http://storage.googleapis.com/example-bucket/library.js.gz>
Is there a way to accomplish the above in a manner that is simple, performant and cost effective? I realize it's possible to just host my own server via App Engine, and let that take care of routing requests to the bucket, but is this the only way?
Lambda#edge is a way to do it but the simple way is to Whitelist the Accept-encoding header on CloudFront so CloudFront can pass it to Origin and you can have the Configuration on Origin to serve file based on the Accept-encoding header.
If you planning to use Lambda#edge, I would suggest using the Origin request function.
1. Whitelist the Accept-encoding header
2.Origin request function to read the header value and change the request URI path.
i am trying to implement caching for dynamic API calls where data is near-static. The approach i have taken is using the ETag and returning an ETag header for a Web API response headers. However, Browser doesn't seems to return the "if-None-Match" header at all for me to validate the subsequent calls.
Please note that i am using https and i have a valid SSL installed. Anyone had this issue and potential clues?
Found the root cause of the issue, It was due to the wrong cache-headers being sent by the server particularly Cache-Control: no-store
After changing the response headers, Browser is now able to send the If-None-Match request header.
My current response header is as below which is good enough to request the browser to re-validate it.
I have a RESTful API. DELETE /Collection/<Object-ID> will delete the specified object. We don't delete the objects internaly. It will only marked as deleted.
Now it is required to enter a delete comment. How is this possible with REST?
You have many options (as outlined in this question), but none of them are really considered standard practice. I personally would avoid using custom HTTP headers, but then you might run into trouble with certain HTTP implementations disallowing (or even ignoring) request bodies when sending DELETEs.
You can add a custom http header on the request from the client, and then read it from the server.
Edit:
Example for request with custom header:
DELETE /path/to/resource HTTP/1.1
Host: localhost:8080
Accept: */*
Delete-Comment: not needed anymore
Response:
HTTP/1.1 410 Gone
Date: Thu, 14 Nov 2013 13:56:26 GMT
Content-Length: 0
Note that X-Headers are deprecated: Custom HTTP headers : naming conventions
Ok, I'm confused. I'm trying to send back the magic headers from my server that will prevent a client from hitting the server again until a resource is stale.
I understand how ETag or Last-Modified works (Validation) - the client will ALWAYS still hit the server, and the server needs to validate the date or etag against the current value to know whether to bother serving up a new one.
Cache-Control and Expires, however, I don't think I understand. I've set the following:
Cache-Control: max-age=86400, must-revalidate
No matter what I do, my client (my browser, curl, NSURLConnection) always hits the server again on the second request. Is this a client thing? What headers should I send back to get the client to use it's private cache for a certain length of time?
As Nathan hints at in his answer, clients can issue a subsequent request with an If-Modified-Since header to determine whether or not their cache is stale. If the client receives a 304 Not Modified response, it will serve the content out of the local cache.
According to RFC 2616 (the HTTP 1.1 specification), the presence of must-revalidate within the Cache-control header forces clients to re-check their cache's status with the originating server prior to serving out of the cache.
For future reference - Mark Nottingham has written a great guide to HTTP caching:
http://www.mnot.net/cache_docs/#CACHE-CONTROL
The server needs to check the If-Modified-Since header and return a 304 not modified header if it wants the browser to keep caching.