Connection Creation Cost
What you'll learn: Why creating a new database connection from scratch is surprisingly expensive and slow.
The Hidden Cost of "Hello, Database"
When your application needs to talk to a database, it can't just instantly start sending queries. Establishing a connection involves several resource-intensive steps that happen before any real work begins.
The Three-Part Handshake Tax
Every new database connection carries overhead from:
1. TCP Handshake
Your application and database server must exchange network packets to establish a reliable TCP connection (the SYN, SYN-ACK, ACK dance). This typically requires 1-3 network round trips depending on distance and configuration.
2. Authentication
The database must verify your credentials—checking usernames, passwords, and permissions. This involves cryptographic operations and database lookups that consume CPU cycles and time.
3. Session Initialization
The database allocates memory for your connection, sets up session variables, loads configuration, and prepares internal data structures to track your queries. Each connection needs its own isolated workspace.
Real-World Impact
Creating a single connection might take 10-50 milliseconds or more. If your application creates a fresh connection for every user request, and you handle 1,000 requests per second, you're burning 10-50 seconds of cumulative overhead just to say hello—not counting actual query time.
Analogy: It's like going through airport security every time you want to board a plane, instead of staying in the terminal between flights.
Key Takeaway: Connection creation involves TCP handshakes, authentication, and session setup—costly operations that add significant latency, which is why connection pooling (reusing existing connections) is essential for performance.