Database and HTTP connection pooling patterns for Python async applications. Use when configuring asyncpg pools, aiohttp sessions, or optimizing connection lifecycle in high-concurrency services.
View on GitHubyonatangross/skillforge-claude-plugin
ork
January 25, 2026
Select agents to install to:
npx add-skill https://github.com/yonatangross/skillforge-claude-plugin/blob/main/plugins/ork/skills/connection-pooling/SKILL.md -a claude-code --skill connection-poolingInstallation paths:
.claude/skills/connection-pooling/# Connection Pooling Patterns (2026)
Database and HTTP connection pooling for high-performance async Python applications.
## Overview
- Configuring asyncpg/SQLAlchemy connection pools
- Setting up aiohttp ClientSession for HTTP requests
- Diagnosing connection exhaustion or leaks
- Optimizing pool sizes for workload
- Implementing health checks and connection validation
## Quick Reference
### SQLAlchemy Async Pool Configuration
```python
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(
"postgresql+asyncpg://user:pass@localhost/db",
# Pool sizing
pool_size=20, # Steady-state connections
max_overflow=10, # Burst capacity (total max = 30)
# Connection health
pool_pre_ping=True, # Validate before use (adds ~1ms latency)
pool_recycle=3600, # Recreate connections after 1 hour
# Timeouts
pool_timeout=30, # Wait for connection from pool
connect_args={
"command_timeout": 60, # Query timeout
"server_settings": {
"statement_timeout": "60000", # 60s query timeout
},
},
)
```
### Direct asyncpg Pool
```python
import asyncpg
pool = await asyncpg.create_pool(
"postgresql://user:pass@localhost/db",
# Pool sizing
min_size=10, # Minimum connections kept open
max_size=20, # Maximum connections
# Connection lifecycle
max_inactive_connection_lifetime=300, # Close idle after 5 min
# Timeouts
command_timeout=60, # Query timeout
timeout=30, # Connection timeout
# Setup for each connection
setup=setup_connection,
)
async def setup_connection(conn):
"""Run on each new connection."""
await conn.execute("SET timezone TO 'UTC'")
await conn.execute("SET statement_timeout TO '60s'")
```
### aiohttp Session Pool
```python
import aiohttp
from aiohttp import TCPConnector
connector = TCPConnector(
# Connection limits
limit=100