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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>When the CPU is overloaded the message queues backup but if CPU usage is okay message queues stay mostly empty.</p>
<p>256 connections per service per cpu. So if you are talking to two services you’d have two pools each with 256 max_connections. Calls to each service use their own pool. We were only calling two services heavily so if you are calling more you may need to adjust a bit.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109689" 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/difficult-debugging-problem/18988/82">Post #81</a>
	                </div>
	            </div>
              <div id="likers-container-109689" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109689"
                     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 #81"></div>
  </section>
</div>
    <div class="postbit" id="109832" data-post-id="109832">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Very interesting thread. Based on the description of incoming / outgoing request rate, you may want to read the guide my team has put together:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://www.rabbitmq.com/docs/networking">
  <header class="source">
      <img src="https://www.rabbitmq.com/img/rabbitmq-logo.svg" class="site-icon" alt="" width="500" height="500">

      <a href="https://www.rabbitmq.com/docs/networking" target="_blank" rel="noopener nofollow ugc">rabbitmq.com</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/190;"><img src="https://www.rabbitmq.com/img/rabbitmq-social-media-card.svg" class="thumbnail" alt="" width="690" height="190"></div>

<h3><a href="https://www.rabbitmq.com/docs/networking" target="_blank" rel="noopener nofollow ugc">Networking and RabbitMQ | RabbitMQ</a></h3>

  <p>&lt;!--</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>This came about due to RabbitMQ MQTT users who ran into issues in scenarios like you describe. You <em>may</em> get a lot of mileage out of adjusting system-level TCP settings.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109832" data-batch-url="/posts/batch_likers">
                        3
                      </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/difficult-debugging-problem/18988/83">Post #82</a>
	                </div>
	            </div>
              <div id="likers-container-109832" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109832"
                     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 #82"></div>
  </section>
</div>
    <div class="postbit" id="109845" data-post-id="109845">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I run a similar application that handles over 1B requests a day. It definitely sounds like you are hitting limits on TCP ports, either open ports in general or TCP ephemeral ports.</p>
<p>We use dedicated servers for this. It’s nice to get 24 CPU cores and 32 GB of RAM for about $100/month… Here are some things to look at. After that, it’s probably K8s related.</p>
<ol>
<li>Basic open file limits for the account running the app: <code>/etc/security/limits.d/foo-limits</code></li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">foo soft    nofile          1000000
foo hard    nofile          1000000
</code></pre>
<ol start="2">
<li>
<p>Open file limits for systemd (which doesn’t respect the limits file), e.g.<br>
<code>LimitNOFILE=65536</code></p>
</li>
<li>
<p>Open file limits in the VM, in <code>vm.args</code> for your release, e.g.<br>
<code>-env ERL_MAX_PORTS 65536</code></p>
</li>
</ol>
<p>You may also want:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">## Enable kernel poll and a few async threads
+K true
+A 128
</code></pre>
<ol start="4">
<li>If you are running Nginx in front (not needed or helpful in this kind of application but here for reference), then you need to tune that as well, e.g.<br>
<code>worker_rlimit_nofile 65536;</code></li>
</ol>
<p>See <a href="https://www.cogini.com/blog/serving-your-phoenix-app-with-nginx/" class="inline-onebox" rel="noopener nofollow ugc">Serving your Phoenix app with Nginx</a></p>
<ol start="5">
<li>After that, you will run into lack of ephemeral TCP ports.<br>
In TCP/IP, a connection is defined by the combination of source IP + source port + destination IP + destination port. In this situation, all but the source port is fixed: 1.2.3.4 + random + 4.5.6.7 + 80. There are only 64K ports. The TCP/IP stack won’t reuse a port for 2 x maximum segment lifetime, which by default is 2 minutes.</li>
</ol>
<p>Doing the math:</p>
<ul>
<li>60000 ports / 120 sec = 500 requests per sec</li>
</ul>
<p>It hits you very hard running behind Nginx as a proxy, but can also hit you on the outbound side when you are talking to a small number of back end servers.</p>
<p>Tune the kernel settings to reduce the maximum segment lifetime, e.g.:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Decrease the time default value for tcp_fin_timeout connection 
net.ipv4.tcp_fin_timeout = 15
# Recycle and Reuse TIME_WAIT sockets faster
net.ipv4.tcp_tw_reuse = 1
</code></pre>
<p>There are other kernel TCP settings you should tune as well, e.g.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">sysctl -w fs.file-max=12000500
sysctl -w fs.nr_open=20000500
ulimit -n 20000000
sysctl -w net.ipv4.tcp_mem='10000000 10000000 10000000'
sysctl -w net.ipv4.tcp_rmem='1024 4096 16384'
sysctl -w net.ipv4.tcp_wmem='1024 4096 16384'
sysctl -w net.core.rmem_max=16384
sysctl -w net.core.wmem_max=16384
</code></pre>
<p>See <a href="https://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></p>
<p>If you are getting limited talking to back end servers, then it’s useful to give your server multiple IP addresses. Then tell your HTTP client library to use an IP from a pool as its source when talking to the back ends. So the equation turns into “source IP from pool” + random port + target IP + 80.<br>
You may be able to reuse outbound connections, with HTTP pipelining, if the back ends support it.<br>
At a certain point, the back end servers may be the limit. They may benefit from having more IPs as well.</p>
<p>DNS lookups on the backends can become an issue. We have had hosting providers block us because they thought we were doing a DOS attack on their DNS. Run a local caching DNS on your server.</p>
<p>See <a href="https://www.cogini.com/blog/best-practices-for-deploying-elixir-apps/" class="inline-onebox" rel="noopener nofollow ugc">Best practices for deploying Elixir apps</a></p>
<p>Glad to give you more specific help if you need it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109845" data-batch-url="/posts/batch_likers">
                        19
                      </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/difficult-debugging-problem/18988/84">Post #83</a>
	                </div>
	            </div>
              <div id="likers-container-109845" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109845"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="109928" data-post-id="109928">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="9mm" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/9mm/120/7475_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  9mm
                    <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>Mother of god <img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"><img src="https://forum.elixirforum.com/images/emoji/apple/clap/3.png?v=15" title=":clap:t3:" class="emoji" alt=":clap:t3:" loading="lazy" width="20" height="20"></p>
