Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize connection timeout while using Serverless Aurora, looking for a way to increase timeout duration or retry connection

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
}
}
like image 789
Scott Carpenter Avatar asked Sep 23 '18 11:09

Scott Carpenter


1 Answers

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.

like image 134
Scott Carpenter Avatar answered Oct 26 '22 12:10

Scott Carpenter