Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

In a high-traffic server environment, managing the maximum number of TCP/IP connections efficiently is important. Linux imposes limits on both client and server sides to manage these connections by default.

In this tutorial, we’ll explore ways to adjust system configurations to increase the maximum of TCP/IP connections to ensure better performance in high-demand scenarios.

2. Understanding Client-Side Limit

On the client side, two key factors affect the number of outbound TCP connections: The ephemeral port range and the TCP FIN timeout.

2.1. Ephemeral Port Range

The ephemeral port range defines the range of ports available for outbound connections from a host IP. We can check the default range with the sysctl command:

$ sysctl net.ipv4.ip_local_port_range
net.ipv4.ip_local_port_range = 32768	60999

This means that the local port range starts at 32768 and ends at 60999. Furthermore, to determine the total number of ports available for outbound connection, we calculate the difference between the maximum and minimum port numbers in the range:

Number of Ports = Maximum Port - Minimum Port + 1
Number of Ports = 60999 - 32768 + 1 = 28232

Thus, by default, the Linux system has 28232 ports available for outbound connections.

If we need more connections, we can extend the range:

$ sudo sysctl net.ipv4.ip_local_port_range="15000 61000"
net.ipv4.ip_local_port_range = 15000 61000

This command increases the number of outbound connections by increasing the size of the port range.

2.2. TCP FIN Timeout

The tcp_fin_timeout determines how long a connection stays in the TIME_WAIT state before being released:

$ sysctl net.ipv4.tcp_fin_timeout
net.ipv4.tcp_fin_timeout = 60

By default, the value is 60 seconds. However, we can reduce the time to a lower value, such as 30 seconds:

$ sudo sysctl net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_fin_timeout = 30

This command reduces the timeout to 30 seconds. In particular, it frees up ports faster thereby increasing the number of available connections.

3. Adjusting Server-Side Limits

On the server side, several system parameters can be adjusted to handle a larger number of incoming connections.

3.1. Increase somaxconn

The somaxconn parameter defines the maximum number of connections the kernel can queue for a listening socket. By default, this is set to 128.

Let’s increase the number of connections the kernel can queue for a listening socket:

$ sudo sysctl net.core.somaxconn=1024
net.core.somaxconn = 1024

We increased the value to 1024 which improved the server’s ability to handle large volumes of incoming connections.

3.2. Increasing txqueuelen

The txqueuelen parameter affects the length of the transmission queue for network interfaces. By default, the value is typically set to 1000. Although, this varies depending on the operating system or network interface configuration.

When dealing with high network traffic, especially large bursts of data, increasing the txqueuelen parameter can help avoid packet loss. However, when there is a lot of outbound traffic, a larger queue can ensure that the system can handle a surge in outgoing packets without dropping them.

For example, let’s increase the value with the use of ifconfig command to better handle heavy network traffic:

$ sudo ifconfig eth0 txqueuelen 5000

Increasing this value by 5000 helps handle large bursts of network traffic.

3.3. Adjusting netdev_max_backlog

This parameter controls the maximum number of packets that can be queued for processing by the network stack. By default, the value is 1000, but we can increase it to 2000:

$ sudo sysctl net.core.netdev_max_backlog=2000
net.core.netdev_max_backlog = 2000

This enhances the system’s performance. This means the system can temporarily store more incoming packets in the queue, giving it more time to process them.

3.4. Increasing tcp_max_syn_backlog

The tcp_max_syn_backlog parameter controls how many SYN requests the kernel can queue before it starts dropping them. For example, let’s increase the tcp_max_syn_backlog parameter from its default of 1024 to 2048:

$ sudo sysctl net.ipv4.tcp_max_syn_backlog=2048
net.ipv4.tcp_max_syn_backlog = 2048

Increasing the parameter improves the handling of new connection requests.

4. Optimizing TCP Throughput

Additionally, in some cases, adjusting the TCP window size can significantly impact performance, particularly for high-bandwidth environments.

For example, let’s use the following command to check and increase the window size:

$ sudo sysctl net.ipv4.tcp_rmem="4096 87380 16777216"
net.ipv4.tcp_wmem = 4096 87380 16777216

This command boosts performance, especially in high-latency, high-bandwidth environments

5. Conclusion

In this article, we’ve explored how to increase the number of concurrent TCP/IP connections on a Linux system by adjusting the client-side and server-side settings. However, after making these changes, monitoring system performance closely is essential to ensure the system can handle the increase in concurrent connections without introducing new bottlenecks or stability issues.