Return stale data and refresh in background instead of waiting #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #9
Instead of expiring data after 24 hours and refreshing, keep the stale data and refresh it in the background. This works by setting a parallel Redis key at
year:[year]:[page]:c:fresh
that does expire after 24 hours. When retrieving the cached data, we check if that key exists - if it doesn't, then we need to refresh the data, but still return the old data.To refresh the data, we trigger a separate HTTP request against the app. This is because I don't trust the promise to resolve on its own if we already return from the function. We can't use the
fetch
passed to the handler since that will resolve against the app without an HTTP request, and we want to make a separate request.Marvel data doesn't change that much, so the amount of stale data should be negligible. This will be a big perf improvement due to how slow the Marvel API can be.