The Connection Drain
Difficulty: MEDIUMID: connection-pool-leak
The Scenario
Your FastAPI service is crashing with a cryptic error:
FATAL: remaining connection slots are reserved for non-replication superuser connections
Your database has a maximum of 100 connections, but your app keeps opening new ones without closing them. After processing 100 requests, your database refuses all new connections.
The Problem
Your code opens a new database connection for each user inside a loop. This is inefficient - the TCP handshake for each connection adds 10-50ms overhead. With 100 users, that's up to 5 seconds wasted just establishing connections!
The Goal
Implement connection pooling by reusing a single connection.
Requirements:
- Open the database connection once before the loop
- Reuse the same connection for all queries
- Close the connection after all processing is complete
- The test will verify that
sqlite3.connect()is called exactly once
Note: We simulate with SQLite for simplicity, but this applies to Postgres, MySQL, etc.
solution.py
Loading...
⚠️ Do not include PII or secrets in your code.
SYSTEM_LOGS
5/5
// Waiting for execution trigger...
PREVIEW MODE — SOLVE PREVIOUS MISSIONS TO UNLOCK