perf(api): cache ProjectAuth in Redis and increase DB connection pool#468
Merged
perf(api): cache ProjectAuth in Redis and increase DB connection pool#468
Conversation
Under high concurrency (50+ concurrent sessions), the auth middleware's
per-request DB query on projects table caused connection pool saturation,
leading to cascading timeouts (88 read timeouts, 8 gateway 504s).
- Add Redis cache for project auth lookups (key: project:auth:{hmac},
TTL: 5min) with graceful fallback to DB on Redis errors
- Increase DB maxOpen from 50→100 and maxIdle from 25→50
GenerQAQ
added a commit
that referenced
this pull request
Mar 23, 2026
…#468) Under high concurrency (50+ concurrent sessions), the auth middleware's per-request DB query on projects table caused connection pool saturation, leading to cascading timeouts (88 read timeouts, 8 gateway 504s). - Add Redis cache for project auth lookups (key: project:auth:{hmac}, TTL: 5min) with graceful fallback to DB on Redis errors - Increase DB maxOpen from 50→100 and maxIdle from 25→50
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.
Why we need this PR?
Benchmark run with 50 concurrent sessions caused cascading API failures: 88 read timeouts + 8 gateway 504s. Root cause: the
ProjectAuthmiddleware hits the database on every request with no caching, which saturated the DB connection pool (maxOpen: 50) under high concurrency. Even though theprojectstable only has 308 rows with a unique index, queries took up to 1.6s because they were waiting for a free DB connection, not for the query itself.Describe your solution
Redis cache for auth lookups —
ProjectAuthnow checks Redis first (project:auth:{hmac}, TTL 5min) before falling back to DB. On Redis errors, it gracefully degrades to the DB path. This eliminates the per-request DB hit for authenticated requests.Increased DB connection pool —
maxOpen: 50→100,maxIdle: 25→50to provide more headroom under concurrent load.Implementation Tasks
lookupProject()helper with Redis-first, DB-fallback patternProjectAuthsignature to accept*redis.ClientRouterDepsto includeRedis *redis.Clientmain.gointo routermaxOpenandmaxIdleinconfig.yamlImpact Areas
Checklist
devbranch.