Skip to content

Feature: Support Serverless Postgres (Scale-to-Zero) in Atom via sqlx Connection Pool Configuration #42

Description

@rodropcom

Is your feature request related to a problem? Please describe.

Yes. In modern serverless PostgreSQL environments (such as Neon.tech or AWS Aurora Serverless), compute instances automatically scale to zero during periods of inactivity to save costs. For example, Neon requires a continuous 5-minute window of zero active server connections to trigger auto-suspend mode.

Currently, the Magistrala Atom service makes it impossible to achieve scale-to-zero due to two concurrent issues:

  1. Connection Pool Parameters Ignored: Atom ignores standard sqlx connection pool configuration passed via the DATABASE_URL query string (e.g., ?min_connections=0&idle_timeout=300), falling back to sqlx defaults. This prevents setting min_connections=0, forcing Atom to constantly maintain at least idle connections to the database.

  2. Database-Heavy Health Checks: When deployed behind a reverse proxy (like Nginx) or a container orchestrator that sends HTTP health checks to Atom's /health endpoint (e.g., every 30 seconds), Atom validates its database connection by pinging it. This creates a database query every 30 seconds, continuously resetting the serverless inactivity timer.

Together, these behaviors force the underlying database to remain active 24/7, resulting in continuous billing for idle non-production environments.

Describe the feature you are requesting, as well as the possible use case(s) for it.

I would like Atom to be fully compatible with serverless database environments. Specifically:

Respect URI Parameters: Atom should parse and apply standard sqlx connection pool configuration parameters provided in the DATABASE_URL string (specifically min_connections, max_connections, and idle_timeout). Alternatively, expose these as explicit environment variables (e.g., MG_ATOM_DB_MIN_CONNS).

Configurable or Shallow Health Check: The /health endpoint should decouple HTTP server health from database health. It could offer a "shallow" mode that only verifies the HTTP server is responsive, or implement caching/debouncing for the DB status so that a 30-second HTTP ping does not result in a 30-second DB query.

Describe alternatives you've considered I have attempted to mitigate the connection bloat using PgBouncer in transaction mode with an aggressive server_idle_timeout=10s. While this correctly closes idle connections on the server side, the combination of Atom's minimum connection enforcement and frequent 30-second health queries completely defeats PgBouncer's timeout, as the connections are immediately reopened.

Additional context

- Magistrala Version: v0.40.0 (Atom)
- Database: Neon Serverless PostgreSQL 16
- Language/Framework: Rust / sqlx

As serverless Postgres becomes the standard for modern cloud deployments, respecting connection string configuration variables and optimizing health checks is critical for the Magistrala ecosystem to remain cloud-native and cost-effective.

Indicate the importance of this feature to you.

Nice-to-have

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions