node-postgres : ECONNREFUSED when connections are too many
I've developed a simple resource for my nodejs express app. I'm using brianc's node-postgres client for handling connections to my postgres database. Following async/await style from the documentation, my users.js controllers should look like this:
code:
const { Pool } = require('pg');
const pool = new Pool();
module.exports = {
index: async (req, res, next) => {
try {
const dbResult = await pool.query(`select * from users`, );
//do stuff with the data here and return response
} catch (err) {
next(err);
}
}
}
I believe the resource is pretty simple, and it works fairly. However I wanted to load test my server using this endpoint using loadtest. I tested it using concurrency of 100 at 200 requests/second using:
loadtest:
loadtest -H authorization:(sometoken) -c 100 --rps 200 http://localhost:3000/users
I would run my node app and look at the console while the loadtest is being conducted. The first couple of hundred requests would push through status 200 fine, while soon, i would get errors ECONNREFUSED in most of the requests. Error rate is usually at 62% of total requests (a lower rate would ofcourse be just fine).
initial findings:
I've done a lot of googling, trial and error. I've found that ECONNREFUSED is a result of connection limit being maxed out. But I thought connection pooling is supposed to handle it since it recycles/reuses client/conections.
I've also experimented with tinkering with postgresql.conf setting max_connections = n, and shared_buffers = nGB, tried different configs and none of them worked, error rate would be the same.
Tried different styles, promise-based, callback, and I still get the same error rate for the loadtest. Tried tinkering with connectionTimeoutMillis paramater, and no value gives any change.
I believe breaking at 200 rps with 100 concurrency is bad for a simple resource such as a simple select query. If it's a machine limitation, I'm using a fairly okay machine - Core i7 6700, 16GB DDR4. Did I miss something or am I doing something wrong? Thanks!
node.js postgresql express concurrency connection-pooling
add a comment |
I've developed a simple resource for my nodejs express app. I'm using brianc's node-postgres client for handling connections to my postgres database. Following async/await style from the documentation, my users.js controllers should look like this:
code:
const { Pool } = require('pg');
const pool = new Pool();
module.exports = {
index: async (req, res, next) => {
try {
const dbResult = await pool.query(`select * from users`, );
//do stuff with the data here and return response
} catch (err) {
next(err);
}
}
}
I believe the resource is pretty simple, and it works fairly. However I wanted to load test my server using this endpoint using loadtest. I tested it using concurrency of 100 at 200 requests/second using:
loadtest:
loadtest -H authorization:(sometoken) -c 100 --rps 200 http://localhost:3000/users
I would run my node app and look at the console while the loadtest is being conducted. The first couple of hundred requests would push through status 200 fine, while soon, i would get errors ECONNREFUSED in most of the requests. Error rate is usually at 62% of total requests (a lower rate would ofcourse be just fine).
initial findings:
I've done a lot of googling, trial and error. I've found that ECONNREFUSED is a result of connection limit being maxed out. But I thought connection pooling is supposed to handle it since it recycles/reuses client/conections.
I've also experimented with tinkering with postgresql.conf setting max_connections = n, and shared_buffers = nGB, tried different configs and none of them worked, error rate would be the same.
Tried different styles, promise-based, callback, and I still get the same error rate for the loadtest. Tried tinkering with connectionTimeoutMillis paramater, and no value gives any change.
I believe breaking at 200 rps with 100 concurrency is bad for a simple resource such as a simple select query. If it's a machine limitation, I'm using a fairly okay machine - Core i7 6700, 16GB DDR4. Did I miss something or am I doing something wrong? Thanks!
node.js postgresql express concurrency connection-pooling
add a comment |
I've developed a simple resource for my nodejs express app. I'm using brianc's node-postgres client for handling connections to my postgres database. Following async/await style from the documentation, my users.js controllers should look like this:
code:
const { Pool } = require('pg');
const pool = new Pool();
module.exports = {
index: async (req, res, next) => {
try {
const dbResult = await pool.query(`select * from users`, );
//do stuff with the data here and return response
} catch (err) {
next(err);
}
}
}
I believe the resource is pretty simple, and it works fairly. However I wanted to load test my server using this endpoint using loadtest. I tested it using concurrency of 100 at 200 requests/second using:
loadtest:
loadtest -H authorization:(sometoken) -c 100 --rps 200 http://localhost:3000/users
I would run my node app and look at the console while the loadtest is being conducted. The first couple of hundred requests would push through status 200 fine, while soon, i would get errors ECONNREFUSED in most of the requests. Error rate is usually at 62% of total requests (a lower rate would ofcourse be just fine).
initial findings:
I've done a lot of googling, trial and error. I've found that ECONNREFUSED is a result of connection limit being maxed out. But I thought connection pooling is supposed to handle it since it recycles/reuses client/conections.
I've also experimented with tinkering with postgresql.conf setting max_connections = n, and shared_buffers = nGB, tried different configs and none of them worked, error rate would be the same.
Tried different styles, promise-based, callback, and I still get the same error rate for the loadtest. Tried tinkering with connectionTimeoutMillis paramater, and no value gives any change.
I believe breaking at 200 rps with 100 concurrency is bad for a simple resource such as a simple select query. If it's a machine limitation, I'm using a fairly okay machine - Core i7 6700, 16GB DDR4. Did I miss something or am I doing something wrong? Thanks!
node.js postgresql express concurrency connection-pooling
I've developed a simple resource for my nodejs express app. I'm using brianc's node-postgres client for handling connections to my postgres database. Following async/await style from the documentation, my users.js controllers should look like this:
code:
const { Pool } = require('pg');
const pool = new Pool();
module.exports = {
index: async (req, res, next) => {
try {
const dbResult = await pool.query(`select * from users`, );
//do stuff with the data here and return response
} catch (err) {
next(err);
}
}
}
I believe the resource is pretty simple, and it works fairly. However I wanted to load test my server using this endpoint using loadtest. I tested it using concurrency of 100 at 200 requests/second using:
loadtest:
loadtest -H authorization:(sometoken) -c 100 --rps 200 http://localhost:3000/users
I would run my node app and look at the console while the loadtest is being conducted. The first couple of hundred requests would push through status 200 fine, while soon, i would get errors ECONNREFUSED in most of the requests. Error rate is usually at 62% of total requests (a lower rate would ofcourse be just fine).
initial findings:
I've done a lot of googling, trial and error. I've found that ECONNREFUSED is a result of connection limit being maxed out. But I thought connection pooling is supposed to handle it since it recycles/reuses client/conections.
I've also experimented with tinkering with postgresql.conf setting max_connections = n, and shared_buffers = nGB, tried different configs and none of them worked, error rate would be the same.
Tried different styles, promise-based, callback, and I still get the same error rate for the loadtest. Tried tinkering with connectionTimeoutMillis paramater, and no value gives any change.
I believe breaking at 200 rps with 100 concurrency is bad for a simple resource such as a simple select query. If it's a machine limitation, I'm using a fairly okay machine - Core i7 6700, 16GB DDR4. Did I miss something or am I doing something wrong? Thanks!
node.js postgresql express concurrency connection-pooling
node.js postgresql express concurrency connection-pooling
asked Nov 13 '18 at 17:22
muffinmuffin
84242661
84242661
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Check your pool configuration as described in node-pool. You should set max=100
to cope with your 100 concurrency assuming your database is able to accept 101 connections (concurrency + a query browser for the dev guy :) ).
For fine tuning to avoid waiting resources, probably acquireTimeoutMillis
and maxWaitingClients
are good values to look at.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286443%2fnode-postgres-econnrefused-when-connections-are-too-many%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Check your pool configuration as described in node-pool. You should set max=100
to cope with your 100 concurrency assuming your database is able to accept 101 connections (concurrency + a query browser for the dev guy :) ).
For fine tuning to avoid waiting resources, probably acquireTimeoutMillis
and maxWaitingClients
are good values to look at.
add a comment |
Check your pool configuration as described in node-pool. You should set max=100
to cope with your 100 concurrency assuming your database is able to accept 101 connections (concurrency + a query browser for the dev guy :) ).
For fine tuning to avoid waiting resources, probably acquireTimeoutMillis
and maxWaitingClients
are good values to look at.
add a comment |
Check your pool configuration as described in node-pool. You should set max=100
to cope with your 100 concurrency assuming your database is able to accept 101 connections (concurrency + a query browser for the dev guy :) ).
For fine tuning to avoid waiting resources, probably acquireTimeoutMillis
and maxWaitingClients
are good values to look at.
Check your pool configuration as described in node-pool. You should set max=100
to cope with your 100 concurrency assuming your database is able to accept 101 connections (concurrency + a query browser for the dev guy :) ).
For fine tuning to avoid waiting resources, probably acquireTimeoutMillis
and maxWaitingClients
are good values to look at.
answered Nov 16 '18 at 15:56
Luis MuñozLuis Muñoz
4,15621431
4,15621431
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286443%2fnode-postgres-econnrefused-when-connections-are-too-many%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown