question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow returning stale values while caches are revalidated

See original GitHub issue

TLDR: Support a staleWhileRevalidate hint on the cacheControl directive

type Variant {
  name: String
  sku: String
  inStock: Boolean @cacheControl(maxAge: 5, staleWhileRevalidate: 100)
}

I’ve been experimenting with cacheControl hints on an Apollo Server. The current per field cache control that Apollo exposes is quite powerful. It has a real impact on the response times of our queries.

Saying that, I still have a few REST data sources that are quite slow and every time my cached field value expires, I can notice how the REST endpoint is being hit and the cache recreated. This is of course, expected behaviour.

I was wondering if it would be possible to have a @cacheControl directive hint that would instruct the cache to default to returning cached data even when it’s stale and in that specific case, to update the key’s value on the background.

This would mean that only the first uncached request would have to wait for the rest endpoint to respond.

From the look of it, it seems that this specific use case would be impossible to implement without both a custom KeyValueCache and a custom apollo-server-plugin-response-cache implementation.

I can see two modifications being required, the first one on the Interface exported by KeyValueCache, having a method in the following form would greatly simplify implementing revalidation on the background. This would give the cache the responsibility of creating the cached value when and if required.

const value = await cache.getOrSetOnStale('key', async () => {
  const updatedValue = await generateValue();
  await cache.set('key', updatedValue);
  return updatedValue;
});

The second change would require having apollo-server-plugin-response-cache pass in the hint that the value should be revalidated in the background if it’s stale.

Reference http stale while revalidate implementation

For some reference, see this article: https://www.fastly.com/blog/stale-while-revalidate-stale-if-error-available-today

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:31
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
stewartmcgowncommented, May 29, 2021

Yep! Which is why I combined it with response-cache-plugin so it would work with existing clients using the POST /graphql verbage. When switching to automatic persisted questions over GET for clients automatic CDN caching works great, but I wanted to use it with a response cache plugin.

1reaction
matchucommented, May 28, 2021

(Ah yeah, you probably already understand this but I should say it out loud for clarity: my gist only implements the HTTP Cache-Control header, and it relies on a separate cache layer like Varnish, Cloudflare, Fastly, or Vercel’s built-in cache, to actually perform the response caching according to what the Cache-Control header says!)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stale-while-revalidate Data Fetching with React Hooks: A Guide
Leveraging the stale-while-revalidate HTTP Cache-Control extension is a popular technique. It involves using cached (stale) assets if they are found in the ...
Read more >
Keeping things fresh with stale-while-revalidate - web.dev
Let's break down stale-while-revalidate into two parts: the idea that a cached response might be stale, and the process of revalidation. First, ...
Read more >
Staleness and revalidation - Fastly Developer Hub
Once that freshness lifetime expires, the stale-while-revalidate directive allows Fastly to continue to serve the same content for up to another 60 seconds ......
Read more >
Considering A Stale-While-Revalidate Pattern To Caching In ...
The concept, "stale while revalidate", usually means that your request to read a value is served immediately from a cache. Then, a background ......
Read more >
RFC 5861 - HTTP Cache-Control Extensions for Stale Content
The stale-while-revalidate HTTP Cache-Control extension allows a cache to immediately return a stale response while it revalidates it in the background, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found