Skip to content

perf(api): cache ProjectAuth in Redis and increase DB connection pool#468

Merged
GenerQAQ merged 1 commit intodevfrom
perf/auth-redis-cache-and-db-pool
Mar 23, 2026
Merged

perf(api): cache ProjectAuth in Redis and increase DB connection pool#468
GenerQAQ merged 1 commit intodevfrom
perf/auth-redis-cache-and-db-pool

Conversation

@GenerQAQ
Copy link
Copy Markdown
Contributor

Why we need this PR?

Benchmark run with 50 concurrent sessions caused cascading API failures: 88 read timeouts + 8 gateway 504s. Root cause: the ProjectAuth middleware hits the database on every request with no caching, which saturated the DB connection pool (maxOpen: 50) under high concurrency. Even though the projects table 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

  1. Redis cache for auth lookupsProjectAuth now 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.

  2. Increased DB connection poolmaxOpen: 50→100, maxIdle: 25→50 to provide more headroom under concurrent load.

Implementation Tasks

  • Add lookupProject() helper with Redis-first, DB-fallback pattern
  • Update ProjectAuth signature to accept *redis.Client
  • Update RouterDeps to include Redis *redis.Client
  • Pass Redis client from main.go into router
  • Increase maxOpen and maxIdle in config.yaml
  • Verify build and tests pass

Impact Areas

  • API Server

Checklist

  • Open your pull request against the dev branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.

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 GenerQAQ requested a review from a team as a code owner March 23, 2026 07:24
@GenerQAQ GenerQAQ merged commit 08a91cf into dev Mar 23, 2026
2 of 3 checks passed
@GenerQAQ GenerQAQ deleted the perf/auth-redis-cache-and-db-pool branch March 23, 2026 07:47
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
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.

1 participant