You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Introduce an in-flight request registry for question generation so that concurrent requests for the same topic share a single Gemini API call instead of independently generating identical responses.
When a cache miss occurs, the first request should create and register a Promise responsible for generating and caching the questions. Subsequent requests for the same cache key should await the existing Promise rather than initiating another Gemini request.
Problem Statement
Target file: backend/routes/AptitudeQuestions.js
The current implementation checks the cache before invoking the Gemini API but does not coordinate concurrent cache misses.
If multiple requests for the same topic arrive before the first request has populated the cache, each request independently calls the Gemini API, performs retries, parses the response, and writes the same data back into the cache.
This cache stampede increases external API usage, introduces unnecessary latency and retry traffic, and may accelerate rate limiting under heavy workloads.
Proposed Solution
Maintain an in-memory Map keyed by the normalized cache key, where each entry stores the Promise representing an ongoing question generation request.
New request flow:
Check the cache.
On a cache hit, return the cached response immediately.
On a cache miss, check whether an in-flight Promise already exists for the cache key.
If one exists, await and return its result instead of creating a new Gemini request.
Otherwise, create a new Promise that performs question generation, response parsing, and cache population, store it in the cache registry, and await its completion.
Remove the Promise from the registry so that future cache misses can trigger fresh generation.
Benefits
Eliminates duplicate Gemini API requests for concurrent cache misses.
Reduces external API usage and associated costs.
Lowers the possibility of rate limiting during traffic spikes.
Improves response consistency by ensuring concurrent requests receive the same generated result.
Integrates cleanly with the existing NodeCache implementation without introducing additional dependencies.
Feature/Project Proposal
Problem Statement
Target file:
backend/routes/AptitudeQuestions.jsProposed Solution
New request flow:
Benefits
NodeCacheimplementation without introducing additional dependencies.Priority
High
Additional Context
Checklist