简体   繁体   中英

Connection pooling issue in nodeJS and MySql

I am using NodeJs and Mysql. I have a query to update one field in mysql table as:

Update messaging SET count = count + 1 WHERE msgId = 10;

I am using connnection pool. configuration as:

var pool = mysql.createPool({
connectionLimit : 100,
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database,
port: 3306,
debug: false,
multipleStatements: true});

API that is using this query works when message is delivered to the 5 million users. it makes the connections to remain opened . That results in server processing very slow. Is there any alternative solution to improve the server performance ?

Paradoxically, reducing the number of connections will generally improve performance. Pooled connections are serially reusable resources, and there's a queue of connection requests. So, instead of trying to do a whole lot of queries at once, the DBMS will handle a smaller number of queries at once, and handle the load serially.

So for queries like yours that should run for a short time, create a pool with a small connectionLimit. Try 10. If that works well, try 5.

If you have other queries that take longer, you may want a different connection pool for those.

Not to mention that a clustered node app will open that number of connections for each member of the cluster. 400 connections is too many for almost all MySQL deployments. (If you have a deployment that handles hundreds of connections, your purchasing manager knows about it, because such deployments are very expensive.)

You might have this problem, however: If millions of users are updating exactly the same row msgId = 10 you will have DBMS contention on that row. In that case, you may want to store up the number of hits to the row, and then do UPDATE... SET count = count + n once every few seconds rather than upon every hit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM