Skip to content

[#099] stale-while-revalidate cache dedupe#380

Open
Menjay7 wants to merge 2 commits into
ezedike-evan:mainfrom
Menjay7:main
Open

[#099] stale-while-revalidate cache dedupe#380
Menjay7 wants to merge 2 commits into
ezedike-evan:mainfrom
Menjay7:main

Conversation

@Menjay7
Copy link
Copy Markdown

@Menjay7 Menjay7 commented Jun 1, 2026

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

  1. Stale-While-Revalidate
    Returns cached response immediately (even if stale)
    Triggers background refresh asynchronously
  2. Request Deduplication
    Prevents duplicate in-flight executions
    Uses a shared promise map keyed by request hash
  3. Cache Strategy
    Soft TTL: data can still be served while stale
    Hard TTL: forces recomputation
  4. Observability
    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

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

@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.

@Menjay7
Copy link
Copy Markdown
Author

Menjay7 commented Jun 1, 2026

Both PR and conflict are in check sir

@Menjay7
Copy link
Copy Markdown
Author

Menjay7 commented Jun 2, 2026

Done sir..@ezedike evan

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[#099] stale-while-revalidate cache dedupe

2 participants