How do you optimize server performance? A practical walkthrough

Industry news

Choosing a server configuration

A server is built from CPU, memory, disk and network interface, so choosing a configuration means choosing core count, memory size, disk size and type, and network bandwidth. Server sizing is hard to standardize: you cannot simply infer the configuration needed for “a backend server that must reach 1000 TPS”, because real performance depends heavily on how the software is implemented. Even two endpoints in the same backend application can perform differently because their functions differ.

Server sizing should therefore be based on real test results. Start with a modestly configured server for tuning and testing, then use its results as the basis for choosing production hardware.

Take an order service as an example. Testing a server with 4 CPU cores, 16GB of memory, 10Mbps bandwidth and a 50GB mechanical disk showed it could handle 50 concurrent requests and 300 TPS (higher concurrency produced timeout errors). During the test, CPU usage was close to 75%, memory usage stayed below 50%, bandwidth usage stayed below 50%, and there was no disk activity other than logging.

From that we can estimate that a server with 4 CPU cores (keeping usage below 75%), 8GB of memory (usage can approach 100%) and 5Mbps bandwidth (usage can approach 100%) can carry the order endpoint at 50 concurrent requests and 300 TPS.

Reaching 200 concurrent requests and 2400 TPS would take either eight servers with 4 cores, 8GB of memory and 5Mbps bandwidth, or a single server with 32 cores, 64GB of memory and 40Mbps bandwidth. The final configuration still has to be verified by testing.

Note: tune application and database servers together, so the application tier is not over-provisioned while the database tier becomes the bottleneck. This sizing approach does not fit every scenario, so use it as a reference.

Analyzing server load

Before tuning, analyze the server's load. In practice you look at CPU usage, memory usage, disk I/O, load average and bandwidth usage.

CPU usage

CPU usage reflects how busy the processor is. At 100%, some processes start waiting and the CPU does not service them for the moment. To absorb bursts of traffic, production servers usually keep CPU usage under 75%. If a server repeatedly exceeds 75%, it is time to add capacity.

htop is a good choice for monitoring CPU usage: it shows CPU usage, memory usage and load at a glance.

Watching CPU load with htop

Install htop first, for example on CentOS:

yum install htop -y

Then run htop:

htop

htop shows CPU load per core. The operating system distributes work across cores, so the server only counts as above 75% CPU usage when every core exceeds 75%.

htop screen showing per-core CPU usage, memory usage and the process list
htop screen: per-core CPU bars on top, Mem and Swp rows for memory and swap, process list below.

Reading the CPU load

The screenshot above comes from a 4-core server. At the moment of capture, three cores were already above 75%; after watching for a while, every core hovered around 85%, which means the CPU is heavily loaded and more capacity is needed.

Memory usage

Memory usage reflects how much memory is in use. Memory holds program code and data and is usually split into physical memory and virtual memory: physical memory is the server's RAM, while virtual memory is a slice of disk. When physical memory reaches 100%, the system starts using virtual memory. Because virtual memory is far slower, any program pushed into it runs poorly.

As a rule, keep physical memory usage below 80% and virtual memory usage at 0%.

htop also shows memory: the Mem row at the top is physical memory and the Swp row is swap. In the screenshot above, the server has 16GB in total and about 10GB in use — a 62% memory utilization rate with room to spare, while swap is disabled. The MEM% column in the process list shows per-process memory usage.

Disk I/O

Disk I/O means reading from and writing to disks. Logging, file operations and database work all create disk pressure, with database operations the heaviest: under high concurrency the database is usually the first bottleneck.

iostat is a convenient tool for watching disk activity.

Watching disk I/O with iostat

Install iostat first, for example on CentOS:

yum install sysstat -y

Then check disk usage with iostat:

# Overall disk activity; 1 means refresh every second
iostat -x 1
iostat output showing %idle for the CPU and %util per disk device
iostat output: the two indicators that matter most are %idle and %util.

