router icon indicating copy to clipboard operation
router copied to clipboard

whole response caching

Open abernix opened this issue 3 years ago • 3 comments

Today, the Apollo Gateway supports whole response caching thanks to @mandiwise's contribution of https://github.com/apollographql/federation/pull/870 as an evolution of the @cacheControl story in Apollo Server, which produces cache-control (and age) headers.

The PR above introduced the ability for the Gateway to synthesize a cache policy from cache-control headers of subgraphs and leverage Apollo Server's response cache plugin. In terms of implementation, this leverages uses Apollo Server's responseForOperation plugin hook to short-circuit execution in the event of a cache-hit, and keys things using a cache key that is based on a hash of a variety of things including variables, operationName, etc. — as seen here.

An analysis of the way things are done today should probably be drawn up as a design (or we should ask the Apollo Server team, or do some spelunking to see if a design already exists.)

All of this could be built on a key-value cache abstraction that supports either string in-memory, or in a distributed fashion (à la Memcached/Redis). Though that abstraction could be a separate issue (and also be used to implement other key-value stores that could also be, optionally, distributed, like APQ in #130)

And of course, we should consider any reports we want to sent to Studio, as was once requested in #1009. (See issue)

abernix avatar Jan 14 '22 11:01 abernix

Worth noting that one could break out "calculate a cache-control response header based on combining cache-control headers from subgraphs" as a smaller piece from actually implementing caching in the Router, if that's preferable.

glasser avatar Sep 23 '22 21:09 glasser

Big thumbs up for this one. It would be really beneficial to get some frequently used responses from the Apollo Router's cache when the actual data source is having some outages, or just to make the searches more efficient. If I understood right, that's what this feature would be for? For example, when building a client app with some SEO friendly server side rendered static pages, the data needs to be available and it would be nice to have it quickly for fast CI.

nikomakela avatar Feb 17 '23 08:02 nikomakela

For those following along we have released entity caching in Router 1.40.0: https://github.com/apollographql/router/releases/tag/v1.40.0

This is not whole response caching as implemented in the Gateway, it is actually more advanced and can still be used to cache response data

smyrick avatar Apr 02 '24 22:04 smyrick