The number of connections to an RDS instance is tracked in CloudWatch under the DatabaseConnections metric. Blue Matador monitors the number of connections to a database. For databases running Aurora, MySQL, MariaDB, and PostgreSQL, Blue Matador also warns you when you are near the maximum allowed connections for that database.

Effects


Hitting the maximum amount of connections will cause the database to reject new connections. This can be especially problematic when deploying application code because the code might take up more connections than usual during the release process, and fail to run when the database does not allow connections.

Most applications use a library for managing database connections that will readily log an error when a connection fails, which can be used to verify that you are at the max connections limit. Connecting to your database using a CLI can be useful, but in many failure scenarios you may get lucky and get a connection open when others will be rejected. In the case of MySQL, an account with the SUPER permission will be able to connect even if the database is already at max connections.

 

Solutions


In RDS, the maximum number of connections for Aurora, MySQL, MariaDB, and PostgreSQL is configured via the max_connections parameter in the DB Parameter Group for an instance. The reason that connections are limited in the first place is that each database connection consumes RAM for buffers and result sets that would otherwise be used to increase the performance of the database.

The default value is usually a function that relies on the size of the instance to determine the max number of connections. Determine how many connections your application really needs before making changes to the max_connections parameter. To override the default value for max_connections, create or modify a custom DB Parameter Group and set the max_connections value to allow more connections. Then you must modify the instance and apply the changes either immediately or in the next maintenance window. Alternatively, you can increase the size of the instance to an instance type with a higher amount of memory, since the default max_connections value increases with instance size.  Depending on your RDS set up, changing the instance size or parameters could cause a service disruption, so use caution.

Ensure that your application uses a library that supports connection pooling, which will limit the number of connections that are created to the database. Most libraries that support connection pooling also do a great job of managing the lifecycle of a connection by closing it when no longer needed. From an operations perspective, having a variable connection pool size can be a problem because the application might deploy and function correctly until some time in the future when connections get rejected. Make sure that the maximum size of the connection pool make sense, and that your database can handle the full maximum amount of connections from all services at once.

 

Resources