CodexBloom - Programming Q&A Platform

GCP Cloud SQL Connection Pooling implementing Node.js and Sequelize in Production

👀 Views: 0 💬 Answers: 1 📅 Created: 2025-06-12
gcp cloud-sql sequelize nodejs connection-pooling JavaScript

Quick question that's been bugging me - I'm working on a personal project and I'm experiencing issues with connection pooling when using GCP Cloud SQL with a Node.js application that leverages Sequelize ORM. Specifically, I notice that after a period of inactivity, my application fails to connect to the database, throwing a `SequelizeConnectionError: Connection pool exhausted` behavior. I have configured the Sequelize connection like this: ```javascript const { Sequelize } = require('sequelize'); const sequelize = new Sequelize('database_name', 'username', 'password', { host: 'your-cloud-sql-instance-ip', dialect: 'mysql', pool: { max: 5, min: 0, acquire: 30000, idle: 10000, }, }); ``` In my application, I have set the maximum number of connections to 5 and the idle time to 10 seconds. However, during peak loads, I often find that connections seem to drop, and when new requests come in after a few minutes of inactivity, the pool doesn't seem to recover properly, leading to the above behavior. I've also tried increasing the `idle` time to 60000 milliseconds and the `max` connections to 10, but that doesn't seem to help either. From what I've read, it's important to ensure that the connections are correctly closed after use, so I make sure to call `sequelize.close()` whenever my application shuts down. However, I’m still working with this scenario in production. Additionally, I’ve set up Cloud SQL to use a private IP and configured the necessary IAM permissions, following GCP’s best practices. I can successfully connect to Cloud SQL from my local environment without issues—only the production environment seems to be problematic. Any advice on optimizing the connection pooling or troubleshooting this scenario would be greatly appreciated! Thanks in advance! This is part of a larger CLI tool I'm building. I'd really appreciate any guidance on this.