Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I choose between Client or Pool for node-postgres

From https://node-postgres.com/features/connecting , seems like we can choose between Pool or Client to perform query

pool.query('SELECT NOW()', (err, res) => {   console.log(err, res)   pool.end() }) 

client.query('SELECT NOW()', (err, res) => {   console.log(err, res)   client.end() }) 

Their functionalities look very much the same. But, the documentation doesn't explain much the difference between Pool and Client.

May I know, what thing I should consider, before choosing between Pool or Client?

like image 806
Cheok Yan Cheng Avatar asked Feb 12 '18 16:02

Cheok Yan Cheng


People also ask

What is difference between pool and client postgres?

You have two options that you can connect to a PostgreSQL server with the node-postgres module. One of the options is to use a single client. The other method is to use a connection pool. However, if your application is using the database very frequently, the pool will be a better option than using a single client.

What are pools and clients?

Client is one static connection. Pool manages a dynamic list/pool of Client objects, with automatic re-connect functionality ;) Normally, you would just create a single Pool object and use it ;) – vitaly-t.

What is pool in node?

A node pool is a group of nodes within a cluster that all have the same configuration. Node pools use a NodeConfig specification. Each node in the pool has a Kubernetes node label, cloud.google.com/gke-nodepool , which has the node pool's name as its value.


1 Answers

May I know, what thing I should consider, before choosing between Pool or Client?

Use a pool if you have or expect to have multiple concurrent requests. That is literally what it is there for: to provide a pool of re-usable open client instances (reduces latency whenever a client can be reused).

In that case you definitely do not want to call pool.end() when your query completes, you want to reserve that for when your application terminates because pool.end() disposes of all the open client instances. (Remember, the point is to keep up to a fixed number of client instances available.)

like image 71
user268396 Avatar answered Oct 04 '22 12:10

user268396