<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Tej on Medium]]></title>
        <description><![CDATA[Stories by Tej on Medium]]></description>
        <link>https://medium.com/@tej111997?source=rss-246787be011c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*TD5enOtYeD8sPNEY</url>
            <title>Stories by Tej on Medium</title>
            <link>https://medium.com/@tej111997?source=rss-246787be011c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 12:09:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tej111997/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to handle too many sleeping connections in aws rds.]]></title>
            <link>https://medium.com/@tej111997/how-to-handle-too-many-sleeping-connections-in-aws-rds-8da7707bf3b8?source=rss-246787be011c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8da7707bf3b8</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[rds-proxy]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Tej]]></dc:creator>
            <pubDate>Sat, 20 Aug 2022 19:11:19 GMT</pubDate>
            <atom:updated>2022-08-21T17:55:52.777Z</atom:updated>
            <content:encoded><![CDATA[<h4>What exactly was the issue ?</h4><p>There was a Cron job which was scheduled every day in the morning when there was close to zero traffic to the db. This cron job was a heavy Insert task which opened close to 1000 connections on an average every day. But the problem was these connections are not closing as expected (Most of the days not disconnecting at all) and we can see them in SLEEP mode when we run <em>show processlist </em>on mysql console.</p><p>Below you can take a look at stats of both db connections and cpu utilization. CPU utilization spiked at the time the Cron started running and it was pretty normal rest of the time in the day. This gave us a clear idea that the sleeping connections are not doing any activity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*f7PPWGnjxPeCGJIErJ7Opw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*zt2VVgcU6YLXx2H81Mb0tw.png" /></figure><p>Note : We were using an rds proxy for the Aurora mysql DB.</p><h4>Initial Observations :</h4><p>This issue started to arise the moment we setup an rds proxy for our DB cluster. More about the rds proxy in the link below.</p><p><a href="https://aws.amazon.com/rds/proxy/#:~:text=Amazon%20RDS%20Proxy%20is%20a,database%20failures%2C%20and%20more%20secure.">Amazon RDS Proxy | Highly Available Database Proxy | Amazon Web Services</a></p><p>At first we started checking for anomalies in DB configuration which may lead to these <em>sleeping</em> connections. This can be seen in the mysql console using the query <em>show processlist </em>which shows all the processes at the given instant of time. Which should look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*4ERwG6VJLruej-xSN-uugA.png" /></figure><p>But keep in mind <em>show processlist </em>only gives the status of the queries at that particular instant.</p><p>Then we started to tweak around the DB configurations which we thought were causing the issue. We changed the <em>wait_timeout </em>and <em>interactive_timeout </em>to<em> </em>600 seconds hoping this will close the connections which were in <em>sleep </em>state. You can change these values by first checking which parameter group does this DB belong to and then changing the values in the specified parameter group.</p><p>Note: If the parameter is a default parameter group you cannot modify the values, for that you first have to change the parameter group for that db or create a new one and then modify the values.</p><blockquote><a href="https://us-east-1.console.aws.amazon.com/rds/home?region=us-east-1#dashboard:">You can check the parameter group accosiated with the DB here RD</a>S &gt; <a href="https://us-east-1.console.aws.amazon.com/rds/home?region=us-east-1#databases:">Databases</a> &gt; Database Clsuter &gt; Database_name &gt;Configurations tab</blockquote><blockquote>Commands to get the values of the variables.</blockquote><blockquote>SHOW GLOBAL VARIABLES where Variable_Name=’wait_timeout’;</blockquote><blockquote>SHOW GLOBAL VARIABLES where Variable_Name=’interactive_timeout’;</blockquote><p>This would have worked if we did not have a proxy sitting in between the client and the DB. But in our case it wasnt the fix. Then we tried to manually kill all the connections from the command line and tried to run the cron manually during the working hours to see the action in live.</p><blockquote>CALL mysql.rds_kill(11058); This is an example query to kill one process with id 11058 through mysql console. You can create multiple commands to execute at once by writing a script and copy pasting them directly on to the console.</blockquote><blockquote>#include &lt;iostream&gt;</blockquote><blockquote>using namespace std;</blockquote><blockquote>int main() {</blockquote><blockquote>int i,start_id=500,end_id=1000;</blockquote><blockquote>for(i=0;i&lt;end_id-start_id+1;i++) {</blockquote><blockquote>cout&lt;&lt;”CALL mysql.rds_kill(“&lt;&lt; start_id+i &lt;&lt;”);” &lt;&lt; endl;</blockquote><blockquote>}</blockquote><blockquote>return 0;</blockquote><blockquote>}</blockquote><p>Doing this was also not the fix for the problem we were solving. It didnt work. But one thing that we observed was the “time” column shown above was constantly changing for the same host and same port number. If it was just increasing, that would have been normal behaviour, but the value sometimes was less than the previous one and sometimes greater. This gave us a new direction to the solution. We thought may be the RDS is doing all of this stuff ignoring database configurations and YES we were right. After a bit of research we found that rds was constantly pinging the db in a timely fashion saying that all the opened connections are still alive. Because all the connections(From Rds to the Db) in the connection pool that were opened at the time the Cron started running were “keep-alive” connections . So we started to look into the rds proxy configurations and we found the one which was creating this whole mess.</p><blockquote>That variable was MaxIdleConnectionsPercent</blockquote><p>MaxIdleConnectionsPercent is percentage of total maximum connections allowed to the DB which the proxy maintains even though these connections are idle and doing nothing after <em>wait_timeout (Which is configured in the DB).</em></p><blockquote>Command to get the value of maximum connections. SHOW GLOBAL VARIABLES where Variable_Name=’max_connections’;</blockquote><p>Now based on your requirement you can adjust the proxy configurations in the proxies tab by selecting on the proxy or you can do it through the commad line directly. The command is given below which sets both <em>MaxIdleConnectionsPercent</em> and <em>MaxConnectionsPercent(Which is the maximum number of connections the proxy can pool).</em></p><blockquote>aws rds modify-db-proxy-target-group — db-proxy-name “Your proxy name” — target-group-name “default” — connection-pool-config ‘{ “MaxIdleConnectionsPercent”: 25, “MaxConnectionsPercent”: 50}’</blockquote><p>More about these configurations in the links below.</p><ul><li><a href="https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy-managing.html#rds-proxy-connection-pooling-tuning">Managing an RDS Proxy</a></li><li><a href="https://docs.aws.amazon.com/cli/latest/reference/rds/modify-db-proxy-target-group.html">modify-db-proxy-target-group - AWS CLI 1.25.57 Command Reference</a></li></ul><p><strong>Conclusion:</strong></p><p>All it took was two tweaks to solve this issue, but the important thing was where to look, which took most of our time and which always matters the most. Hope it helps. Thanks!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8da7707bf3b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>