Unable to retry on connectivity problems (when using pgbouncer)
Unable to retry on connectivity problems (when using pgbouncer)
What are you doing?
We are using sequelize with a postgres database that is behind a pgbouncer connection pool. We have node servers that are connecting to the database, and those servers are using sequelize. Occasionally there are connection problems between the database servers and the node servers, which is (of course) expected.
We wish to handle occasional connection problems by transparently retrying failed queries. To facilicate that, we are using the following retry configuration:
const retry = {
max: Infinity,
match: [
/ConnectionError/,
/SequelizeConnectionError/,
/SequelizeConnectionRefusedError/,
/SequelizeHostNotFoundError/,
/SequelizeHostNotReachableError/,
/SequelizeInvalidConnectionError/,
/SequelizeConnectionTimedOutError/,
/SequelizeConnectionAcquireTimeoutError/,
],
};
What do you expect to happen?
On connection problems, we expect that sequelize would retry the connection until queries succeeds.
What is actually happening?
Sequelize occasionally throws a SequelizeDatabaseError: Connection terminated unexpectedly when there are connectivity problems. SequelizeDatabaseError seems to be the wrong error type to throw for connection problems.
Additional context
I believe that the problem may be "caused" by pgbouncer, but I would still hope sequelize to handle this better, so that there would be some way to retry these queries.
Probably this happens when sequelize is able to connect to the pool (pgbouncer), but the pool has having problems communicating with the database.
Environment
- Sequelize version: 5.21.2
- Node.js version: 12.16.0
- Operating System: OS x
- If TypeScript related: TypeScript version: 3.72, sequelize-typescript version 0.6.11
Issue Template Checklist
How does this problem relate to dialects?
- [ ] I think this problem happens regardless of the dialect.
- [x] I think this problem happens only for the following dialect(s): postgres
- [ ] I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
- [ ] Yes, I have the time and I know how to start.
- [ ] Yes, I have the time but I don't know how to start, I would need guidance.
- [ ] No, I don't have the time, although I believe I could do it if I had the time...
- [x] No, I don't have the time and I wouldn't even know how to start.
I studied the error signature matching logic of retry-as-promised and found the following workaround which appears to solve the problem:
const retry = {
max: Infinity,
match: [
/ConnectionError/,
/SequelizeConnectionError/,
/SequelizeConnectionRefusedError/,
/SequelizeHostNotFoundError/,
/SequelizeHostNotReachableError/,
/SequelizeInvalidConnectionError/,
/SequelizeConnectionTimedOutError/,
/SequelizeConnectionAcquireTimeoutError/,
/Connection terminated unexpectedly/, # this is what appears to solve the problem
],
};
Sequelize can only provide a way to retry queries on some error. This is possible with retry option. So your solution is correct, to retry queries on error which may be appearing from pgbouncer.
stumbled upon this. I needed to retry on any error so I used a regex (not in an array):
const retry = {
max: Infinity,
match: /.*/,
};
Doing that is dangerous: an unexpected unrecoverable error will completely block your software. It will be stuck in an endless retry loop
@ephys You're right. I fixed that by finding more specific errors and limiting the number of retries.