Introduction When RDS reaches its max_connections limit, new connections are rejected with "Too many connections" (MySQL) or "FATAL: sorry, too many clients already" (PostgreSQL). This cascades into application errors, failed deployments, and potential data loss.

Symptoms - MySQL error: `ERROR 1040 (HY000): Too many connections` - PostgreSQL error: `FATAL: sorry, too many clients already (max_connections=100)` - Application connection pool exhaustion with timeout errors - RDS CloudWatch metric DatabaseConnections at or near max_connections - Slow queries that hold connections open for extended periods

Common Causes - max_connections parameter too low for workload - Connection leaks in application code (connections not returned to pool) - Sudden traffic spike exceeding planned capacity - Long-running transactions holding connections - Zombie connections from crashed application instances - Missing connection pooling

Step-by-Step Fix 1. **Check current connection count and limit**: ```bash # MySQL mysql -h my-db.xxxx.us-east-1.rds.amazonaws.com -u admin -p \ -e "SHOW STATUS LIKE 'Threads_connected'; SHOW VARIABLES LIKE 'max_connections';"

# PostgreSQL psql -h my-db.xxxx.us-east-1.rds.amazonaws.com -U postgres \ -c "SELECT count(*) FROM pg_stat_activity;" ```

  1. 1.Identify idle connections:
  2. 2.```sql
  3. 3.-- MySQL
  4. 4.SELECT id, user, host, db, command, time, state
  5. 5.FROM information_schema.processlist
  6. 6.WHERE command = 'Sleep' AND time > 300 ORDER BY time DESC;

-- PostgreSQL SELECT pid, usename, state, query_start, query FROM pg_stat_activity WHERE state = 'idle' AND state_change < now() - interval '5 minutes'; ```

  1. 1.Kill idle connections:
  2. 2.```sql
  3. 3.-- MySQL: KILL <process_id>;
  4. 4.-- PostgreSQL:
  5. 5.SELECT pg_terminate_backend(pid)
  6. 6.FROM pg_stat_activity
  7. 7.WHERE state = 'idle' AND state_change < now() - interval '5 minutes';
  8. 8.`
  9. 9.Increase max_connections via parameter group:
  10. 10.```bash
  11. 11.aws rds modify-db-parameter-group \
  12. 12.--db-parameter-group-name my-param-group \
  13. 13.--parameters "ParameterName=max_connections,ParameterValue=500,ApplyMethod=pending-reboot"
  14. 14.aws rds reboot-db-instance --db-instance-identifier my-db-instance
  15. 15.`
  16. 16.Deploy RDS Proxy for connection pooling:
  17. 17.```bash
  18. 18.aws rds create-db-proxy \
  19. 19.--db-proxy-name my-proxy --engine-family MYSQL \
  20. 20.--auth '{"IAMAuth": "DISABLED", "SecretArn": "arn:aws:secretsmanager:..."}' \
  21. 21.--role-arn arn:aws:iam::<account>:role/RDSProxyRole \
  22. 22.--vpc-subnet-ids subnet-xxx subnet-yyy
  23. 23.`

Prevention - Always use connection pooling (RDS Proxy, PgBouncer, or application-level) - Set connection pool max size to 70-80% of max_connections - Configure idle connection timeout (300-600 seconds) - Monitor DatabaseConnections CloudWatch metric with alarm at 80% of max - Implement connection retry logic with exponential backoff