connection-pooling
Skill4 starsChanged 4 months agoMITView on GitHub
Reuse database connections instead of opening one per request — persistent connections under PHP-FPM, external poolers (ProxySQL, PgBouncer, RDS Proxy), and sizing pools against the server's connection limit. Use when the user sees per-request overhead from opening a fresh DB connection (TCP handshake, TLS, auth) and wants to reuse connections to cut latency; the user hits "Too many connections" errors, or the database is saturated by more client connections than it can serve, and needs to bound and pool them; the user is deciding how to reuse connections given their runtime — PHP-FPM, Swoole/RoadRunner workers, or serverless — and wants the right strategy. Keywords: connection-pooling, persistent-connections, performance, database, php-fpm, proxysql, pgbouncer, max-connections, pdo.
2 other projects use a version of this.
Adopted by
Most recently taken up by
- auralshin/coding-skillstheir own version, first seen Aug 27, 2026
- ffsshhttiikk/opencode-agents-skillstheir own version, first seen Feb 28, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- ffsshhttiikk/opencode-agents-skills★ 2same name
- auralshin/coding-skills★ 0same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository119 other files it gives its agents.
- strategy-bitrix-d7-incrementalSkill
- strategy-explicit-over-magicSkill
- strategy-framework-conventionsSkill
- strategy-legacy-safe-editsSkill
- strategy-minimal-diffSkill
- strategy-no-untested-refactorSkill
- strategy-preserve-public-apiSkill
- strategy-readonly-immutabilitySkill
- strategy-security-firstSkill
- strategy-test-drivenSkill
- openapiSkill
- paginationSkill
- rest-designSkill
- versioningSkill
- cqrsSkill
- dependency-inversionSkill
- hexagonalSkill
- repository-patternSkill
- service-layerSkill
- bitrixSkill
- findings-reportSkill
- laravelSkill
- performanceSkill
- phpSkill
- project-analysisSkill
- refactoringSkill
- roi-prioritizationSkill
- securitySkill
- stack-detectionSkill
- cache-layersSkill
- component-engineSkill
- d7-eventsSkill
- d7-loaderSkill
- d7-orm-queriesSkill
- iblock-elementsSkill
- kernel-main-moduleSkill
- localizationSkill
- controllersSkill
- ormSkill
- validationSkill
- controllersSkill
- query-builderSkill
- routingSkill
- autoload-psr4Skill
- dependency-managementSkill
- scriptsSkill
- mysql-indexingSkill
- redisSkill
- transactionsSkill
- ci-github-actionsSkill
- deployment-strategiesSkill
- docker-phpSkill
- env-configSkill
- associationsSkill
- entities-mappingSkill
- orm-basicsSkill
- entity-apiSkill
- module-developmentSkill
- routing-controllersSkill
- formsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 68
Do not add pooling or persistent connections to a one-shot CLI script or migration that opens one connection and exits.
- Distinctive: line 70
Do not add an external pooler when the app comfortably fits inside max_connections and connection setup is not a measured bottleneck.
- Very specific: line 84
Read the database max_connections, subtract headroom for admin/replicas, then divide by the number of app hosts to get a per-host worker …
- Very specific: line 88
Keep idle timeout under the server wait_timeout, wrap the first query in a reconnect-once retry for "gone away", and alert on …
- Distinctive: line 94
When many app instances need few concurrent server connections, put an external pooler (ProxySQL for MySQL, PgBouncer for Postgres, RDS …
- Very specific: line 97
Use transaction-level pooling (PgBouncer transaction mode / ProxySQL multiplex) for maximum reuse only if the app avoids session-scoped …
- Very specific: line 100
Keep the connection idle timeout on the pooler shorter than the database's wait_timeout, and validate or reconnect on a "server has gone …
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
<!-- generated: do not edit — source: knowledge/atoms/databases/connection-pooling/ --><!-- schema: atomVersion=3 skillVersion=2 graphVersion=1 --><!-- compile: @php-skills/compiler v1.0.0 (deterministic build) -->Database Connection Pooling
When to use
- The user sees per-request overhead from opening a fresh DB connection (TCP handshake, TLS, auth) and wants to reuse connections to cut latency.
- The user hits "Too many connections" errors, or the database is saturated by more client connections than it can serve, and needs to bound and pool them.
- The user is deciding how to reuse connections given their runtime — PHP-FPM, Swoole/RoadRunner workers, or serverless — and wants the right strategy.
When NOT to use
- Do not add pooling or persistent connections to a one-shot CLI script or migration that opens one connection and exits.
- Do not add an external pooler when the app comfortably fits inside max_connections and connection setup is not a measured bottleneck.
Core guidance
- Use persistent connections under PHP-FPM — Under PHP-FPM enable persistent connections (
PDO::ATTR_PERSISTENT => trueor ap:DSN prefix) so each worker reuses its socket across requests instead of reconnecting on every request. - Size total connections below the server limit — Persistent connections multiply: FPM workers x servers must stay under the
database's
max_connections, leaving headroom for admin and replicas. Cap FPMpm.max_childrenaccordingly rather than assuming pooling adds capacity.
Quick recipe
- Read the database
max_connections, subtract headroom for admin/replicas, then divide by the number of app hosts to get a per-host worker budget. (verify: sum ofpm.max_childrenacross hosts plus headroom is belowmax_connections.) - Set
PDO::ATTR_PERSISTENT => trueon the DSN and setpm.max_childrento the per-host budget from step 1 so workers reuse sockets without exceeding the limit. (verify: New requests reuse existing connections andThreads_connectedstays flat under load.) - Keep idle timeout under the server
wait_timeout, wrap the first query in a reconnect-once retry for "gone away", and alert onThreads_connectednearingmax_connections. (verify: A killed backend connection recovers on the next request and the connection-count alert fires before saturation.)
Best practices
- When many app instances need few concurrent server connections, put an external pooler (ProxySQL for MySQL, PgBouncer for Postgres, RDS Proxy on AWS) in front so hundreds of clients share a small warm backend pool
- Use transaction-level pooling (PgBouncer transaction mode / ProxySQL multiplex)
for maximum reuse only if the app avoids session-scoped state — prepared
statements pinned to a session, temp tables,
SETvariables, advisory locks - Keep the connection idle timeout on the pooler shorter than the database's
wait_timeout, and validate or reconnect on a "server has gone away" error so a request never inherits a dead persistent socket - In Swoole/RoadRunner/Octane workers, hold a real in-process connection pool and return each connection after use, rather than opening a connection per job
Anti-patterns
| Anti-pattern | Why it fails | Do instead |
| Opening a new connection per query or per function | Connecting per query multiplies latency and connection count, the exact cost pooling exists to remove. | Create one connection per request (or check out from a pool), inject it, and<br>reuse it for all queries in that request. |
| Persistent connections without resetting transaction state | Persistence reuses the exact server session, so an unfinished transaction or stray SET bleeds into the next user. | Ensure every request commits/rolls back and clears session state before it<br>returns, or use a pooler that resets the connection on checkin. |
| An unbounded pool sized to peak concurrency | Past a point more connections add context-switching and lock contention, lowering throughput instead of raising it. | Set a bounded pool near the database's optimal concurrency (often a small<br>multiple of CPU cores) and let requests queue briefly for a connection. |
See references/anti-patterns.md.
Common mistakes
- Persistent connections turn a scale-out into a connection storm — Enabling persistent connections and then scaling out FPM workers multiplies open
connections (workers x hosts), which can blow past
max_connectionsand lock everyone out. Fix: Cappm.max_children, put an external pooler in front so app scale is decoupled from backend connections, and monitorThreads_connectedagainst the limit. - Session state leaks across requests on a persistent connection — A persistent connection carries over server-side state — an uncommitted
transaction,
SETsession variables, temp tables, or the current database — into the next request that reuses it. Fix: Always commit or roll back before the request ends, avoid relying on session variables, and reset connection state (or disable persistence) where isolation matters. - Reusing a dead connection throws "server has gone away" — A cached persistent connection can be closed by the server (idle
wait_timeout, restart, failover) while the client still believes it is open, so the next query fails with error 2006 "MySQL server has gone away". Fix: Keep idle timeouts belowwait_timeout, enable the client's ping/validation, and wrap the first query in a reconnect-once retry after a gone-away error. - Server-side prepared statements break under transaction pooling — In PgBouncer transaction mode (or ProxySQL multiplexing), server-side prepared
statements,
LISTEN/NOTIFY, advisory locks, and temp tables fail because each transaction may land on a different backend connection. Fix: Use client-side/emulated prepares, avoid session-scoped features, or switch that connection to session-mode pooling.
Version notes
- PDO persistent connections work from PHP 7.4 onward (PHP >=7.4) —
PDO::ATTR_PERSISTENTand the mysqlip:DSN prefix are available on PHP 7.4 and all later versions; there is no in-language connection pool, so external poolers or worker runtimes remain the way to get true pooling.
Security
- Keep TLS and least-privilege on pooled connections — Terminate the app-to-pooler and pooler-to-database hops over TLS, and give the pooled DB user only the privileges the app needs — do not weaken auth to make pooling easier.
Testing
- Activation and decision behavior are verified in
eval/scenarios/databases/connection-pooling.yaml.
Additional resources
- references/anti-patterns.md
- references/core.md
- references/examples.md
- references/pitfalls.md
- references/recipes.md
- references/version-differences.md
Knowledge graph
Related: databases/mysql-indexing, databases/transactions
<!-- graph-fragment: graph/fragments/databases/connection-pooling.yaml -->Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

