What do car rentals and libraries have in common? They temporarily lend resources only as long as someone needs them and then take them back so that someone else can use them. In object-oriented programming, such a system is called an ‘object pool’.
Object pooling in real life
Let’s explore the purpose of a library and a book named ‘The Art of Computer Programming’.
- No one needs to read this book 24 hours a day for 7 days a week. People need books only for three purposes: learning, reference, review. A person most likely needs a single book only for 30 – 40 hours put together in an entire lifetime.
- Instead of buying his/her own copy of the book, a person may choose to borrow the book for the duration needed.
- A library solves this need. They stock up multiple copies of the most popular books and also single copies of less popular ones. Let’s call our library, the ‘Friend of Readers’.
- ‘Friends of Readers’ estimates that they should stock up 5 copies of ‘The Art of Computer Programming’ on their shelves to start with. This is what they will call the ‘initial’ or ‘default’ level. If no one has borrowed this book, you will see 5 copies on their shelves.
- As readers start borrowing this book, the library waits and watches. As per library rules, readers need to see at least 2 copies of the book on the shelf. So as soon as the number of copies goes down to 1, the library will retrieve more copies from a basement warehouse or get in touch with the publisher for more copies. Either way, new copies are arranged. How much should they replenish? Upto the ‘default’ level, i.e. until 5 copies are seen on the shelves again.
- But about peak season? Perhaps there is an upcoming graduation examination for computer programming majors. Or some leading companies have announced hiring camps. In such cases, the library will up the ante and stock more than 5 books. But a library is a physical space with limited shelves. They have to set a ‘maximum’. At no point will they have more than 15 copies of the book in circulation. This includes both the borrowed and available books put together.
- As the peak season fades, the library will remove the extra books to free up space and scale back to their default level of 5. The extra books will usually go to a basement warehouse.
So what exactly is object pooling?
Let’s use the library example to describe an ‘object pool’ system. In the object pool, there is a pool of objects that programmers want to use. These objects are called ‘resource objects’. This is similar to the book ‘The Art of Programming’.
A pool manager object overseas the pool. The ‘Friends of Readers’ library is the pool manager. Just like readers do not buy their own copy of the book, the client objects do not directly create a copy of the resource object, but approaches the pool manager.
The pool manager maintains default, minimum and maximum levels. The pool manager starts by creating resource objects matching the default level. It does not allow the level to fall below the minimum. If the level is breached, the pool manager quickly scales up to the default level. During peak usage, the pool manager has to create more objects, but it never creates more than the maximum. At any point of time, there can be no more resource objects in circulation than the level mandated by the maximum.
Object pooling in software
Database connections take time to set up. Once set up, they consume memory and processor to stay put. They are computationally expensive resources. In a big application, it does not make sense for every corner of the program to set up its own database connection. Instead, there is a database connection pool. The pool manager sets up a fixed number of connections and allows other parts of the application to borrow a connection for use. There is a default number of connections to start with, a minimum below which the number of unoccupied connections cannot fall and a maximum number of connections to the database, both used and free combined.
The effort and time required to set up database connections is taken away from the client and managed by the connection pool. The client doesn’t have to worry about disconnecting when done. The pool takes back the returned connection and gives it to another part of the application.
Conclusion
Pooling resources and the system of borrowing and lending is an efficient way to manage resources. The ones who want to use it do not have to worry about how to get it in the first place. They just need to go to a place that has those resources. Since the users do not own the resources, the pool gets them back so that others can use them. This way, a limited number of resources, consuming less space and requiring low maintenance, can make the world a less crowded and less chaotic place for everyone.
Further reading
- Object Pool Design Pattern by Source Making
- Object Pool Pattern by OO Design