<p>Also thank you <a class="mention" href="/u/lukebakken" rel="nofollow">@lukebakken</a></p>
<p>these replies are both 1000% phenomenal and I’m super grateful. I will have to go through each of these very slowly in detail, THANK YOU!!!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109928" 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/difficult-debugging-problem/18988/85">Post #84</a>
	                </div>
	            </div>
              <div id="likers-container-109928" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109928"
                     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 #84"></div>
  </section>
</div>
    <div class="postbit" id="109938" data-post-id="109938">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="9mm" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/9mm/120/7475_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  9mm
                    <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 few questions</p>
<ol>
<li>Is <code>-env ERL_MAX_PORTS 65536</code> the same as <code>+Q 134217727</code>?</li>
<li>i dont see any ipv4.<em>mem</em> options.. are those not available on some servers? maybe because im inside the container… if so im not sure how to set these other sysctl values outside of kubernetes ..hmmmmm (I just checked, its not in the instance itself. im using google container optimized OS). i might just have to do this on vanilla ubuntu servers or something</li>
<li>Whats the easiest way to run a ‘local caching dns’ on the server?</li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109938" 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/difficult-debugging-problem/18988/86">Post #85</a>
	                </div>
	            </div>
              <div id="likers-container-109938" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109938"
                     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 #85"></div>
  </section>
</div>
    <div class="postbit" id="109953" data-post-id="109953">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="benwilson512" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/120/1457_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  benwilson512
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Craft GraphQL APIs in Elixir with Absinthe</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/9mm" rel="nofollow">@9mm</a> KubeDNS / CoreDNS already is one of those. You’ll just want to check out the tuning / configuration link for CoreDNS I mentioned earlier to make sure it has enough resources.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109953" 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/difficult-debugging-problem/18988/87">Post #86</a>
	                </div>
	            </div>
              <div id="likers-container-109953" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109953"
                     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 #86"></div>
  </section>
</div>
    <div class="postbit" id="109954" data-post-id="109954">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<ol>
<li>Yes, <code>ERL_MAX_PORTS</code> is the same as <code>+Q</code>, use that</li>
<li>The kernel params are probably different running in a container</li>
<li>Here is an example of running a local caching DNS on a server: <a href="https://www.cogini.com/blog/running-a-local-caching-dns-for-your-app/" class="inline-onebox" rel="noopener nofollow ugc">Running a local caching DNS for your app</a></li>
</ol> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109954" 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/difficult-debugging-problem/18988/88">Post #87</a>
	                </div>
	            </div>
              <div id="likers-container-109954" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109954"
                     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 #87"></div>
  </section>