The screenshot above shows overall disk activity. Two indicators matter most: %idle and %util.

  1. %idle: the share of CPU idle time not spent waiting on disk I/O, which should stay above 70%.
  2. %util: the share of time the device spends on I/O, which should stay below 70%; 100% means the device is saturated.

To lower disk load, move to faster storage such as NVMe SSDs or PCIe cards, or reduce how often the disk is touched by using asynchronous and batched writes.

Load average

Load average is the average number of active processes over a period of time and a key indicator of server load. As a rule it should stay below the server's CPU core count.

htop shows load average as well; watch the Load average field, where the three numbers cover the last 1, 5 and 15 minutes.

Load average needs to stay under the core count. To leave headroom for bursts, a 4-core server should keep it at or below 75% of its cores, that is, below 3; a sustained higher value means it is time to add performance.

Network usage

Network usage matters too: insufficient bandwidth sharply increases response times. To absorb bursts of concurrency, keep bandwidth usage below 80%. Note that the physical network card caps the maximum bandwidth available to the server.

nload is a convenient tool for watching network usage.

Watching the network with nload

Install nload first, for example on CentOS:

yum install nload -y

Then run:

nload
nload screen: Incoming traffic on top, Outgoing traffic below
nload output: Incoming maps to downstream bandwidth, Outgoing to upstream bandwidth.

The screenshot shows network usage after running nload, split into incoming and outgoing traffic: incoming corresponds to downstream bandwidth and outgoing to upstream. When current speed keeps approaching maximum speed, bandwidth usage is close to 100%.

Indicator reference:

  1. Curr: current speed
  2. Avg: average speed
  3. Min: minimum speed
  4. Max: maximum speed
  5. Ttl: total traffic

Tuning kernel parameters

Strong hardware alone is not enough: kernel parameters also need tuning so the server can deliver its performance under high concurrency. Not every server needs high-concurrency tuning — it matters mainly for servers handling concurrent requests, typically front-end servers, back-end servers and database servers.

Two groups of parameters matter most: the maximum number of open files per process, and TCP settings.

Maximum open files per process

To raise the per-process open file limit, edit /etc/security/limits.conf and append these four lines:

* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535

Here * means all users and 65535 is the new limit; the change takes effect after a reboot.

TCP settings

Tuning TCP parameters improves high-concurrency communication. Edit /etc/sysctl.conf and add the following:

# Disable SYN cookies; high-concurrency systems need this off to avoid SYN flood handling
net.ipv4.tcp_syncookies = 0

# Enable TCP connection reuse so TIME-WAIT connections can be reused
net.ipv4.tcp_tw_reuse = 1

# Enable fast recycling of TIME-WAIT connections
net.ipv4.tcp_tw_recycle = 1

# Timeout in seconds for FIN-WAIT-2 when this side closes the connection
net.ipv4.tcp_fin_timeout = 30

# Interval in seconds between keepalive probes, 2 hours by default
net.ipv4.tcp_keepalive_time = 1200

# Outbound port range, which limits connections to other servers
net.ipv4.ip_local_port_range = 1024 65535

# SYN queue length, 1024 by default
net.ipv4.tcp_max_syn_backlog = 65535

# Maximum number of TIME_WAIT connections, 180000 by default
net.ipv4.tcp_max_tw_buckets = 5000

# Maximum packets queued when the NIC receives faster than the kernel processes
net.core.netdev_max_backlog = 65535

# Maximum TCP connections
net.core.somaxconn = 65535

# Default receive buffer size in bytes
net.core.rmem_default = 8388608

# Maximum receive buffer size in bytes
net.core.rmem_max = 16777216

# Default send buffer size in bytes
net.core.wmem_default = 8388608

# Maximum send buffer size in bytes
net.core.wmem_max = 16777216

# Avoid timestamp anomalies
net.ipv4.tcp_timestamps = 0

# Cap on orphaned TCP sockets not attached to a file handle, guards against simple DoS
net.ipv4.tcp_max_orphans = 3276800