SQL & Relational Databases (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
Database Connection Pooling: Sizing and Leak Prevention.
senior LevelDatabases & SQL
In Simple Words: Creating a new database connection requires TCP handshakes, TLS negotiation, authentication, and allocating backend server processes (consuming ~5-10MB RAM per connection in Postgres).
- Connection Pools (HikariCP, PgBouncer) maintain a warm pool of reusable connections.
- Formula for Optimal Pool Size: $\text{Pool Size} = ((\text{Core Count} \times 2) + \text{Spindle Count})$. A smaller pool (e.g. 20-30 connections) prevents CPU context-switching thrashing and outperforms a bloated pool of 500 connections!