Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why limit DB Connection Pool Size in SQLAlchemy?

This is a follow-up to a question I posted earlier about DB Connection Pooling errors in SQLAlchemy.

According to the SQLAlchemy docs the sqlalchemy.pool.QueuePool.__init__() method takes the following argument:

pool_size – The size of the pool to be maintained, defaults to 5. This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain. pool_size can be set to 0 to indicate no size limit; to disable pooling, use a NullPool instead.

What are the drawbacks to setting pool_size=0? What is the benefit of limiting the connection pool size? Is it just to save memory? The database shouldn't really care if a large number of unused connections are open, right?

like image 386
Saqib Ali Avatar asked Jul 22 '16 19:07

Saqib Ali


People also ask

What is connection pool in SQLAlchemy?

A connection pool is a standard technique used to maintain long running connections in memory for efficient re-use, as well as to provide management for the total number of connections an application might use simultaneously.

What is the max pool size in connection pool?

A connection pool is created for each unique connection string. When a pool is created, multiple connection objects are created and added to the pool so that the minimum pool size requirement is satisfied. Connections are added to the pool as needed, up to the maximum pool size specified (100 is the default).

When should you not use connection pooling?

You reuse a prior database connection, in a new context to avoid the cost of setting up a new database connection for each request. The primary reason to avoid using database connections is that you're application's approach to solving problems isn't structured to accommodate a database connection pool.

What happens when DB connection pool is exhausted?

When those additional connections are returned to the pool, they are disconnected and discarded. It follows then that the total number of simultaneous connections the pool will allow is pool_size + max_overflow , and the total number of “sleeping” connections the pool will allow is pool_size .


1 Answers

The main drawback to not limiting the pool size would be the potential for a runaway program to create too many connections.

There are real limits, both at the database level, and O/S level to how many connections the database will support. Each connection will also use additional memory. Limiting the pool size helps protect your database against both bugs in your program, or a malicious attack on your server. Either of those could bring your database server to a standstill by using too many connections or too much memory.

Under normal circumstances, the additional memory each connection uses shouldn't be too much of an issue, but it's best to limit it to the maximum number you think you'll use concurrently (maybe plus a few for good measure).

like image 194
Gerrat Avatar answered Sep 28 '22 10:09

Gerrat