Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to return prior cached value on update failure #132

Open
cdhowie opened this issue Nov 17, 2022 · 5 comments
Open

Option to return prior cached value on update failure #132

cdhowie opened this issue Nov 17, 2022 · 5 comments
Milestone

Comments

@cdhowie
Copy link

cdhowie commented Nov 17, 2022

We have a case where some network-loaded asset changes rarely, but we need to notice changes to it relatively quickly. Therefore, we use a memoized async function with a maxAge of 10 minutes.

However, if there is a brief network interruption when the item is requested after expiring, this causes an error to be returned and the cached value purged. This makes sense as a default behavior, but it would be nice to be able to opt-in to a behavior that will not purge the cache, but will return the last-cached value when the update function fails (synchronous exception, or returned promise becomes rejected).

In the case of async functions in particular, this may be somewhat complex because there can be two pieces of cached data: the last-resolved value, and a currently-awaiting promise. The desired behavior on a rejection is that the currently-awaiting promise is cleared and the last-cached value is returned instead of the rejected promise, but the item's age is unaffected, which will cause future fetches to retry immediately. Combined with a low timeout, this allows the application to continue using the last-known value while continually retrying for an updated value.

This is the widely-used "serve stale on error" pattern used by CDNs, applied to this module.

Since this causes errors to be swallowed by the memoized function, the documentation should probably hint that errors within the fetch function should be logged somewhere as they will not be observable otherwise.

Alternatively, there could be some way to communicate via the rejection error object (by error type or a flag on the object) whether the failure should be propagated to the caller or if the last-cached value should be used instead.

One potential way this could be implemented would be to introduce a mechanism by which the update function can be given the last-cached value, which it could choose to return. Combined with the ability for the update function to be able to indicate the TTL of the item (#23) this could easily give users their own way to implement this feature in a more flexible way.

@medikoo
Copy link
Owner

medikoo commented Nov 17, 2022

@cdhowie great, thanks for the input. That looks as a valid request. I've added it to v1 spec.
Unfortunately, implementing this in the context of the current version doesn't look trivial, so we need to wait for v1, of which the ETA is unknown.

@medikoo medikoo added this to the v1 milestone Nov 17, 2022
@cdhowie
Copy link
Author

cdhowie commented Nov 18, 2022

@medikoo Thanks. BTW I think you linked #117 but I think you meant to link to this issue?

@medikoo
Copy link
Owner

medikoo commented Nov 18, 2022

@cdhowie link seems fine, I've linked to v1 spec

@cdhowie
Copy link
Author

cdhowie commented Nov 18, 2022

Sorry, I meant on the spec I think the added text was intended to link to this issue but links to the other one (117) instead.

@medikoo medikoo mentioned this issue Nov 18, 2022
@medikoo
Copy link
Owner

medikoo commented Nov 18, 2022

@cdhowie ah.. I see now, thanks, fixed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants