How to Measure p99 Latency & Find Slowest Requests
In this article
This guide shows you how to measure p99 latency (the 99th percentile response time) and identify the specific requests that are causing your slowest experiences. By the end, you'll have a repeatable method to pinpoint performance bottlenecks in any HTTP-based service, using common tools like logs, databases, or simple scripts.
What You'll Need
- Access to request logs (e.g., NGINX access logs, application logs) or a database that stores request timings.
- A way to query or process the logs: command-line tools (awk, sort, jq), a scripting language (Python, Node.js), or a SQL database.
- Basic familiarity with the command line and your log format.
- Optional: an APM tool (like Datadog, New Relic) if you prefer a GUI, but the steps below work without one.
Step-by-Step
- Collect request timings. Ensure your logs record the time each request took to complete, typically in milliseconds. If you use NGINX, the
$request_timevariable is standard. For application logs, you may need to add a timer middleware. If you have no logs, instrument your code to log the duration for each request. - Extract the duration field. Parse your logs to pull out just the duration value. For example, with NGINX default log format, you can use awk to get the last field:
awk '{print $NF}' access.log. If you have JSON logs, usejq '.duration'. For a database, run a query to select the duration column. - Sort the durations. Pipe the extracted values into
sort -nto sort them numerically. This is essential for percentile calculation. - Count the total number of requests. Use
wc -lon the extracted list to get the count. Let's call this N. - Compute the index for p99. The p99 value is the duration at the 99th percentile. The index is
ceil(0.99 * N). For example, if you have 1000 requests, the index is 990 (since 0.99*1000=990). If you have 1001, it's 991. - Find the value at that index. Use
sed -n 'INDEXp'on the sorted list to get the duration at that position. For instance,sed -n '990p'gives the 990th value. That number is your p99 latency. - Identify the slowest requests. To find the actual slowest requests (e.g., the top 1%), you can sort the full log lines by duration descending. For NGINX, use:
awk '{print $NF, $0}' access.log | sort -rn | head -n 10to see the top 10 slowest full lines. Or, if you have a database,SELECT * FROM requests ORDER BY duration DESC LIMIT 10;. - Automate with a script. For regular monitoring, write a short script (Python or Bash) that reads the log file, computes p99, and prints the slowest requests. This makes the process repeatable.
Common Mistakes
- Forgetting to sort: Percentiles require sorted data. If you skip sorting, you'll get a random value.
- Using the wrong index: Some definitions use
floor(0.99*N)orround(0.99*N). The difference is negligible for large N, but be consistent. Usingceilensures you capture the 99th percentile boundary. - Including non-request lines: Logs often contain errors, health checks, or other noise. Filter them out before calculating, or your p99 will be skewed.
- Mixing time units: If some logs are in milliseconds and others in seconds, your p99 will be wrong. Normalize all durations to the same unit first.
- Looking only at p99: p99 hides the worst outliers. Always also check the maximum latency and the top 10 slowest requests to understand the tail.
Tips
- Use a percentile library: In Python, the
statisticsmodule doesn't have percentile, but you can usenumpy.percentile(data, 99)or write a simple function. - Monitor p99 over time: A single p99 value is useful, but tracking it over time (e.g., per minute) reveals trends and regressions.
- Group by endpoint: Calculate p99 separately for each API endpoint. This helps you find which specific service is slow.
- Use a log aggregation tool: If you have ELK or Splunk, you can use their built-in percentile functions (e.g.,
percentile(duration, 99)in Elasticsearch). - Consider p99 vs. p99.9: For critical systems, p99.9 might be more relevant. The same method works with 0.999.
Troubleshooting
- My p99 value seems too high or too low. Double-check your sorting and index calculation. Also, verify that your log duration field is correctly parsed. Print a few sample lines to confirm.
- I get an error like 'sed: -e expression #1, char 1: unknown command'. This usually means you didn't pipe the sorted list into sed correctly. Ensure you use
sort -n | sed -n 'INDEXp'. - My logs are huge and the command is slow. For millions of lines, consider using a more efficient tool like
datamash(e.g.,datamash -s -g1 perc:99 1) or a database. Alternatively, sample a subset of logs if you only need an estimate. - I don't have a duration field in my logs. You need to add instrumentation to your application. For NGINX, ensure the log format includes
$request_time. For custom apps, add a middleware that records the time taken.
Frequently Asked Questions
What exactly is p99 latency?
p99 latency is the response time below which 99% of your requests fall. In other words, only 1% of requests are slower than this value. It's a common metric for measuring the tail end of performance.
Why not just use average latency?
Average latency can be misleading because a few very slow requests can skew it. p99 gives you a better picture of the worst-case experience that most users won't see, but that still affects a small percentage.
How many requests do I need to calculate p99 accurately?
For a stable estimate, you need at least a few hundred requests. The more you have, the more reliable the percentile. With fewer than 100, p99 might not be very meaningful.
Can I calculate p99 from my database directly?
Yes, if you store request durations in a table, you can use SQL. For example: SELECT duration FROM requests ORDER BY duration LIMIT 1 OFFSET (SELECT COUNT(*) FROM requests) * 0.99; (adjust for your SQL dialect).
What is the difference between p99 and p99.9?
p99.9 is the 99.9th percentile, meaning only 0.1% of requests are slower. It's even more sensitive to extreme outliers and is often used for very critical services where even rare slow responses are unacceptable.
Should I include all HTTP status codes in the calculation?
It depends. If you want to measure user experience, include 2xx and 3xx responses. Exclude 4xx and 5xx errors because they often have different response times and may distort the picture. Or, calculate separately for errors.