Skip to content

Reduce Database Connections#1606

Merged
stefangutica merged 2 commits into
developmentfrom
reduce-db-connections
Jun 3, 2026
Merged

Reduce Database Connections#1606
stefangutica merged 2 commits into
developmentfrom
reduce-db-connections

Conversation

@stefangutica
Copy link
Copy Markdown
Collaborator

@stefangutica stefangutica commented Jun 3, 2026

Reasoning

  • PersistenceModule.forRoot() was previously imported across multiple feature modules, forcing TypeORM to repeatedly initialize separate MongoDB connection pools.
  • This redundant initialization caused an unnecessary connection spike from 13 to 93 upon application startup (+80 redundant connections for a custom start config).
  • Moving the initialization to the root module eliminates this overhead, resulting in a ~62.5% reduction in newly spawned connections (dropping from +80 down to +30 for a custom start config).
  • This optimization lowers the total peak connection load on the database server by ~54% (from 93 down to 43 connections for a custom start config), preventing potential connection exhaustion.

Proposed Changes

  • Removed .forRoot() imports from all individual feature modules to prevent multiple pool allocations.
  • Registered PersistenceModule.forRoot() exclusively within the main root module where the application is bootstrapped (listen).
  • Utilized the existing @Global() decorator on the module to seamlessly distribute database repositories and PersistenceService globally across the app without breaking dependencies.
  • EXTRA: remove queue.worker.module.ts which is not used anywhere

How to test

  • Boot up the application locally and monitor active connections via MongoDB Compass (Performance tab) or mongosh using db.serverStatus().connections.
  • Verify the optimized footprint: connection count spike should drop significantly
  • Test various feature module endpoints (e.g., NFT metadata, media, settings) to ensure that the global database providers and repositories resolve correctly without throwing dependency errors.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 3, 2026

k6 load testing comparison.
Base Commit Hash: 733e9b6
Target Commit Hash: 71234de

Metric Base Target Diff
AvgMax9095AvgMax9095AvgMax9095
Nodes48.68627.5658.6661.6940.43205.4451.3353.56-16.93% ✅-67.26% ✅-12.50% ✅-13.17% ✅
Tokens48.87436.8158.7161.7540.51266.7651.3553.50-17.11% ✅-38.93% ✅-12.54% ✅-13.37% ✅
Pool48.76439.3858.6861.6740.43227.1751.3153.66-17.08% ✅-48.30% ✅-12.55% ✅-12.99% ✅
Transactions59.48800.6859.9064.0950.732208.3752.1454.91-14.71% ✅+175.81% 🔴-12.95% ✅-14.32% ✅
Mex48.51439.3358.6561.6140.45227.2251.3153.69-16.63% ✅-48.28% ✅-12.50% ✅-12.85% ✅
Blocks53.31658.1059.1862.8345.36637.1451.9655.27-14.92% ✅-3.19% ✅-12.20% ✅-12.04% ✅
Accounts52.07828.4459.1562.4941.611448.4451.4153.89-20.09% ✅+74.84% 🔴-13.08% ✅-13.76% ✅
Test Run Duration60003.2260002.48

Legend: Avg - Average Response Time, Max - Maximum Response Time, 90 - 90th Percentile, 95 - 95th Percentile
All times are in milliseconds.

@stefangutica stefangutica merged commit bbdbfc3 into development Jun 3, 2026
6 checks passed
@stefangutica stefangutica deleted the reduce-db-connections branch June 3, 2026 11:09
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.

3 participants