<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="312728" data-post-id="312728">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="maxguzenski" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maxguzenski/120/10877_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  maxguzenski
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I use it as a middleware to graphql (Absinthe) that keep query results in cache to fast response. Like “query usersSearch() {}”, an user does not need to receive a very updated list of registered profiles, we keep a 30min cached data for that case.</p>
<p>usersSearch can receive a list of filter, so cache is per filters and per logged user.</p>
<p>I’m working on that api since 2010 (initially rails), I rewrote everything in elixir 2015 or 2016. Since 2010 until today, each month has set a new record in the number of accesses.</p>
<p>This API has been greatly refined over time, and today it has around 700 local tests. It’s been many months since the backend has had any glitches or bugs, so I’m finding what’s happening very strange.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="312728" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-312728" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="312728"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="313318" data-post-id="313318">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="posilva" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/posilva/120/10741_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  posilva
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/maxguzenski" rel="nofollow">@maxguzenski</a> quick questions:</p>
<ul>
<li>How many container s are running in the cluster when this happens? ( With a pool of 100 and running 10 containers that will result in 1000 connections)</li>
<li>What is the criteria of scaling up if is scaling too fast it will put pressure on the connections to DB (specially if it’s over SSL) what can cause some timeouts</li>
<li>What is the connect timeout?</li>
<li>There are also some configs (timeout to killl if idle) around DB Idle connection, that can cause issues.</li>
<li>Depending how heavy is the app accessing the database I would use a smaller pool for connections (100 is too much IMO without knowing more).</li>
<li>it’s running on ECS on EC2 or Fargate? EC2 may give you some hints about the OS (network buffers, open files etc) using sysctl/netstat and similar tools</li>
</ul>
<p>Maybe you got already over it.</p>
<p>Next questions would be around Erlang VM using Recon and some Erlang VM inspection tooling.</p>
<p>Cheers</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313318" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-313318" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313318"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="313341" data-post-id="313341">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dch" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dch/120/2682_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dch
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>lots of great points already here, but not much on OS level.</p>
<h2><a name="p-313341-questions-1" class="anchor" href="#p-313341-questions-1" aria-label="Heading link" rel="nofollow"></a>questions</h2>
<ul>
<li>any info on what is the bottleneck? storage? network? kernel?</li>
<li>is there a load balancer in between you and the users?</li>
<li>are there any errors reported in kernel logs at this time?</li>
<li>any other sysctl or other tunables set for ulimit, max open files, …</li>
<li>are you running into any tcp ephemeral port exhaustion? this is surprisingly common when running behind load balancers, you should monitor total ports, and what state they are in, at runtime, both in kernel and of elixir app.</li>
</ul>
<p>I would initially check the networking side first, before looking at storage. A reasonable hypothesis is this:</p>
<ul>
<li>you are running out of free tcp sockets for inbound connections and/or queued connections in the kernel, and they’re not being recycled fast enough by OS</li>
<li>this could cause both the DB pool to be unable to spawn new connections, <em>and</em> same for phoenix on accepting new user connections</li>
<li>user requests start piling up in process mailboxes, causing memory to balloon</li>
<li>everything rapidly turns to custard</li>
</ul>
<h2><a name="p-313341-tldr-2" class="anchor" href="#p-313341-tldr-2" aria-label="Heading link" rel="nofollow"></a>TLDR</h2>
<p>You need to check/fix all of ulimits <em>as seen by beam.smp process</em>, kernel backlog &amp; accept queues, phoenix acceptors, backlogs, and max requests.</p>
<ul>
<li>check <code>cat /proc/&lt;pid&gt;/limits</code> for elixir beam.smp pid to ensure ulimits are ok (fix in systemctl unit file or ulimit if not a systemd service)</li>
<li>check <code>ss -s</code> to see how many “active” connections there are at runtime</li>
<li>save full output of <code>ss -tan</code> somewhere for review later</li>
<li>check <code>sysctl net.ipv4.ip_local_port_range</code> and increase it <code>sysctl -w net.ipv4.ip_local_port_range="4096 65535"</code> to give more headroom</li>
<li>check <code>ss -lnt</code> to see how many active connections phoenix is handling atm</li>
<li>adjust phoenix to handle more active connections if required (use gen_tcp backlog 1024 as a reasonable number)</li>
</ul>
<h2><a name="p-313341-longer-3" class="anchor" href="#p-313341-longer-3" aria-label="Heading link" rel="nofollow"></a>Longer</h2>
<blockquote>
<p>NB many caveats and handwavey innacuracies, to keep it simple.</p>
</blockquote>
<p>Incoming network connections arrive in the kernel, and are queued in several places.</p>
<ul>
<li>first kernel queue is backlog queue, until tcp handshake is completed (syn, then syn-ack reply, final ack from client)</li>
<li>next kernel queue is the accept queue, waiting for your listening app to accept the next connection. The kernel will buffer 1.5x the maximum configured connections on behalf of your app. This default is either 128 or 4096 depending on OS. Note that 1.5 * 4096 is 6144 which is awfully close to 5500. Could be coincidence of course!</li>
<li>final queue is gen_tcp acceptors in phoenix app, if there are no free ones (busy with existing requests), then we “push back” and requests start piling up in the buffer, then the accept queue itself overflows, and the backlog queue, until new requests <em>for both receive and send</em> get rejected.</li>
</ul>
<p>If you are using the default port range, you’ll have around 28000 available sockets behind a NAT. If each user request creates 2 tcp requests in, and a further temporary 1 or 2 out to DB or elsewhere, then 5500 active users brings you pretty close to maxing that out. Also another coincidence possibly!</p>
<p>You may see this info in AWS LB metrics, or in whatever grafana or similar server metrics you collect too.</p>
<p>Probably something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">### whats the configured ephemeral port range
# sysctl net.ipv4.ip_local_port_range
### how many sockets are open atm? one of these commands should work for you
### they all give slightly different info
# netstat -nat
# ss -lnt
# ss -tan | awk '{print $4}' | cut -d':' -f2 | sort | uniq -c | sort -n
</code></pre>
<p><a href="https://forum.elixirforum.com/t/difficult-debugging-problem/18988/90" rel="nofollow">I wrote a bit about this in the past</a> so start with that. Some details on tcp networking in general:</p>
<h2><a name="p-313341-background-4" class="anchor" href="#p-313341-background-4" aria-label="Heading link" rel="nofollow"></a>background</h2>
<ul>
<li><a href="https://blog.cloudflare.com/how-to-stop-running-out-of-ephemeral-ports-and-start-to-love-long-lived-connections/" class="inline-onebox" rel="noopener nofollow ugc">How to stop running out of ephemeral ports and start to love long-lived connections</a></li>
<li><a href="https://vincent.bernat.ch/en/blog/2014-tcp-time-wait-state-linux" class="inline-onebox" rel="noopener nofollow ugc">Coping with the TCP TIME-WAIT state on busy Linux servers</a></li>
<li><a href="https://veithen.io/2014/01/01/how-tcp-backlog-works-in-linux.html" class="inline-onebox" rel="noopener nofollow ugc">How TCP backlog works in Linux</a></li>
<li><a href="https://krisnova.net/posts/linux-accept-queues/" class="inline-onebox" rel="noopener nofollow ugc">Observing and Understanding Accept Queues in Linux | Kris Nóva</a></li>
</ul>
<h2><a name="p-313341-finding-appropriate-tunables-settings-5" class="anchor" href="#p-313341-finding-appropriate-tunables-settings-5" aria-label="Heading link" rel="nofollow"></a>finding appropriate tunables / settings</h2>
<ul>
<li><a href="https://www.rabbitmq.com/networking.html#dealing-with-high-connection-churn" class="inline-onebox" rel="noopener nofollow ugc">Networking and RabbitMQ | RabbitMQ</a></li>
<li><a href="https://www.phoenixframework.org/blog/the-road-to-2-million-websocket-connections" class="inline-onebox" rel="nofollow">The Road to 2 Million Websocket Connections in Phoenix - Phoenix Blog</a></li>
<li><a href="https://www.cogini.com/blog/tuning-tcp-ports-for-your-elixir-app/" class="inline-onebox" rel="noopener nofollow ugc">Tuning TCP ports for your Elixir app</a></li>
<li><a href="https://www.getpagespeed.com/server-setup/nginx/maximizing-nginx-performance-a-comprehensive-guide-to-tuning-the-backlog-and-net-core-somaxconn-parameters" class="inline-onebox" rel="noopener nofollow ugc">Maximizing NGINX Perfo... [Performance Boost] - GetPageSpeed</a></li>
</ul>
<h2><a name="p-313341-cant-i-just-query-how-many-ephemeral-ports-are-left-6" class="anchor" href="#p-313341-cant-i-just-query-how-many-ephemeral-ports-are-left-6" aria-label="Heading link" rel="nofollow"></a>can’t I just query how many ephemeral ports are left?</h2>
<p>No. That complexity can wait for another day.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313341" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-313341" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313341"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="313532" data-post-id="313532">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="maxguzenski" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maxguzenski/120/10877_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  maxguzenski
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/posilva" rel="nofollow">@posilva</a> <a class="mention" href="/u/dch" rel="nofollow">@dch</a></p>
<p>We believe we have fixed the problem. Monday will be the real test (as it is the day of the week with the most hits).</p>
<p>Problem summary:</p>
<p>Under high demand (more than 40k requests/minute), the app completely freeze from one moment to the next, which was resolved with a complete docker container restart. RAM usage grew exponentially in a few seconds and the logs stopped being reported (Logger dropped the logs, but what could be seen in the logs a few seconds before they stopped was a large number of connection errors with the database. )</p>
<p>Even requests without access to the database gave a timeout. Which prevented me from seeing any telemetry generated by the application. (and I wasn’t using any external services to collect beam data)</p>
<p>The application runs in Docker on a c6i.4xlarge (only 1 instance) connecting to AWS Aurora (r6.4xlarge).</p>
<p>I immediately scaled the application to a c6.12xlarge and the database as well, hoping to have time to investigate the problem. But the problem remained the same.</p>
<p>Initially what helped was increasing the db connection pool (ecto) from 200 to 2000, but even then the application needed to be restarted every 30 minutes.</p>
<p>It wasn’t an OS-level problem.</p>
<p>Solution:<br>
A good database engineer improved some aurora queries and configurations, this didn’t solve the problem at all but it improved the time the application was responsive.</p>
<p>A RateLimit was implemented to prevent abuse of requests from some users. This helped the application to run much longer (+/- 1 hour).</p>
<p>But what really resolved it was reversing absinthe (graphql) from version 1.7.5 to 1.6.8, which had already been well tested in the past. Version 1.7 had already been used for a couple months, but I believe that something happens with asinthe, when it is under intense use, that does not happen in version 1.6.x.</p>
<p>Now let’s wait for Monday, and if the issue really solved, I will do performance tests with absinthe (or stay with version 1.6.8).</p>
<p>Thank you very much to everyone who helped!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="313532" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-313532" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="313532"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="316692" data-post-id="316692">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="posilva" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/posilva/120/10741_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  posilva
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/maxguzenski" rel="nofollow">@maxguzenski</a> let us know if it fixed the issue. Maybe there are some n+1 query problems with GraphQL data loaders <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" loading="lazy" width="20" height="20"> . I am just curious</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="316692" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-316692" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="316692"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="316805" data-post-id="316805">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="maxguzenski" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/maxguzenski/120/10877_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  maxguzenski
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I roll it back to version 1.6.8 of absithe and 1.0.11 of dataloader and everything just worked.</p>
<p>For now I haven’t had time to test further what causes the performance issue with the latest version of absinthe/dataloader.</p>
<p>There are no n+1 queries.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="316805" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-316805" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="316805"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="326143" data-post-id="326143">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="LetThereBeDwight" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/LetThereBeDwight/120/34480_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  LetThereBeDwight
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/maxguzenski" rel="nofollow">@maxguzenski</a> were you able to do any further investigation into the issues you were seeing with Absinthe 1.7.x? My team and I are currently facing issues that sound very similar to the ones you were having.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="326143" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/app-in-production-crashing-with-dbconnection-connectionerror/60622/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-326143" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="326143"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>