Skip to content

Postgres connection exhaustion on dev: authenticated requests fail with 'too many clients already' (500) or time out (504) #134

Description

@saqibmanan

Summary

Authenticated requests to the dev backend intermittently exceed nginx's 60s proxy timeout and return 504 Gateway Time-out, while unauthenticated endpoints answer in ~0.2s. Roughly half of calls to the token-exchange endpoint are affected.

Found while enabling the API smoke suite against dev (CivicDataSpace-test#19) — the tests had never actually run in CI, so this had no way of surfacing.

Measurements

All taken from a single session against https://dev.api.civicdataspace.in:

POST /api/auth/keycloak/login/ — four consecutive calls:

Attempt Result
1 504 after 60.1s
2 200 in 6.9s
3 200 in 42.2s
4 504 after 60.1s

GET /api/activities/user/ (authenticated) — three consecutive calls:

Attempt Result
1 200 in 8.3s
2 200 in 8.3s
3 200 in 50.3s

What this is not

Deliberately ruled out:

Endpoint Latency
Keycloak OIDC discovery 0.13s
Keycloak JWKS (/certs) 0.12s
Keycloak userinfo 0.13s
Backend GET /health/ 0.20s

Keycloak is fast and the backend's own health endpoint is fast. /health/ reports database, elasticsearch and redis all healthy. So this is not the auth.civicdatalab.in migration, not general network latency, and not a wholly-down service — it is specific to the authenticated request path inside the backend.

The bimodal distribution (fast, or 40-60s+, with little in between) looks more like waiting on a resource than like slow computation — a connection pool, a lock, or a blocking call in auth middleware would all produce this shape.

Impact

  • Any client doing the Keycloak token exchange fails about half the time on dev.
  • ParakhAI is affected: its DataSpaceAuthMiddleware forwards user tokens to this endpoint, so a timeout there degrades ParakhAI requests.
  • It masked a test defect. A cross-application test asserted status != 401, which a 504 satisfies — so it passed while the endpoint was timing out entirely. Tightened in ParakhAI_test#10.

Current mitigation (tests only)

CivicDataSpace-test#19 retries gateway timeouts in the shared API client and skips authenticated tests when the exchange never answers, so an infrastructure problem stops presenting as an API contract failure. That keeps CI usable — it does not fix this, and the retries will keep hiding it until it is addressed.

Health checks there remain strict, so a genuine outage still fails.

Suggested next step

Compare timings on prod. If prod is clean this is dev-environment resourcing; if prod shows the same shape it is a code path worth profiling — starting with what the auth middleware does per request and whether it holds a DB connection while making an outbound call.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions