sequelize icon indicating copy to clipboard operation
sequelize copied to clipboard

Unable to retry on connectivity problems (when using pgbouncer)

Open juhoojala opened this issue 6 years ago • 5 comments

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.

juhoojala avatar Mar 11 '20 09:03 juhoojala

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
  ],
};

juhoojala avatar Mar 11 '20 16:03 juhoojala

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.

sushantdhiman avatar Mar 19 '20 10:03 sushantdhiman

stumbled upon this. I needed to retry on any error so I used a regex (not in an array):

const retry = {
  max: Infinity,
  match:  /.*/, 
};

mahdiou avatar Feb 03 '23 15:02 mahdiou

Doing that is dangerous: an unexpected unrecoverable error will completely block your software. It will be stuck in an endless retry loop

ephys avatar Feb 04 '23 09:02 ephys

@ephys You're right. I fixed that by finding more specific errors and limiting the number of retries.

mahdiou avatar Feb 09 '23 15:02 mahdiou