Skip to content

[Feature]: Prevent duplicate Gemini API requests using in-flight request coalescing #380

Description

@Priyanshu1-62

Feature/Project Proposal

  • 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:

  1. Check the cache.
  2. On a cache hit, return the cached response immediately.
  3. On a cache miss, check whether an in-flight Promise already exists for the cache key.
  4. If one exists, await and return its result instead of creating a new Gemini request.
  5. Otherwise, create a new Promise that performs question generation, response parsing, and cache population, store it in the cache registry, and await its completion.
  6. 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.

Priority

High

Additional Context

  • Label: GSSoC'2026, type: feature, type: performance

Checklist

  • I have searched existing issues and discussions before creating this request.
  • I am willing to work on this feature if it is approved.

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions