/plushcap/analysis/cloudflare/how-to-stop-running-out-of-ephemeral-ports-and-start-to-love-long-lived-connections

How to stop running out of ephemeral ports and start to love long-lived connections

What's this blog post about?

Programmers often encounter invalid assumptions when using APIs or systems beyond their limits, leading to unexpected failures. One such case is the Linux networking stack, which can run out of ephemeral ports and become unable to establish any outgoing connections. This issue was experienced by Marek Majkowski while working with TCP and UDP connections. The root cause lies in the fact that each Linux connection consumes a local port (ephemeral port), which limits the total connection count based on the size of the ephemeral port range. The default range contains more than 28,000 ports, but this doesn't mean we can have at most 28,000 outgoing connections. To avoid such issues, programmers should be aware of how to allow source port reuse and prevent having the ephemeral-port-range limit imposed. This can be achieved by using an userspace connectx() function, which is a better way of creating outgoing TCP and UDP connections on Linux. The implementation involves various techniques such as manual source IP and port discovery, REUSEADDR locking dance, and undocumented behavior of the Linux operating system. In summary, understanding the limitations of APIs and systems can help programmers avoid unexpected failures and improve their code's performance and reliability.

Company
Cloudflare

Date published
Feb. 2, 2022

Author(s)
Marek Majkowski

Word count
3310

Language
English

Hacker News points
195


By Matt Makai. 2021-2024.