Search code examples
node.jspostgresqlsequelize.jsamazon-rds

Understanding the Sequelize Pool.Idle Setting?


The Sequelize docs for pool.idle say it is:

The maximum time, in milliseconds, that a connection can be idle before being released.

But on a github issues page, a Sequelize contributor says:

Basically, idle decides if a connection is idle or not, say if connection is not used for 15sec then it is idle.

This suggests that pool.idle has nothing to do with the connection being released, apparently contradicting the Sequelize documentation.

The same comment continues:

Being "idle" does not mean it will get removed from pool. It is evict that will remove any idle connection. As per your config, evictor will run every 10sec and check if there are any idle connection to remove.

I.e. pool.idle just says how long a connection can be unused before it is marked as idle.

Which is correct? Or, are both somehow correct?


Solution

  • Documentation: https://sequelize.org/docs/v6/other-topics/aws-lambda/
    All the Sequelize connections are pushed to pool after finishing the query execution. Idle and evict are part of the pool configuration.

    Idle(not working): This is the wait time(milliseconds) for a DB connection to consider idle(free) before being used for other query/queries execution. After passing this time connections will be considered as idle and all idle connections will be pushed to sequelize pool.

    Evict: The time interval, in milliseconds, after which sequelize-pool will remove idle connections.

    Example: Let's say we've a DB: products. We established a DB connection: seqConnection using sequelize by providing pool: {idle: 160000,evict: 180000} // idle: 2min. evict: 3min

    1. If you don't use the seqConnection for 2 minutes it will be considered idle.
    2. Now you can retain idle seqConnection from sequelize-pool within 3 minutes.
    3. If you don't retain idle seqConnection within 3 minutes then it will be destroyed/removed from the sequelize-pool.
      NOTE: On this point, if you're playing just with a single DB connection at a time. i.e. pool: {max:1} and you want a connection then you've to create a new DB connection.