15

I'm having an issue at the moment where I'm trying to make use of a Serverless Aurora database as part of my application.

The problem is essentially that when the database is cold, time to establish a connection can be greater than 30 seconds (due to db spinup) - This seems to be longer than the default timeout in Sequelize (using mysql), and as far as I can see I can't find any other way to increase this timeout or perhaps I need some way of re-attempting a connection?

Here's my current config:

const sequelize = new Sequelize(DATABASE, DB_USER, DB_PASSWORD, {
    host: DB_ENDPOINT,
    dialect: "mysql",
    operatorsAliases: false,
    pool: {
      max: 2,
      min: 0,
      acquire: 120000, // This needs to be fairly high to account for a 
      serverless db spinup
      idle: 120000,
      evict: 120000
    }
});

A couple of extra points: Once the database is warm then everything works perfectly. Keeping the database "hot", while it would technically work, kind of defeats the point of having it as a serverless db (Cost reasons). I'm open to simply having my client re-try the API call in the event the timeout is a connection error.

Here's the logs in case they help at all.

{
"name": "SequelizeConnectionError",
"parent": {
    "errorno": "ETIMEDOUT",
    "code": "ETIMEDOUT",
    "syscall": "connect",
    "fatal": true
},
"original": {
    "errorno": "ETIMEDOUT",
    "code": "ETIMEDOUT",
    "syscall": "connect",
    "fatal": true
}
}
Scott Carpenter
  • 449
  • 1
  • 3
  • 10
  • where is that coming from? " acquire: 120000, // This needs to be fairly high to account for a serverless db spinup". Can you point me to the documentation. We currently use the following options: pool:{ max: MAX_MYSQL_POOL_CONNECTIONS, min: 0, acquire: 20000, idle: 3000, evict: 1500, } retry: { max: 5, backoffBase: 1000, // Initial backoff duration in ms. Default: 100, backoffExponent: 1.5, // Exponent to increase backoff each try. Default: 1.1} – Lewix Sep 01 '21 at 20:04

1 Answers1

23

So after some more digging it looks like you can use the dialectOptions prop on the options object to pass things down to the underlying connection.

dialectOptions: {
  connectTimeout: 60000
}

This seems to be doing the trick.

Scott Carpenter
  • 449
  • 1
  • 3
  • 10
  • Thank you so much for posting your own solution! Very useful! https://www.npmjs.com/package/mysql#connection-options – Jérémie Dec 30 '18 at 17:04
  • 1
    Thanks for posting this. do you still use a connection pool? or have you disabled it and only have this connectTimeout param? – luliandro Jul 04 '19 at 21:44