Skip to content

[Feature]: Architecture - Decouple AI Generation into a Background Message Queue Worker Pipeline #1861

Description

@Diwakar-odds

Feature/Project Proposal

Implement an asynchronous message queue architecture using BullMQ and Redis to handle Gemini AI generations in the background.

Problem Statement

The aiController.js processes Google Gemini AI requests synchronously within the Express request-response cycle. This architecture ties up main thread event loops, leads to frequent API timeouts (504 Gateway Timeouts) on complex prompt generation, and severely limits horizontal scaling.

Proposed Solution

  1. Refactor aiController.js to immediately enqueue an AI generation job into Redis and return a jobId to the client (202 Accepted).
  2. Build a dedicated background worker process (workers/aiWorker.js) that consumes jobs, interacts with the Gemini API using exponential backoff retry logic, and saves the result to MongoDB.
  3. Implement a polling endpoint or WebSocket connection for the frontend to retrieve the completed AI payload.

Alternatives Considered

Instead of BullMQ, we could use Node.js standard worker_threads, but a robust queueing system like BullMQ guarantees job persistence, retries, and cluster scalability across multiple Node instances.

Benefits

Prevents server crashes under heavy AI load, eliminates request timeouts for users, and allows independent scaling of web servers and background workers.

Priority

High

Additional Context

N/A

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

Labels

claimedThis issue has been claimed by a contributor

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions