Networking
Server Latency Explained: How to Measure It and How to Reduce It
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيLatency is the time it takes for data to travel from one point to another and, in most practical discussions, back again. When people say a website "feels slow" even though the server has plenty of CPU and RAM, latency is often the reason. It is also the one performance factor you cannot fix by buying a bigger server, because a large part of it is set by physics and geography. This guide explains where latency comes from, how to measure it properly, and what you can actually do to reduce it.
What latency really means
Latency is usually expressed as round-trip time (RTT): the milliseconds between sending a small packet and receiving the reply. It is different from bandwidth. Bandwidth is how much data can flow per second; latency is how long each trip takes. A wide highway with a long distance still means a long drive.
Total delay seen by a user is a sum of several parts:
| Component | What causes it | Can you change it? |
|---|---|---|
| Propagation delay | Distance; light in fiber covers roughly 200 km per millisecond | Only by moving the server closer |
| Routing and hops | Each router and peering point along the path | Partly, by choosing a well-connected location |
| Queuing delay | Congested links and full buffers | Sometimes, by avoiding saturated links |
| Server processing | Application code, database queries, disk I/O | Yes, fully under your control |
| Protocol round trips | TCP handshake, TLS handshake, redirects | Yes, with configuration |
The first three belong to the network. The last two belong to you, and they are frequently larger than people expect.
How to measure latency
ping: the quick check
ping -c 20 your-server-ipLook at the summary line: min/avg/max/mdev. The average tells you the typical RTT, and mdev (deviation) tells you how stable it is. A path with a steady 60 ms is often better for interactive work than one that swings between 30 and 200 ms. Note that some networks deprioritize or block ICMP, so ping is a hint, not the final word.
mtr: latency per hop
sudo apt install mtr-tiny
mtr -rwc 50 your-server-ipmtr combines ping and traceroute and shows each hop with its loss and latency. If latency jumps sharply at one hop and stays high for all hops after it, that segment is where the delay is added. A single hop with high latency that does not carry forward is usually just a router that answers probes slowly, not a real problem. Hop-by-hop reading is also the main tool for diagnosing packet loss.
curl: what the application user feels
Network RTT is only part of the story. This command breaks down a real HTTPS request:
curl -o /dev/null -s -w "dns: %{time_namelookup}\nconnect: %{time_connect}\ntls: %{time_appconnect}\nttfb: %{time_starttransfer}\ntotal: %{time_total}\n" https://example.com/connectminusdnsis roughly one network round trip (the TCP handshake).tlsminusconnectis the TLS handshake.ttfbminustlsis mostly server processing time.
If TTFB is 900 ms while connect time is 40 ms, the network is not your problem; the application is.
Measure from where your users are
Testing from the server itself or from the same data center hides the real picture. Test from the cities your visitors live in, from home connections and mobile networks if possible, and at different times of day.
How to reduce latency
1. Put the server close to your audience
This is the biggest single lever. If most of your users are in Egypt and the Middle East, a European location such as Falkenstein, Nuremberg or Helsinki is usually much closer in network terms than the US West Coast. For audiences in Southeast Asia, Singapore makes sense; for North America, Ashburn or Hillsboro. Before you commit, test the RTT from your users' region to each location. You can compare plans and regions on the pricing page.
2. Cut the number of round trips
Each round trip costs a full RTT. Practical wins:
- Enable HTTP/2 or HTTP/3 so many requests share one connection.
- Use TLS 1.3, which needs one fewer round trip than TLS 1.2 for a new connection.
- Keep connections alive (
keepalive_timeoutin Nginx) instead of reopening them. - Remove redirect chains such as
http://tohttps://towww.
A basic Nginx snippet:
listen 443 ssl;
http2 on;
ssl_protocols TLSv1.2 TLSv1.3;
keepalive_timeout 65;(On Nginx versions older than 1.25.1, use listen 443 ssl http2; instead of the separate http2 on; directive.)
3. Make the server answer faster
Server processing time is pure latency added to every request. Cache rendered pages, use an object cache such as Redis for repeated queries, enable PHP OPcache, and add database indexes for slow queries. Check that the server is not swapping: memory pressure turns microsecond memory reads into millisecond disk reads.
4. Use a CDN for static content
A CDN serves images, CSS and JavaScript from locations near the user, so those files do not cross continents. Dynamic requests still reach your origin, so steps 1 to 3 still matter.
5. Tune the TCP stack when it fits
On modern Linux kernels, enabling the BBR congestion control algorithm can help on long or slightly lossy paths:
echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.conf
sudo sysctl -pThis mostly improves throughput under real conditions rather than the base RTT, so measure before and after.
FAQ
What is a good latency for a website?
There is no universal number, because it depends on distance. As a rough guide, a few tens of milliseconds of RTT feels instant for browsing, and under about 100 ms is comfortable for most interactive sites. For gaming, trading or remote desktop, lower and more stable latency matters more.
Will a more powerful VPS reduce latency?
Only the server processing part. More CPU and RAM help if your application is slow to respond, but they do nothing for the distance between the user and the data center. Location and round trips usually matter more than raw power.
Why does ping look fine but my site is still slow?
Ping measures only the network round trip. The delay may be in DNS lookups, the TLS handshake, slow application code or database queries. Use curl -w timing to see which stage takes the most time.