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

11,556

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.

Share:
11,556
Scott Carpenter
Author by

Scott Carpenter

Updated on June 14, 2022

Comments

  • Scott Carpenter
    Scott Carpenter almost 2 years

    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
    }
    }
    
  • Jérémie
    Jérémie over 5 years
    Thank you so much for posting your own solution! Very useful! npmjs.com/package/mysql#connection-options
  • luliandro
    luliandro almost 5 years
    Thanks for posting this. do you still use a connection pool? or have you disabled it and only have this connectTimeout param?