Merged
Conversation
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
kcostell06
pushed a commit
to kcostell06/mcp-context-forge
that referenced
this pull request
Feb 24, 2026
* [PERFORMANCE]: N+1 query pattern in EmailTeam.get_member_count() IBM#1892 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> * [PERFORMANCE]: N+1 query pattern in EmailTeam.get_member_count() IBM#1892 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> * [PERFORMANCE]: N+1 query pattern in EmailTeam.get_member_count() IBM#1892 Signed-off-by: Mihai Criveti <crivetimihai@gmail.com> --------- Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: Fix N+1 query patterns in team member counts
Summary
/admin/P95 latency from 14s to <500ms under loadProblem
Load testing with 3000 concurrent users revealed critical performance issues:
/admin/endpointRoot cause:
EmailTeam.get_member_count()executes a separate COUNT query for each team, creating an N+1 pattern when listing teams.Solution
Phase 1: Batch Query Methods
Three new methods in
TeamManagementService:get_member_counts_batch()- batch fetch member countsget_user_roles_batch()- batch fetch user rolesget_pending_join_requests_batch()- batch fetch pending join requestsPhase 2: Call Site Updates
Updated 6 call sites to use batch methods:
admin.py:admin_ui()admin.py:_render_unified_teams_view()admin.py:admin_list_teams()routers/teams.py:list_teams()routers/teams.py:discover_teams()Phase 3: Database Index
Added partial index for PostgreSQL:
Phase 4: Redis Caching
get_member_counts_batch_cached()with configurable TTLConfiguration
New settings (all optional, sensible defaults):
Performance Impact
/admin/P95 latencyFiles Changed
Configuration
mcpgateway/config.py- New cache settings.env.example- Documentationdocker-compose.yml- Default valuescharts/mcp-stack/values.yaml- Helm chart valuescharts/mcp-stack/values.schema.json- Schema validationdocs/docs/manage/configuration.md- User documentationImplementation
mcpgateway/services/team_management_service.py- Batch methods + cachingmcpgateway/admin.py- Updated call sitesmcpgateway/routers/teams.py- Updated call sitesmcpgateway/alembic/versions/- Index migrationTests
tests/unit/mcpgateway/services/test_team_management_service.pytests/integration/- Cache invalidation testsCloses
Closes #1892
Closes #1893
Contributes to #1894
Contributes to #1862
Testing