Performance: Connection pooling
Connection pooling affects how quickly the server can handle database requests by reusing open connections instead of opening new ones each time.
Jump into concepts and practice - no test required
from asyncpg import create_pool pool = await create_pool(dsn) async def get_data(): async with pool.acquire() as conn: result = await conn.fetch('SELECT * FROM table') return result
async def get_data(): conn = await asyncpg.connect(dsn) result = await conn.fetch('SELECT * FROM table') await conn.close() return result
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No connection pooling (new connection per request) | N/A | N/A | N/A | [X] Bad |
| Using connection pooling with asyncpg in FastAPI | N/A | N/A | N/A | [OK] Good |
pool_size.pool_size=10, which is valid syntax.from sqlalchemy import create_engine
engine = create_engine('sqlite:///test.db', pool_size=5, max_overflow=0)
# Each request uses engine.connect()engine = create_engine('postgresql://user:pass@localhost/db', pool_size=5, max_overflow=0)
connection = engine.connect()
# forgot to close connection after use
What problem will this cause?Options: A) pool_size=20, max_overflow=10 B) pool_size=1, max_overflow=50 C) pool_size=5, max_overflow=0 D) pool_size=0, max_overflow=0