You tap “Search” in a shopping app. A moment later, results appear. That moment, from the tap to the results, is latency. If it’s 100 milliseconds, the app feels instant. If it’s 3 seconds, you start wondering if it’s broken, and many people give up.
Latency is one of the two numbers engineers watch most in any system (the other is throughput). This guide explains what latency is, where it comes from, and why the average is the wrong way to measure it.
What is latency?
Latency is the time it takes for one request to get its answer. It’s measured in milliseconds (ms); one millisecond is a thousandth of a second.
People use a few close words:
- Latency: strictly, the delay before something starts arriving. In everyday system design talk, it usually means the full time for a request.
- Response time: the full time the user waits, from sending to receiving the whole answer.
- Round-trip time (RTT): how long a tiny message takes to go to a server and back, with no real work done. It’s the network’s part of latency.
In this guide, “latency” means the time a user waits for one request.
Where does the time go?
A single request’s latency is a sum of many small delays:
- Network travel. The request crosses the internet to the server, and the answer comes back. Distance matters: light in fibre covers roughly 200 km per millisecond, so a server on another continent adds tens to hundreds of milliseconds per round trip.
- Connection setup. Opening a new secure (HTTPS) connection takes extra round trips before any data is sent. Reusing connections avoids this.
- Waiting in a queue. If the server is busy, the request waits its turn. This is often the biggest and most unpredictable part.
- Doing the work. Running the code, checking permissions, building the answer.
- Calling other things. Each database query, cache lookup or call to another service adds its own latency.
Why averages lie: p50, p95 and p99
Say 99 requests take 50 ms and one takes 5,000 ms. The average is about 100 ms. Sounds fine. But one user in a hundred waited five seconds, and the average hides them.
So engineers use percentiles:
- p50 (the median): half of requests are faster than this. The typical experience.
- p95: 95% are faster; 1 in 20 is slower.
- p99: 99% are faster; 1 in 100 is slower. This is the tail.
Why care about 1 in 100? Because:
- Busy users hit the tail often. If loading one page makes 50 requests, a 1-in-100 slow request will show up on a large share of page loads.
- Your best customers do the most. The people who use your app the most make the most requests, so they meet slow ones most often.
- The tail is where trouble starts. Slow requests hold onto threads and connections, which can make everything else queue up.
A typical goal sounds like: “p99 latency under 300 ms for the search API.” That’s the kind of target that belongs in your non-functional requirements.
Latency vs throughput
They’re related but different:
- Latency is how long one request takes.
- Throughput is how many requests you finish per second.
Think of a highway. Latency is how long your drive takes. Throughput is how many cars pass per hour. A wider road raises throughput but doesn’t make your trip shorter. And when the road gets close to full, traffic jams form and everyone’s trip gets longer: high load raises latency. That’s why the two are watched together.
How to reduce latency
- Move closer to users. Serve static content from a CDN, and run servers in regions near your users.
- Cache. A memory lookup takes a fraction of a millisecond; a database query might take tens. See caching strategies.
- Make fewer round trips. Combine many small calls into one. Reuse connections instead of opening new ones.
- Do things in parallel. If you need data from three services, ask all three at once instead of one after another.
- Move slow work out of the way. Send emails or generate reports in the background with a message queue, and answer the user right away.
- Fix the slowest step. Find the bottleneck with tracing (see observability) and speed that up first, often a missing database index.
- Set timeouts. Don’t let one slow dependency hold a request forever. See resilience patterns.
Common misconceptions
“Average latency tells me how fast we are.” It hides the slow requests users complain about. Use p50 for the typical case and p95/p99 for the bad ones.
“More bandwidth means lower latency.” Bandwidth is how much data fits through per second. For small requests, the delay is mostly distance and waiting, which more bandwidth doesn’t fix.
“Latency is a network problem.” Network is one part. Queues, slow queries and chains of service calls are often bigger.
Key takeaways
- Latency is how long one request takes, usually in milliseconds.
- It’s the sum of network travel, connection setup, queueing, work and calls to other services.
- Measure with percentiles: p50 for typical, p95 and p99 for the slow tail.
- Latency and throughput are different, but high load raises latency.
- Cut it with CDNs, caching, fewer round trips, parallel calls, background work and fixing the bottleneck.
Frequently asked questions
What is latency in simple words?
Latency is the delay between asking for something and getting it. In apps, it’s how long you wait after a tap or click before the answer appears.
What does p99 latency mean?
p99 latency is the time that 99% of requests finish within. Only 1 in 100 requests is slower than the p99 value. It’s used to track the slow experiences that averages hide.
What is a good latency for a website or API?
It depends on the product, but many teams aim for API responses under a few hundred milliseconds at p99. Research on user interfaces often quotes about 100 ms as the point where a response feels instant.
What is the difference between latency and throughput?
Latency is how long one request takes. Throughput is how many requests a system handles per second. A system can have high throughput and still high latency, for example if every request waits in a long queue.
What causes high latency?
Common causes are long network distances, busy servers with requests waiting in queues, slow database queries, too many calls between services, and no caching.
Comments