node.js - nodejs + mysql : when to use pooled connections? -
first of all, know there several similar questions, don't answer need, let me open new 1 :)
second, question focused mysql, not limited it, applying other poolable services memcached.
as far know, nodejs executes scripts single-threaded can create threads, it's able manage concurrent users in server. that's why makes sense create pool of connections.
the problem comes when have test api served express, , perform following benchmark code:
ab -t 30 -c 1000 localhost/test
giving me following output single-direct-connection database:
requests per second: 1732.07 [#/sec] (mean) time per request: 577.344 [ms] (mean)
in mysql pool 1 connection:
requests per second: 1346.24 [#/sec] (mean) time per request: 742.811 [ms] (mean)
and using pool 100 connections:
requests per second: 662.82 [#/sec] (mean) time per request: 1508.716 [ms] (mean)
which should opposite, right? pooled connection better performance.
i know pooling management requires time (but shouldn't significant) , sql query simple, but... idk...
the api this:
function test(response, request, dbc) { dbc.query(sql, params, (err, rows) => { dbc.release(); if(err) { log.error('error while performing query', err.code); return; } response.send(response, rows); }); }
where dbc
database connection abstracted single/pooled connections, , dbc.release()
nothing if connection not pool (to allow same api work on direct/pooled connections same code, changing option).
am missing here?
just correct 1 little bit: node.js runs non-blocking i/o operations in single thread allows serve multiple requests @ same time (interleaving) - no need threads serve multiple users.
the long response time pooling seems weird must admit. test quite straight forward - fire query , close connection. i'd suppose start better towards pooling if test did "work" before releasing connection. typically there transactions opened, multiple statements executed , on. mimic using simple delay before releasing connection - example 2 seconds.
you can read more how node server runs requests single thread example in "node.js: 100 new requests while still serving first one!"
Comments
Post a Comment