DEV Community

Discussion on: Explain database connection pooling like I'm five

Collapse
 
nestedsoftware profile image
Nested Software • Edited

Let's say a connection pool is like the books at your local library (we'll work on the assumption that the library is really close to your home). If you want to borrow a book, you stroll over, check it out (the connection from the pool is assigned to you), and start reading. The library has multiple copies of popular books (multiple connections in the pool), so other people can still borrow a book even if you've got a copy checked out. When you return the book to the library (return the connection to the pool), it becomes available again for the next person. Let's say a book is so popular that all of the local copies are checked out (no more connections in the pool). In that case, you may order the book from another branch (create a new connection), but this takes much longer. Alternatively, you may just wait until a copy is returned by someone else (wait for the pool to have a free connection again).

As users interact with an application, it's common for the application to connect to a database to get and save data. To speed things up, connection pools can be used by the application to maintain open sessions to the database. When these are not in use, the pool just keeps the connections open. When a user requests a connection, the pool assigns the connection to that user. When the user is done using the connection, it is returned to the pool. If you know how many users typically use your application at the same time, that gives you an idea of how many connections to set up for the pool.