</div>
    <div class="postbit" id="109956" data-post-id="109956">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="9mm" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/9mm/120/7475_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  9mm
                    <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>Ok thanks. I actually dont think GKE uses CoreDNS though, they chose to keep using KubeDNS as as far as i can tell</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="109956" 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/difficult-debugging-problem/18988/89">Post #88</a>
	                </div>
	            </div>
              <div id="likers-container-109956" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="109956"
                     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 #88"></div>
  </section>
</div>
    <div class="postbit" id="110453" data-post-id="110453">
  <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>TLDR this is a game of whack-a-mole move-the-bottleneck. A lot of this advice is covered above but I added some additional tips and hopefully some useful commentary.</p>
<p>If you’re still struggling with this I would bypass all of docker/kubes and spin up a bare metal instance at <a href="https://packet.net/" rel="noopener nofollow ugc">https://packet.net/</a> they provide excellent short-term h/w and networking just like cloud VMs just on real hardware, and do a quick comparison to see what your app ends up with without the layers of virtualisation and load balancer goop in the way, and then in order of least effort/max benefit:</p>
<ol start="0">
<li>
<p>read the error messages, they tell you what’s wrong.  all the logs, all the time. measure the impact of your changes.</p>
</li>
<li>
<p>ensure you are running with max ulimit</p>
</li>
<li>
<p>check your erlang VM ports with recon <a href="http://ferd.github.io/recon/" class="inline-onebox-loading" rel="noopener nofollow ugc">http://ferd.github.io/recon/</a> and monitor it to ensure you’re not running out. It’s an easy fix, and adding overhead is not expensive in memory terms.</p>
</li>
<li>
<p>ensure your socket listen queues are not backlogged in erlang. See <a href="http://erlang.org/doc/man/gen_tcp.html#type-option" class="inline-onebox-loading" rel="nofollow">http://erlang.org/doc/man/gen_tcp.html#type-option</a> for some possible options to pass through to your HTTP/TCP libraries, but basically use large buffers, no nagel delay, use tcp keepalive. This varies depending on the particular library you’re using, but this should get you started at least. You will see from outside with <code>netstat -Lan |grep &lt;port&gt;</code> if you’re running out. A healthy acceptor should be <code>0/0/...</code> at all times, anything else you have problems.</p>
</li>
</ol>
<p>Here’s my haproxy and couchdb for example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">fffff80e098a17a0 tcp46 0/0/2000                         *.443 (haproxy)
fffff8071a26e000 tcp4  0/0/2000                         127.0.0.1.5984 (beam.smp from couchdb)
</code></pre>
<ol start="4">
<li>
<p>tune tcp ephemeral port range, and tweak TIME_WAIT reuse if logs show you’re running out.  I see <code>sonewconn</code> errors in kernel logs on FreeBSD but I’m not sure what linux spits out. The rabbitmq examples are excellent, <a href="https://www.rabbitmq.com/networking.html" class="inline-onebox-loading" rel="noopener nofollow ugc">https://www.rabbitmq.com/networking.html</a> more notes below. In particular I’d expect to see <code>{error, eaddrnotavail}</code> being returned from attempts to open new connections in the VM. These should bubble up to your HTTP layer.</p>
</li>
<li>
<p>get real data from the network side with tcpdump &amp; wireshark to see how your external connections are behaving. You may need help interpreting these, if so just skip this til last.</p>
</li>
<li>
<p>ensure that you’re using persistent outbound TLS + HTTP1.1 (or better) connections to your upstream APIs. The first step is to use multiple IPs for outbound connections in your BEAM pool, and potentially secondly to move some work out of the VM entirely - I use haproxy and also do TLS termination there rather than inside the BEAM. This minimises the work the VM has to do, and also gives me a very nice stats page on how the connections are being handled, or retried, and additional load balancing capabilities across instances and servers.</p>
</li>
<li>
<p>go back and read your logfiles again hand in hand with proxy and debugging output looking for correlations.</p>
</li>
</ol>
<p>Further notes below.</p>
<p>Personally I am a <em>huge</em> fan of grabbing a tcpdump on the server, and throwing that into wireshark to see what’s actually happening on the wire. Every time I’ve needed to do this, I’ve wished that I had done it earlier. I appreciate this is not necessarily everybody’s cup of tea but if you’re dealing with network issues, then you need to see what’s actually happening on the wire.</p>
<p>It’s really common (as others have pointed out) to run out of ports, both in the Erlang VM, and in the OS (called ephemeral TCP port exhaustion). The former is set with +Q as mentioned and just requires more ram, the latter is slightly more complicated.</p>
<p>If this is the case your OS should be logging this somewhere, you’ll need to research that to confirm if it’s happening. On my OS (FreeBSD) I see sonewconn errors in kernel logs. A symptom of this is increasing numbers of TCP connections in TIME_WAIT state. You can see this in dmesg and in output of <code>ss -tan state time-wait</code> as mentioned above. The next step is to make more ports available for use as ephemeral ports. See links for details <a href="http://www.ncftp.com/ncftpd/doc/misc/ephemeral_ports.html" class="inline-onebox-loading" rel="noopener nofollow ugc">http://www.ncftp.com/ncftpd/doc/misc/ephemeral_ports.html</a> and <a href="https://www.nginx.com/blog/overcoming-ephemeral-port-exhaustion-nginx-plus/" class="inline-onebox-loading" rel="noopener nofollow ugc">https://www.nginx.com/blog/overcoming-ephemeral-port-exhaustion-nginx-plus/</a> have reasonable explanations.</p>
<p>For nertwork stack tuning in general, see <a href="https://fasterdata.es.net/host-tuning/" class="inline-onebox-loading" rel="noopener nofollow ugc">https://fasterdata.es.net/host-tuning/</a>  <a href="http://proj.sunet.se/E2E/tcptune.html" class="inline-onebox-loading" rel="noopener nofollow ugc">http://proj.sunet.se/E2E/tcptune.html</a> &amp; <a href="https://www.psc.edu/index.php/networking/641-tcp-tune" class="inline-onebox-loading" rel="noopener nofollow ugc">https://www.psc.edu/index.php/networking/641-tcp-tune</a> are excellent references, as well as <a href="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/performance_tuning_guide/#chap-Red_Hat_Enterprise_Linux-Performance_Tuning_Guide-Networking" class="inline-onebox-loading" rel="noopener nofollow ugc">https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html-single/performance_tuning_guide/#chap-Red_Hat_Enterprise_Linux-Performance_Tuning_Guide-Networking</a> but remember that you can’t simply tweak all the above settings without some consequences. slow gradual change and testing are required <img src="/images/emoji/apple/frowning.png?v=15" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20"></p>
<p>If you make your outbound pools <em>too</em> big, <em>and</em> you’re using persistent connections, the upstream servers can also start disconnecting idle connections, thus increasing your connection churn even further. If you can, contact those providers and ask them if they do any connection handling like this or not.</p>
<p>If you’re connecting over and over again to the same upstream IPs <em>or</em> all your connections are coming from a load balancer, (e.g. kubernetes, AWS LB) you may be hitting the time_wait state much much earlier than expected, as the BSD socket API uses the quad tuple (source IP, source port, dest IP, dest port) as a unique key. The solution is not to enable socket reuse in the kernel, but to inject further source &amp; destination IPs into the mix. You can configure additional (non-public) IPs on the server, and have a load balancer bound to more IPs on both inbound and outbound legs of the proxy (if both are needed) so that the quad tuples are spread out across more IPs. Again the output of <code>ss</code> or <code>netstat -Lan</code> as a simple wc is not helpful, you need to see if a given IP is hitting your ephemeral port limits.</p>
<p>Finally, this (at least for me) is where haproxy shines, I can see what’s happening in the logs and change many of these network stack related settings without interrupting my BEAM applications. Separation of concerns is a nice thing, but you do introduce a further component.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="110453" data-batch-url="/posts/batch_likers">
                        15
                      </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/difficult-debugging-problem/18988/90">Post #89</a>
	                </div>
	            </div>
              <div id="likers-container-110453" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="110453"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="110586" data-post-id="110586">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="9mm" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/9mm/120/7475_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  9mm
                    <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>Wow this is 1000% incredible, thank you. I’m literally bookmarking this reply for years to come.<br>
Can you tell me how to run TCP dump in the way you do to feed it into wireshark? That sounds very helpful. I’ll need to read this a few more times to absorb it all. THANK YOU</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="110586" 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/difficult-debugging-problem/18988/91">Post #90</a>
	                </div>
	            </div>
              <div id="likers-container-110586" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="110586"
                     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 #90"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/18988/load_more?page=10">Load more posts (1 remaining)</a>
</div></template></turbo-stream>