tezvyn:

Connection Pooling: Don't Re-Open, Reuse

AI-drafted, machine-checkedSource: Wikipedia: Connection poolbeginner

A connection pool is a valet service for database access. Instead of creating a new connection for every request, you borrow a ready-made one and return it. This avoids costly setup/teardown in web apps.

WHY IT EXISTS: Establishing a database connection is expensive. It involves a network round-trip, TCP handshake, SSL negotiation, and database authentication. Doing this for every single request in a high-traffic application would be incredibly slow and inefficient, quickly overwhelming the database server. Connection pooling was created to amortize this high setup cost over many requests.

THE MENTAL MODEL: Think of a connection pool like a small fleet of rental cars. Instead of manufacturing a new car for every traveler (a slow and costly process), the agency maintains a fleet of ready-to-go vehicles. A traveler checks out a car, uses it, and returns it to the fleet for the next person. The pool manages a fixed number of pre-established database connections, lending them out to parts of your application as needed.

HOW IT WORKS: When an application starts, the connection pool is initialized. It creates a set number of physical database connections and places them in the pool. When the application needs to query the database, it requests a connection from the pool. If a connection is available, the pool provides it. The application uses the connection to execute its queries and then "closes" it, which doesn't actually terminate the connection but instead returns it to the pool, making it available for other requests. If all connections are in use, a new request will wait for one to be returned, up to a configurable timeout.

WHEN TO USE IT: Use connection pooling in virtually any server-side application that communicates with a database. This is standard practice for web applications, microservices, and API backends. The performance gain is so significant that most modern database drivers and frameworks have built-in pooling capabilities that are often enabled by default.

WHEN NOT TO USE IT: You might not need a connection pool for standalone scripts or batch jobs that run infrequently and make a small number of database queries. In these cases, the overhead of setting up and managing a pool might be more complex than simply opening and closing a single connection.

ONE CANONICAL EXAMPLE: A web server receives 100 concurrent API requests per second. Without pooling, it would attempt to open 100 new database connections. With a connection pool configured to a maximum size of 20, the server handles these requests using only 20 persistent connections. A request borrows a connection, runs its query in milliseconds, and returns it. The same physical connection can serve dozens of different requests within a single second, dramatically reducing load on the database.

Read the original → en.wikipedia.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.