Linux Kernel Tuning
In order to support larger concurrency, optimizing the Linux kernel is also of utmost importance in addition to having to install the event extension. Each of the following optimizations is very, very important, so please make sure to complete them one by one.
Parameter Explanation:
max-file: Indicates the system-level limit on the number of open file handles. This applies to the entire OS, not just the user.
ulimit -n: Indicates the control of the process-level limit on the number of open file handles. It controls the available file handles for the current user's current shell and the processes it starts.
To check the system-level limit on the number of open file handles: cat /proc/sys/fs/file-max
Open the file /etc/sysctl.conf and add the following settings:
# This parameter sets the number of TIME_WAIT sockets; if it exceeds the default value, it will be cleared immediately
net.ipv4.tcp_max_tw_buckets = 20000
# Defines the maximum length of the listening queue for each port in the system; this is a global parameter
net.core.somaxconn = 65535
# The maximum number of connection requests that can be held in a queue without confirmation from the other side
net.ipv4.tcp_max_syn_backlog = 262144
# When the rate of incoming packets at each network interface exceeds the rate at which the kernel processes these packets, this option allows a maximum number of packets to be queued
net.core.netdev_max_backlog = 30000
# This option can lead to timeouts for clients in a NAT network, it is recommended to set to 0. Linux removed the tcp_tw_recycle configuration starting from kernel 4.12; ignore if it shows "No such file or directory"
net.ipv4.tcp_tw_recycle = 0
# Total number of files that all processes in the system can open
fs.file-max = 6815744
# Size of the firewall tracking table. Note: If the firewall is not enabled, it will prompt with error: "net.netfilter.nf_conntrack_max" is an unknown key, which can be ignored
net.netfilter.nf_conntrack_max = 2621440
net.ipv4.ip_local_port_range = 10240 65000
Run sysctl -p to make the changes take effect immediately.
Note:
There are many options that can be set in /etc/sysctl.conf; other options can be modified according to your environment needs.
Number of Open Files
Set the system open files limit to address the too many open files issue under high concurrency. This option directly affects the number of client connections a single process can accommodate.
Soft open files is a Linux system parameter that impacts the maximum number of file handles a single process can open; this value affects applications with long connections, such as chat applications, in terms of the number of user connections a single process can maintain. Running ulimit -n allows you to see this parameter value, which if it shows 1024, means that a single process can maintain at most 1024 client connections simultaneously, or even less since other file handles may also be open. If four processes are started to maintain user connections, then the total number of connections that the entire application can maintain will not exceed 4*1024, meaning it can support at most 4 x 1024 users online. This setting can be increased to allow the service to maintain more TCP connections.
Three Methods to Modify Soft Open Files:
First Method: Run ulimit -HSn 102400 directly in the terminal and then restart Workerman.
This method only takes effect in the current terminal, and once you log out, the open files will revert to the default value.
Second Method: Add ulimit -HSn 102400 to the end of the /etc/profile file so that it executes automatically every time you log in to the terminal. The changes will take effect after restarting Workerman.
Third Method: To make the modification to the open files value permanent, you must change the configuration file: /etc/security/limits.conf. Add the following lines to this file:
* soft nofile 1024000
* hard nofile 1024000
root soft nofile 1024000
root hard nofile 1024000
This method requires a server restart to take effect.