RDS server come up with 40 connection max, as in the following documentation
I am using Magento 1.9, and at some points, i reach the max number then website is out of service.
Do you have any recommended way to solve this issue?
From my understanding, if i have 2 web servers connection to an RDS server.. then I should have 2 RDS connections, not more.
AWS RDS max_connections limit variable is based on Instance type, so you can upgrade your RDS or make more replica.
The RDS types with max_connections limit:
Update 2017-07
The current RDS MySQL max_connections setting is default by
{DBInstanceClassMemory/12582880}
, if you use t2.micro with 512MB RAM, the max_connections could be (512*1024*1024)/12582880 ~= 40, and so on.Each Web server could have many connections to RDS, which depends on your SQL requests from Web server.
You can change the
max_connections
value by either updating the default parameter policy or create a new one - I'd suggest going with the latter.max_connections
valueHope this helps!
Actual info for Postgresql t3-instances (default.postgres10 parameter group):
Its similar for default.postgres9 and default.postgres11
Login to your RDS instance (using a MySQL client) and run the following query:
The maximum number of simultaneous database connections varies by the DB engine type and the memory allocation for the DB instance class. The maximum number of connections is set in the parameter group associated with the DB instance, except for Microsoft SQL Server, where it is set in the server properties for the DB instance in SQL Server Managment Studio (SSMS).
MariaDB/MySQL
{DBInstanceClassMemory/12582880}
Oracle
LEAST({DBInstanceClassMemory/9868951}, 20000)
PostgreSQL
LEAST({DBInstanceClassMemory/9531392}, 5000)
SQL Server
0 (unlimited)
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.MaxConnections
While increasing the max connections on the server config may fix the problem, you should consider verifying your application configurations and implementations.
I'm not an expert on Magento, but as I recently had a similar issue on a project I was working on, I noticed that the default implementation of the framework I was using created a connection with every call to the database.
While that may not cause any problems to some, the moment you have more visitors or some tasks that are database intensive and can run on multiple connections, the best way to prevent the server crashing with "TOO MANY CONNECTIONS" error is to implement a database connection pool.
This prevents the application from creating more connections that the server can handle, and not crashing the application to the users. A connection pool would keep a queue for the requests to access the database until a connection becomes available so it can proceed with processing the users request.
Just keep in mind that the connection pool should be thread safe in a multi thread scenario.
That's not 40 connections max, that is 40 RDS instances max. You are most likely only using 1 instance based on your description.
You can have thousands of connections to the RDS server because each time a session is opened with the database, that creates a new connection. You are probably running into performance constraints and should look into making the RDS instance larger.
Seems like ~45 connections / 2gb of Ram
I saw formulas, checked my config and it has way more complicated formula for max_connections by default and was having hard time to count what is real limit, so did in simple way - just tested. Monitoring dashboard shows red line when reaching close to the limit. Check below.
db.t3.small
(2gb ram) Chart from monitoring console, as you can see it shows max connections ±46, that spike which crossed red line for a moment is 47Upgrading now to 2x larger instance
db.t3.medium
(4gb ram), i expected to get extra 45 for each 2gb of ram and it seems it is true. Red line can be visible approaching ~90 connections with 4gb box: