Pooling Pattern

By reducing the time required to create and release expensive resources, the pooling pattern improves performance and helps in application scaling

Introduction

Creating and releasing expensive resources can impact application performance. Pooling is a pattern of holding expensive resources (thread, connection, object) in a finite pool and using them from there, rather than using them directly, to reduce the cost of creating and releasing them. Resources in the pool are recycled as soon as they are used up. The pool can be initialized with a fixed number of resources at the beginning. When the resources in the pool are occupied, more resources are added to the pool from the resource environment. The pool size is an adjustable parameter.

Types

Connection Pooling

Imagine you have a web application that serves thousands of users simultaneously and is connected to a database. The web application needs to connect to serve a single request from a user. It needs a socket connection to establish a connection between the web server and the database server. The connection between the two must be terminated as soon as the database returns results. Imagine you have millions of active users, then your application has to establish and disconnect a connection for each request. This process is very expensive. This problem can be optimized by maintaining a pool of connections and using connections from the pool rather than acquiring and releasing for every request. Modern Object-relational mappers (ORM) do the job of maintaining the connection pool by default.

Thread pooling

Imagine building a front-end application that makes 100 parallel HTTP requests when you are on the dashboard screen. It will take longer if these requests are made sequentially. To improve performance, the HTTP requests are sent in parallel. The thread must be created and destroyed to make a single HTTP request to the server. Internally, an HTTP client (Axios/requests/retrofit/HttpClient) manages pools of threads to reduce the cost of creating and releasing threads. It uses a free thread from a pool to retrieve data from the server.

Object Pooling

Application performance degrades dramatically if the application requires many objects that are expensive to instantiate and short-lived. In addition, the CPU is busy with instance creation and garbage collection. To reduce the cost of creation and garbage collection, object pools are kept on the list. This pattern is called object pooling which falls under the creational design pattern.

Advantage

Improves application performance by reducing spent time on costly creation and re-acquisition of resources. It helps to scale of application.

Challenges

  • Adds CPU overheads for maintaining a pool. Less costly than creating and releasing expensive resources.

  • The pool requires synchronization to avoid race conditions.

References

Did you find this article valuable?

Support Rajesh Khadka by becoming a sponsor. Any amount is appreciated!