Do you know how many connections a Linux server can handle?

Do you know how many connections a Linux server can handle?

Preface

First, let's see how to identify a TCP connection? The system is identified by a four-tuple, (src_ip, src_port, dst_ip, dst_port), namely source IP, source port, destination IP, and destination port. For example, we have a service at 192.168.0.1 with port 80 open. Then all clients will connect to port 80 of this service. There is a misunderstanding that we often say that a machine has 65536 ports, so the number of connections it carries is 65536. This statement is extremely wrong, which confuses the source port and the access target port. When we do stress testing, we use the stress testing client. The number of connections of this client is limited by the number of ports, but the number of connections on the server can reach tens of thousands, generally up to one million (4C8G configuration). As for the upper limit, it depends on the degree of optimization. The specific steps are as follows:

We are stress testing a target server and want to see the number of connections under load. When we stress it to a certain number, the console suddenly reports "too many open files". This is because when the Linux system creates a TCP connection, it creates a socket handle, and each socket handle is a file handle. The operating system has a limit on the number of open file handles. One of the basic philosophies of Unix/Linux is "everything is a file". To increase TCP carrying capacity, you need to adjust the file handle.

Step 1: Modify the file handle limit

# View the maximum number of file handles that the current user is allowed to open with TCP ulimit -n

# Modify file handle vim /etc/security/limits.conf

*soft nofile 655350
* hard nofile 655350

After the modification, exit the terminal window and log in again (no need to restart the server), and you can see the latest results. This is the first step in optimization, modifying the file handle limit.

Notice:
Soft nofile (soft limit) means that Linux further limits the number of files that users can open simultaneously within the range that the current system can bear.
hard nofile (hard limit) is the maximum number of files that can be opened simultaneously by the system, calculated based on the system hardware resources (mainly system memory). Usually the soft limit is less than or equal to the hard limit.

Step 2: TCP parameter tuning

parameter Default Configuration Adjust configuration illustrate
fs.file-max 1048576 9999999 The number of file descriptors opened by all processes
fs.nr_open 1635590 1635590 The maximum number of files that can be allocated by a single process
net.core.rmem_default 124928 262144 Default TCP read buffer
net.core.wmem_default 124928 262144 Default TCP send buffer
net.core.rmem_max 124928 8388608 Default TCP maximum read buffer
net.core.wmem_max 124928 8388608 Default TCP maximum send buffer
net.ipv4.tcp_wmem 4096 16384 4194304 4096 16384 8388608 TCP send buffer
net.ipv4.tcp_rmem 4096 87380 4194304 4096 87380 8388608 TCP Read Buffer
net.ipv4.tcp_mem 384657 512877 769314 384657 512877 3057792 TCP memory size
net.core.netdev_max_backlog 1000 5000 The maximum number of packets allowed to be sent to the queue when the rate at which packets are received on each network interface is faster than the rate at which the kernel can process them.
net.core.optmem_max 20480 81920 The maximum buffer size allowed for each socket
net.core.somaxconn 128 2048 The maximum listening queue length for each port. This is a global parameter.
net.ipv4.tcp_fin_timeout 60 30 The time (in seconds) that TCP remains in the FIN-WAIT-2 state for a socket connection that is disconnected by the local end. The other party may disconnect or never end the connection or the process may die unexpectedly.
net.core.netdev_max_backlog 1000 10000 The maximum number of packets allowed to be sent to the queue when the rate at which packets are received on each network interface is faster than the rate at which the kernel can process them.
net.ipv4.tcp_max_syn_backlog 1024 2048 The maximum number of connection requests that can be stored in the queue but have not yet been confirmed by the other party. If your server is frequently overloaded, try increasing this number.
net.ipv4.tcp_max_tw_buckets 5000 5000 The maximum number of timewait sockets that the system can handle at the same time
net.ipv4.tcp_tw_reuse 0 1 Whether to allow TIME-WAIT sockets to be reused for new TCP connections
net.ipv4.tcp_keepalive_time 7200 900 Indicates the number of seconds after which a TCP link starts to send a probe message (send an empty message) when there is no data message transmission.
net.ipv4.tcp_keepalive_intvl 75 30 Indicates the time interval between the previous detection message and the next detection message
net.ipv4.tcp_keepalive_probes 9 3 Indicates the number of detections

From the above configuration parameters, we can know that buffer queues are set up for TCP sending and receiving in the Linux kernel, which can improve the system throughput.

All of the above parameters are defined in the /etc/sysctl.conf file. Some parameters may not be defined in the file. The system gives default values. If you need to modify them, add or modify them directly in the file, and then execute the sysctl -p command to make them take effect.

Notice:
The larger the parameter value, the better. Some parameters need to be considered based on the server's hardware configuration and the impact of the parameters on other services on the server.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Linux load balancing summary of the difference between layer 4 load balancing and layer 7 load balancing
  • How to build nginx load balancing under Linux
  • Detailed explanation of Linux system configuration nginx load balancing
  • Detailed explanation of Linux dual network card binding to achieve load balancing
  • How to implement simple load balancing using DNS on Linux
  • Detailed explanation of Linux CPU load and CPU utilization

<<:  Basic usage examples of Vue named slots

>>:  8 ways to manually and automatically backup your MySQL database

Recommend

Example of how to identify the user using a linux Bash script

It is often necessary to run commands with sudo i...

How to change the dot in the WeChat applet swiper-dot into a slider

Table of contents background Target Effect Ideas ...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

How to create a simple column chart using Flex layout in css

The following is a bar chart using Flex layout: H...

Description of the hr tag in various browsers

Generally, we rarely meet HR, but once we do, it c...

Example of how to configure the MySQL database timeout setting

Table of contents Preface 1. JDBC timeout setting...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

React implements import and export of Excel files

Table of contents Presentation Layer Business Lay...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

Summary of methods to check whether the port is open in Linux

Method 1: Use lsof command We can use the lsof co...

Docker case analysis: Building a Redis service

Table of contents 1 Create mount directories and ...