Skip to content

[FEATURE]: Integrate Throng Clustering for Performance and Concurrency #180

Description

@Gautam25Raj

Affected Apps / Packages

Server (apps/server), Infrastructure / Other

Is your feature request related to a problem?

Yes. The backend Express application runs on a single Node.js thread by default. This does not utilize multi-core host machines in staging/production environments (e.g. AWS, Render, Docker). A single-threaded setup experiences request queuing and high latency under concurrent load, and presents a single point of failure if a request causes an unhandled exception that crashes the process.

Describe the solution you'd like

We want to introduce clustering using throng to spawn a pool of Node.js worker processes that share the same port and handle incoming requests concurrently.

  1. Master / Worker Topology:

    • Use throng in the server entry point index.ts.
    • The master process will read CLUSTERING_ENABLED and WEB_CONCURRENCY (worker count) configurations and boot the workers.
    • Each worker process will initialize its own database/Redis connections and start an instance of the Express server.
  2. Cron & Background Job Isolation:

    • Background sync and maintenance cron jobs (e.g. githubSyncJob, viewsFlushJob) must only run on Worker 1 to avoid duplicate execution, race conditions, and excessive database locks.
  3. Graceful Shutdown:

    • Implement process signal handlers (SIGTERM, SIGINT) in both the master and worker processes to perform a graceful teardown:
      • Stop the HTTP server from accepting new connections.
      • Complete in-flight requests with a reasonable timeout.
      • Disconnect from Redis and Prisma cleanly.

Describe alternatives you've considered

  • Running multiple container instances: While container orchestration (e.g., ECS, Kubernetes) handles scaling well, application-level clustering inside each container optimizes cost and CPU utilization.
  • Using PM2: PM2 is heavy and requires separate process monitors, whereas programmatic clustering with throng fits seamlessly inside a standard Node/TypeScript entry point.

Additional Context

  • This setup will require adding CLUSTERING_ENABLED and WEB_CONCURRENCY configurations in config.ts and the .env configuration files.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Fields

No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions