{
    "slug": "database_connection_pool",
    "term": "Database Connection Pooling",
    "category": "concurrency",
    "difficulty": "intermediate",
    "short": "Connection pooling reuses a fixed set of database connections across requests — eliminating the 50–200ms connection overhead on every request and limiting DB connection count.",
    "long": "Opening a DB connection is expensive (TCP handshake, authentication, SSL). Pooling: maintain N open connections, hand them out per request, return after use. PHP-FPM: each worker maintains its own persistent connection (PDO with ATTR_PERSISTENT). PgBouncer (PostgreSQL), ProxySQL (MySQL): external poolers between app and DB. Benefits: lower latency, limited max connections to DB (DB has hard limits). Pitfalls: persistent connections in PHP-FPM can carry state (transactions, temp tables) — always clean up. Swoole: built-in connection pool per coroutine pool. Laravel Octane: persistent PDO connections per worker.",
    "aliases": [],
    "tags": [
        "concurrency",
        "connection-pool",
        "database",
        "performance"
    ],
    "misconception": "PHP persistent connections (ATTR_PERSISTENT) are the same as a connection pool — persistent connections just reuse the same connection per PHP process; a pool manages multiple connections across processes.",
    "why_it_matters": "Without pooling, a DB with 100 connections limit is exhausted by 100 concurrent PHP-FPM workers — connection poolers let thousands of workers share a limited connection count.",
    "common_mistakes": [
        "Not using PgBouncer/ProxySQL in production — DB exhausts connections under load.",
        "Persistent connections with dirty state (uncommitted transactions left on connection).",
        "Forgetting to return connections to Swoole pool in finally block."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "race_condition",
        "semaphore",
        "php_swoole_concurrency",
        "js_performance_api"
    ],
    "prerequisites": [
        "race_condition",
        "performance_db"
    ],
    "refs": [
        "https://www.pgbouncer.org/"
    ],
    "bad_code": "// New connection per request — 100 workers = 100 connections:\n$pdo = new PDO($dsn, $user, $pass); // Fresh connection each time",
    "good_code": "// PHP-FPM persistent:\n$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_PERSISTENT => true]);\n\n// External pooler config (PgBouncer):\n# pgbouncer.ini:\n# max_client_conn = 1000  (app connections)\n# pool_size = 20           (DB connections)",
    "quick_fix": "Use ATTR_PERSISTENT for PHP-FPM. Add PgBouncer (PostgreSQL) or ProxySQL (MySQL) in production. Set pool size based on DB max_connections and worker count.",
    "severity": "high",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/database_connection_pool",
        "html_url": "https://codeclaritylab.com/glossary/database_connection_pool",
        "json_url": "https://codeclaritylab.com/glossary/database_connection_pool.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Database Connection Pooling](https://codeclaritylab.com/glossary/database_connection_pool) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/database_connection_pool"
            }
        }
    }
}