Should Cache-Control request headers influence server-side caching?

Tom

A have seen a lot of discussion regarding proxies with regard to RFC7234 and the Cache-Control header, but I'm not too clear on whether the request headers should impact server-side caching or not.

The diagram below shows where the headers I am concerned are located (down arrows are request headers, up arrows are response headers):

      ┌────────┐
      │ Client │
      └────────┘
        ↓    ↑
      ┌────────┐
      │ Cache  │
      └────────┘
        ↓    ↑
      ┌────────┐
      │ Cache  │
      └────────┘
        ↓    ↑
      ┌────────┐
      │ Server │
      └────────┘
  ??? → ↓    ↑
    ┌─────────────┐
    │ Server-side │
    │    Cache    │
    └─────────────┘

I have a number of questions regarding the interaction between Cache-Control and server-side caching (e.g. should Cache-Control: no-cache, mean that the server should re-validate its own cache?). But the vast majority boil down to the following:

Should the server-side use Cache-Control and other associated headers from the request for logic regarding its server-side caches?

Take the following small (albeit crude) Python Flask application as an example:

@app.route('/app/<id>')
def endpoint(id):
    if 'no-store' not in request.headers['Cache-Control']:
        # If 'no-store' isn't specified, try cache first
        entry = entry_cache.get(id)
        if entry is None:
            entry = service.get_entry(id)
    else:
        # Otherwise, immediately talk to the service
        entry = service.get_entry(id)
    return entry

In addition, this raises another question which, depending on the answer, may render the above irrelevant:

What kind of caching should be done on the server-side? What situations should we rely on HTTP caching and calculate the response on each request (outside of situations like If-None-Match)?

For context, most applications I tend to develop are designed as micro-services either as containers or as serverless functions with caches tending to be calls to external services such as Redis. Not many browsers call these HTTP endpoints; most clients are business applications.

Update (2021-08-17): Thanks Kevin for the feedback! I have updated the diagram and some of the wording to clarify that I refer specifically to the request caching headers rather than the response headers. In addition, I've added a very small (and very crude) Python Flask app to help illustrate the type of logic I am referring to.

Kevin Christopher Henry

The HTTP caching specification is not binding on the internal caching behavior of the server. The server's response is, by definition, fresh, regardless of how it was generated. So while it would be a violation of the protocol for an intermediate proxy to serve a cached response to a request with a no-cache directive, it wouldn't be for the server itself.

That said, it's certainly possible to harmonize both kinds of caching, whether implicitly (in response to requests that happen to have caching request headers set) or explicitly (defining your API to use HTTP caching headers for internal behavior). So your Flask example makes a lot of sense to me. That said, I'm not familiar with sites or frameworks that use these request headers outside of caching proxies, so you may be a trailblazer in this regard.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Make Nginx caching respect cache-control headers

Enable UWSGI caching only when Cache-Control headers are set

Issue with CDN strong caching without Cache-Control or Expires headers

Headers Cache Control: No cache

Browser caching request/response without any Cache-Control header

HTTP Request headers and caching

server side image caching

Cloudflare not caching files even when headers are "Cache-Control: public, max-age=86400"

Understanding the difference between "Response Headers" and "Request Headers" in Firefox Developer Console (network tab) for image cache control

Plotly dash server side caching

Cache-control in response headers

Hide headers on client side request

Why is Cache-Control attribute sent in request header (client to server)?

Cache control in javascript request

nginx caching: return cached page (when specific Cache-Control header given) only if the upstream server is not reachable

Cloudfront not caching despite Cache-Control

Long term caching with webpack chunkhash and cache control

Clearing client side cache via html headers

Safari Caching GET request even with disabled cache

Setting http headers for caching with shiny server & nginx

Plotly dash user dependent server side caching

Server side caching of dynamic content with Nginx and Etags

Caching select query data on server side

Server-side response caching with Hapi

Fetch caching response - no-cache headers not working as expected

AlamoFire Ignore Cache-Control Headers

How to set HTTP headers (for cache-control)?

Difference between Pragma and Cache-Control headers?

Cache-Control Headers not respected on CloudFlare