[#099] stale-while-revalidate cache dedupe#380
Open
Menjay7 wants to merge 2 commits into
Open
Conversation
|
@Menjay7 is attempting to deploy a commit to the ezedikeevan's projects Team on Vercel. A member of the Team first needs to authorize it. |
Author
|
Both PR and conflict are in check sir |
Author
|
Done sir..@ezedike evan |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This PR introduces a stale-while-revalidate (SWR) caching strategy with request deduplication, reducing redundant backend calls and improving response latency under concurrent load. It ensures that identical in-flight requests share a single computation while serving stale cached data immediately.
Changes Made
Implemented stale-while-revalidate caching layer.
Added in-flight request deduplication (single-flight protection).
Cached responses are served immediately while background refresh updates stale data.
Introduced TTL-based cache expiration with soft and hard expiry separation.
Added request key normalization to ensure consistent cache hits.
Integrated cache invalidation hooks for critical mutations.
Added metrics tracking for cache hits, misses, and deduplicated requests.
Problem
Without deduplication and SWR, multiple identical requests:
trigger duplicate expensive backend computations
increase database/API load
degrade performance during traffic spikes
Solution
This PR introduces a combined strategy:
Serve stale cache instantly for fast responses
Revalidate in background without blocking requests
Deduplicate concurrent requests using an in-memory promise registry
This ensures:
Only one upstream request per key at a time
Other requests reuse the same pending result
Cached data remains responsive even during refresh
Key Features
Returns cached response immediately (even if stale)
Triggers background refresh asynchronously
Prevents duplicate in-flight executions
Uses a shared promise map keyed by request hash
Soft TTL: data can still be served while stale
Hard TTL: forces recomputation
Cache hit/miss tracking
Deduplication counters
Revalidation timing metrics
Testing
Unit Tests
Cache hit returns stored value immediately
Stale cache triggers background refresh
Duplicate concurrent requests return same promise
Hard TTL forces recomputation
Cache key normalization correctness
Integration Tests
Multiple parallel requests deduplicated correctly
Backend called only once per unique key
Cache invalidation after mutations
SWR behavior under load
Impact
Reduced backend load under concurrent traffic
Faster average response times
Improved system stability during spikes
Lower database/API cost
Better user experience with near-instant responses
Type of Change
Performance Optimization
Infrastructure Enhancement
Caching Layer Improvement
Reliability Improvement...Closed [#099] stale-while-revalidate cache dedupe #190