introduction
In high concurrency scenarios, the network performance of Linux servers directly affects the user experience. How to optimize TCP connections, monitor network request delays, and adjust kernel parameters has become a skill that developers and operation and maintenance engineers must master. This article combines the CentOS 7 environment to comprehensively analyze the core methods of Linux network performance optimization from kernel parameter tuning, network request analysis to Java code practice.
1. Linux network parameter tuning
1. Key kernel parameters and functions
The following parameters directly affect the server's concurrent processing capabilities:
# Adjust the length of the TCP listening queue (default 128, it is recommended to increase it)sysctl -w =65535 # Keep specific ports (prevent randomly occupied)sysctl -w net.ipv4.ip_local_reserved_ports=9100 # Adjust the local port range (default 32768-60999)sysctl -w net.ipv4.ip_local_port_range="1024 61999" # Allows multiplexing of TIME-WAIT status portssysctl -w net.ipv4.tcp_tw_reuse=1 # Increase the maximum number of file descriptors in the systemsysctl -w -max=1048576
2. Permanently effective configuration
Write the parameter to /etc/ and load:
echo "=65535" >> /etc/ sysctl -p # Reload the configuration
3. Monitor the usage of parameters
parameter | View commands | Optimization goals |
---|---|---|
somaxconn |
cat /proc/sys/net/core/somaxconn |
Avoid TCP connection queue overflow |
tcp_tw_reuse |
sysctl net.ipv4.tcp_tw_reuse |
Reduce TIME-WAIT status port occupancy |
File descriptor usage | cat /proc/sys/fs/file-nr |
Prevent "Too many open files" errors |
2. Time-consuming analysis of network requests
1. The HTTP request phase takes time (curl)
curl -w "
DNS resolution: %{time_namelookup}s
TCP connection: %{time_connect}s
Server processing: %{time_starttransfer}s
Total time : %{time_total}s\n" -o /dev/null -s
Output example:
DNS resolution: 0.012s
TCP connection: 0.045s
Server processing: 0.250s
Total time : 0.251s
2. Use ab for stress testing
ab -n 1000 -c 100 /
Key indicators:
- Connect Time: TCP connection establishment time
- Processing Time: Server processing request time
3. Real-time monitoring of TCP connection status
watch -n 1 "ss -ant | awk 'NR>1 {print \$1}' | sort | uniq -c"
Output example:
ESTAB 500
TIME-WAIT 200
SYN-RECV 10
3. Java code practice: time-consuming monitoring of HTTP requests
Here is an example of measuring HTTP request time using Java:
import ; import ; import ; import ; public class HttpRequestTimer { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = (); HttpGet request = new HttpGet(""); long startTime = (); try (CloseableHttpResponse response = (request)) { long dnsTime = () - startTime; ("DNS + TCP Time: " + dnsTime + "ms"); (()); long totalTime = () - startTime; ("Total Time: " + totalTime + "ms"); } } }
Code description:
- Send HTTP requests using Apache HttpClient.
- Record DNS resolution and TCP connection time (dnsTime).
- Total Time is recorded.
4. Advanced tool chain
1. Tcpdump packet capture analysis
tcpdump -i eth0 -w 'port 80'
Use Wireshark to analyze, focus on:
- TCP three-time handshake time
- HTTP response delay
2. Monitor with Prometheus + Grafana
Configure node_exporter to collect network metrics:
# scrape_configs: - job_name: 'node' static_configs: - targets: ['localhost:9100']
Visualize in Grafana:
- Number of TCP connections
- Request Delay Distribution
5. Frequently Asked Questions and Solutions
Question 1: Too much TIME-WAIT status
Phenomenon:
ss -ant | grep -c 'TIME-WAIT' # Return number >10000
solve:
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
Issue 2: SYN Flood Attack
Phenomenon:
ss -ant | grep -c 'SYN-RECV' # Extremely high
solve:
echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog echo 1 > /proc/sys/net/ipv4/tcp_syncookies
Conclusion
Through kernel parameter tuning, time-consuming request analysis and Java code monitoring, the network performance of Linux servers can be significantly improved. It is recommended to observe key indicators in combination with monitoring tools (such as Prometheus) for a long time and adjust parameters dynamically based on actual business scenarios.
This is the article about the detailed guide to network performance optimization and monitoring in Linux. For more related content on Linux network performance optimization and monitoring, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!