Connection Pool

Connection Pool

On dealing with databases we always face that matter of having a connection limitation on each database server.

Solving this matter, the "singleton" design pattern was evolved to ensure a one connection instance per application.

No alt text provided for this image


But looking to high traffic situations and multiple queries , we face long time issues as a one connection instance keeps all queries inside a queue where they run one after another. So if one query needs 3 minutes to get executed and you have 50 queries requested at the same time , the last query will finish execution after 150 minutes!! TOO LONG.

Moving to another option , what about making a new connection for each query and close it after execution ?

Drawbacks for this option :

1) Making a new connection instance takes time

2)If traffic was higher than database server connections limit , your server gets down.

Here's when connection pool came to solve

Connection pool opens a number of connections that you decide according to your limitation , and it divides queries on those connections. so when a new query comes it looks to a free connection inside the pool otherwise it waits inside the queue.

So instead of using only one connection , you can open 100 and serve 100 requests parallel at the same time without worrying about the amount of traffic hitting your server.

Here is the link for a Nodejs script simulating the difference between running queries using a single instance VS using connection pool.

https://github.com/NourhanMS/ConnectionPool

More resources :

https://riptutorial.com/node-js/example/4587/using-a-connection-pool

Acknowledgement :

Special thanks goes to Alaa Mohamed Hayba for introducing this concept to me .







Asmaa Kamel

Architect and Interior Designer

3 年

????????????????????

Abdulrazak A. Othman

Founder | AWS Community Builder | AI Visionary | Digital Transformation | Data Governance | AI Ethics | Data Driven | Linux Enthusiast

3 年

Great explanation for this topic, its important specially when the environment gets more complex so such problems could be overwhelming

要查看或添加评论,请登录

Nourhan Mohamed Saleh的更多文章

  • DDOS Attacks

    DDOS Attacks

    A distributed denial-of-service (DDOS) attack is a malicious attempt to disrupt the normal traffic of a targeted…

    1 条评论
  • Array vs List vs Linked list

    Array vs List vs Linked list

    I bet a lot have been using a list as an array ! let's discuss the difference between them. 1) Array An array is a…

    8 条评论
  • OAUTH and tokens

    OAUTH and tokens

    Using APIs for communications between different applications and sharing resources has been a main aspect in our age…

  • nodejs_concurrency

    nodejs_concurrency

    Synchronous code in simple words means : Executing code in order . Asynchronous code : Executing code concurrently (at…

    3 条评论

社区洞察

其他会员也浏览了