C.P.O
C.P.O

Reputation: 1281

Rest API: the best way to notify if a resource comes from the cache

Sometimes is useful to know if the response comes from the cache.

For example, using GCP - Bigquery, you can know that:

enter image description here

If there is no data change, and the query is the same we can see:

enter image description here

So, what is the best way to implement this on a Rest-API:

Upvotes: 0

Views: 332

Answers (1)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57229

So, what is the best way to implement this on a Rest-API:

  • setting a (custom) response header
  • adding a property to a response wrapper

Maybe both, maybe neither

That is to say, if you wanted to introduce metadata about the cache into an HTTP response, you can do so via the headers, or via the representation.

In order to apply the meta data to the representation, your connector (ex: a cache) would need to understand the specific media type well enough to know where the metadata should be written.

HTTP describes standardized semantics for announcing that a representation has been transformed, via the Warning header.

That said, I'm suspicious about whether you are trying to solve the right problem here. A consumer probably cares a lot more about how old the information is rather than caring about whether or not the information has been cached.

If I ran the zoo, instead of telling you processed or cached, I'd report the effective date of the report. That's information that doesn't change when we introduce caching.

(The story gets a little bit more complicated when are dealing with requests that carry payloads, because intermediate components aren't going to know how to create a cache-key out of the request body, so everything is going to forward to the origin server anyway. That may get better if we introduce support for safe requests that include queries, but we aren't there yet.)

Upvotes: 1

Related Questions