Why PostgreSQL Connection Pooling Gets Harder in Kubernetes
A routine deployment can exhaust PostgreSQL connections without any increase in traffic. New pods open their pools while terminating pods still hold database sessions. Add several worker processes per pod, and connection demand can grow much faster than the replica count suggests. Connection pooling is supposed to absorb that churn. The post Why PostgreSQL Connection Pooling Gets Harder in…
PostgreSQL connection pooling can become challenging in Kubernetes environments. When pods are created or terminated, their pool sizes and terminated sessions can lead to unexpected connection demands. Connection pooling is designed to handle this, but in Kubernetes, pool sizing, autoscaling, and termination are separate processes. Each component may operate independently, causing the database to run out of capacity.
Consider an API with 12 pods, each having four worker processes and a maximum of five database connections per worker. Directly connecting to PostgreSQL allows for 240 sessions. Adding more pods increases the potential connection count. Kubernetes advises factoring in pods that are about to be terminated, as they still hold database sessions.
The max_connections parameter in PostgreSQL determines the ceiling for connections. After accounting for reserved connections and an operational buffer, the remaining connections are distributed among workloads. An increase in max_connections means more shared memory allocations in PostgreSQL. The number of available connection slots does not dictate the efficiency of database queries.
To optimize connection usage, consider reducing the number of per-worker pools or using transaction poolers like PgBouncer. Each application pod can have its own pooler, connected nearby. A shared PgBouncer deployment allows application replicas to use a limited number of backends, but each PgBouncer replica has its own limits.
PgBouncer allocates pools based on database/user pairs. With reserve pools disabled and no aggregate connection limits, three PgBouncer instances with four database/user pairs can handle 240 backends. The max_client_conn parameter controls incoming client connections but does not set a limit for backends.
Configure max_db_connections and max_user_connections individually for each PgBouncer instance. Consider the total capacity, taking into account database aliases referring to the same PostgreSQL database and replicas sharing poolers during updates. If one pooler fails, other poolers should handle reconnecting clients within the current limits.
Uneven capacity distribution can occur. When using a single PostgreSQL database from multiple instances limited to 60 server connections each, the total available connections become 180 with four instances. PostgreSQL's statement_timeout cannot account for waiting time outside the transaction, so consider application compatibility checks and setting idle_in_transaction_session_timeout for better management.
Rewritten by urgent.news; may contain errors — check the original.
Source: cloudnativenow.com
