2643 - add Redis-backed metrics cache for multi-instance deployments#2857
Draft
gcgoncalves wants to merge 5 commits intomainfrom
Draft
2643 - add Redis-backed metrics cache for multi-instance deployments#2857gcgoncalves wants to merge 5 commits intomainfrom
gcgoncalves wants to merge 5 commits intomainfrom
Conversation
3488f7f to
7f4f2ac
Compare
Member
|
Thanks @gcgoncalves. Solid implementation — good use of dual-write (Redis + local) with automatic fallback, Prometheus hit/miss counters, and one-time warning for sync callers when Redis is active. A few observations from the diff:
|
Implements centralized metrics aggregation cache using Redis to solve metric fluctuation issue in multi-instance deployments behind load balancers. Problem: - Each gateway instance had its own in-memory cache of PostgreSQL aggregates - Load balancer routing to different instances showed different cached values - Metrics fluctuated non-monotonically on page refresh (e.g., 32075 → 21858) Solution: - Shared Redis cache for aggregated query results across all instances - Cache invalidation after buffer flush ensures consistency - Automatic fallback to local cache if Redis unavailable - Default enabled (METRICS_CACHE_USE_REDIS=true) Changes: - mcpgateway/cache/metrics_cache.py: Add Redis backend with async methods, Prometheus metrics, automatic fallback to local cache - mcpgateway/config.py: Add metrics_cache_use_redis setting (default: true) - mcpgateway/services/metrics_buffer_service.py: Invalidate cache after flush - docs/docs/manage/configuration.md: Document METRICS_CACHE_USE_REDIS - docs/docs/deployment/container.md: Add Docker Compose Redis example - docs/docs/deployment/kubernetes.md: Add multi-instance deployment guide - docs/docs/architecture/performance-architecture.md: Add to caching architecture Technical Details: - PostgreSQL aggregation (func.sum/count) was already correct - Issue was per-instance caching, not aggregation - Redis provides shared invalidation signal for all instances - Cache TTL: 60 seconds (configurable via METRICS_CACHE_TTL_SECONDS) Closes #2643 Signed-off-by: Gabriel Costa <gabrielcg@proton.me>
Signed-off-by: Gabriel Costa <gabrielcg@proton.me>
Signed-off-by: Gabriel Costa <gabrielcg@proton.me>
32b0078 to
3488f7f
Compare
Signed-off-by: Gabriel Costa <gabrielcg@proton.me>
3488f7f to
31d098d
Compare
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.
🐛 Bug-fix PR
📌 Summary
Implements centralized metrics aggregation cache using Redis to solve metric fluctuation issue in multi-instance deployments behind load balancers. This change is behind the
METRICS_CACHE_USE_REDISfeature flag.The downside of this approach is that the metrics are updated on the UI only after the cache TTL is expired. The cache TTL is defined using the
METRICS_CACHE_TTL_SECONDSenvironment setting.Closes #2643
🔁 Reproduction Steps
http://localhost:8080/admin/#metrics#2643
🐞 Root Cause
💡 Fix Description
METRICS_CACHE_USE_REDIS=true)🧪 Verification
make lintmake testmake coverage✅ Checklist
make black isort pre-commit)