100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is Connection Pooling?

Understand connection pooling: how reusing database connections improves performance and prevents connection exhaustion under load.

mediumQ25 of 228 in Database Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

Connection pooling is a technique where a fixed set of pre-established database connections is reused across multiple requests instead of opening and closing a new connection every time.

Creating a database connection is expensive โ€” it involves a TCP handshake, authentication, and session setup โ€” so repeating this for every query wastes time and resources. A connection pool keeps a set of live connections ready in memory; when an application needs to talk to the database, it borrows a connection from the pool, uses it, and returns it instead of destroying it. Pool managers like HikariCP or PgBouncer handle sizing, idle timeouts, and health checks automatically. This dramatically reduces latency and prevents the database from being overwhelmed by connection churn under load.

  • Reduces connection setup overhead
  • Improves throughput under load
  • Prevents database connection exhaustion
  • Enables predictable resource usage

AI Mentor Explanation

A stadium keeps a fixed set of golf carts ready near the pavilion instead of manufacturing a brand new cart every time a player needs a lift to the nets. A player borrows an available cart, uses it, and returns it to the pool for the next player, rather than the ground staff building and scrapping a cart per trip. Connection pooling works the same way: a fixed set of database connections is reused across requests instead of opening a brand new one, which is slow, for every single query.

Step-by-Step Explanation

  1. Step 1

    Application requests a connection

    Instead of opening a raw TCP connection, the app asks the pool manager for one.

  2. Step 2

    Pool hands out an idle connection

    If a free, healthy connection exists, it is handed to the requester immediately.

  3. Step 3

    Query executes on the borrowed connection

    The application runs its SQL using the connection as if it opened it itself.

  4. Step 4

    Connection returns to the pool

    After the query finishes, the connection is released back to the pool for reuse rather than closed.

What Interviewer Expects

  • Understanding of why raw connections are expensive
  • Knowledge of pool sizing and timeout tuning
  • Naming real tools like HikariCP or PgBouncer
  • Awareness of connection leaks as a failure mode

Common Mistakes

  • Confusing connection pooling with query caching
  • Not knowing that unreturned connections cause pool exhaustion
  • Assuming a bigger pool is always better without considering database limits
  • Failing to mention real-world pooling libraries

Best Answer (HR Friendly)

โ€œConnection pooling means the application keeps a set of ready-to-use database connections instead of creating a new one for every request. Since opening a connection is slow, reusing them from a shared pool makes the application much faster and prevents the database from being overloaded with connection requests.โ€

Code Example

Configuring a connection pool (pseudo-SQL client config)
-- PgBouncer style pool configuration
[databases]
mydb = host=127.0.0.1 port=5432 dbname=mydb

[pgbouncer]
pool_mode = transaction
max_client_conn = 200
default_pool_size = 20

-- Application simply connects to PgBouncer's port
-- and reuses pooled connections transparently.

Follow-up Questions

  • What happens when a connection pool is exhausted?
  • What is the difference between session pooling and transaction pooling?
  • How do you size a connection pool correctly?
  • What causes a connection leak and how do you detect it?

MCQ Practice

1. What is the main benefit of connection pooling?

Connection pooling avoids the overhead of establishing a new connection for every request by reusing existing ones.

2. Which tool is commonly used for PostgreSQL connection pooling?

PgBouncer is a lightweight connection pooler specifically built for PostgreSQL.

3. What happens if all connections in a pool are in use and a new request arrives?

Requests typically queue or time out waiting for a connection to be returned to the pool.

Flash Cards

What is connection pooling? โ€” Reusing a fixed set of pre-established database connections across requests instead of opening a new one each time.

Name a popular connection pooler. โ€” HikariCP (Java) or PgBouncer (PostgreSQL).

What causes pool exhaustion? โ€” Connections being borrowed but never returned, often due to a leak in application code.

Why is opening a raw connection expensive? โ€” It requires a TCP handshake, authentication, and session setup for every connection.

1 / 4

Continue Learning