Course contentsShow
← System Design
Lesson 272 of 1,9198. Database Performance OptimizationFree lesson

Connection Timeouts and Limits

Configuring checkout timeout, idle timeout, and max lifetime to prevent connection leaks and handle blocking clients.

Connection Timeouts and Limits

What you'll learn: How timeout settings prevent connection pools from clogging up with stuck or abandoned connections.

Why Timeouts Matter

In a connection pool, connections can get "stuck" in problematic states: waiting forever for a slow database query, held by a crashed application thread, or sitting idle indefinitely. Without timeouts, these connections become dead weight—occupying pool slots but doing no useful work. Your pool fills up with zombies, and legitimate requests start timing out or failing.

Three Critical Timeout Types

Checkout timeout (or connection wait timeout): How long a thread will wait to acquire a connection from the pool before giving up. If the pool is exhausted, this prevents requests from waiting forever.

Checkout timeout = 5 seconds
→ Thread waits max 5s for a free connection, then fails fast

Idle timeout: How long a connection can sit unused in the pool before being closed. This prevents accumulating stale connections that may have been silently dropped by the database.

Max lifetime: The absolute maximum age of any connection before forced retirement and replacement. Database servers often close connections after a certain time; this setting proactively rotates them first.

The Connection Leak Problem

Without these limits, a single blocking query or forgotten connection can permanently remove a connection from circulation—a connection leak. Over time, leaks drain your pool dry.

Think of it like a library: checkout timeout is how long you wait in line, idle timeout returns books left on tables, and max lifetime replaces worn-out books entirely.

Key Takeaway: Connection timeouts (checkout, idle, max lifetime) are essential guardrails that prevent pools from becoming clogged with dead, leaked, or stale connections, ensuring healthy connection recycling.