Skip to content

[Backend] Database connection pool exhaustion causes 500 errors instead of 503 with Retry-After header #787

Description

@ekelemepraise-code

Summary

When the PostgreSQL connection pool is fully saturated, Axum handlers return 500 Internal Server Error instead of 503 Service Unavailable. This breaks client retry logic since 503 is the standard signal for a temporary overload condition.

Observed Behaviour

HTTP/1.1 500 Internal Server Error
{"error": "pool timed out while waiting for an open connection"}

Expected Behaviour

HTTP/1.1 503 Service Unavailable
Retry-After: 5
{"error": "service temporarily unavailable", "retry_after_seconds": 5}

Implementation

In backend/src/error.rs, match the SQLx pool timeout error variant and map it to AppError::ServiceUnavailable:

sqlx::Error::PoolTimedOut => AppError::ServiceUnavailable { retry_after: 5 },

And in the error response serialiser, emit the Retry-After header.

Acceptance Criteria

  • Pool timeout maps to 503 with Retry-After header
  • retry_after value is configurable
  • Integration test simulates pool exhaustion
  • Metrics counter incremented on pool exhaustion

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions