[eluser]Unknown[/eluser]
Hi, I'm running a restful api backend on codeigniter and have noticed something worrying, we are seeing our MySQL database being saturated by connections fairly quickly, at first I was just running a few performance tests via JMeter when I noticed that after 12 threads the website started to return database errors (Too Many Connections), so after moving the tests locally and monitoring the mysql statistics table for connections we see it peaking up to 127 connections very quickly and failing to go down for a few minutes after the test siege was shut down.
I'm using two database connections, one for a write master mysql instance and another for a read replica. I can see that this setup generates 2 connections for each model I load (via looking at the mysql connection statistics table), but usually this goes down fairly quickly, the problem is when having many clients calling the api methods.
I digged into the issue a bit more and found that this only happens when running over Apache web server, I was unable to trigger this issue with nginx+php-fpm, which leads me to believe this might be something with how apache manages its mod_php process.
I tried to following with no avail:
1) Reduced the MaxRequestsPerChild configuration to around a 100, this makes the issue a bit harder to exploit but I can still make it happen with only one computer throwing requests at the server, it does however, recover faster once the peak connections is reached, which leads me to believe this may be a connection/memory management issue.
2) Create a connection manager class, which does help reduce the number of open connections by reusing already open connections, this however does not work well when multiple queries need to be run simultaneously while retrieving data, all this was done atop of CI connection loader. In the end it was much harder to trigger the issue but this is not a workable solution for me.
I added some debugging messages to see if the connection close method was being called, which was not, which leads me to believe that CI does not explicitly close the connections via its own interface.
I saw a related problem on stackoverflow but blaming it on the TCP/IP spec that demands that the OS keeps the connection alive a few seconds after is closed (FIN state), but I believe this is not the issue as I cannot trigger the issue with nginx.
If anyone had this problem or can shed a light on this I would certainly appreciate it!