A 167ms average latency can hide a two-second p95
A k6 routine you can run on a laptop sets pass/fail thresholds, finds where p95 starts to bend and adds caching without leaking data between users.
In brief
- Set targets in p95 and requests per second, never in average latency.
- A breached k6 threshold returns a non-zero exit code, so a load test can serve as a CI/CD gate.
- Caching is the first lever to pull, but personalized responses must be marked private.
Of 100 requests to an API, 94 return in 50ms and six take 2,000ms. The average is 167ms, under a 200ms limit, and the report sent to the client will say “pass”. Yet the p95 of those same numbers is 2,000ms: for every 100 users, six wait two seconds.
Google’s SRE book calls it wishful thinking to believe that most requests sit close to the average. For an FDE, this stops being theory in the week before go-live. The client needs to know whether the system will survive launch day, and you are the one who has to answer with data.
This guide walks through a process you can run on a laptop. You will write a load test script with pass/fail thresholds, run it as a gate in the pipeline, raise the load until p95 starts to bend, then add caching without leaking data between users. The load targets, URLs and test results used along the way are hypothetical.
What do you need before typing a command?
You need k6 installed, a staging environment for the API (do not fire load at the client’s production system), access to staging dashboards for CPU, memory and the database, and curl.
If the client’s team is more comfortable with Postman, it can also run virtual users in parallel through a sequence of requests and set pass conditions on the average, p90, p95 or p99. The reasoning below applies to both tools.
A short conversation with the client matters more than the tool. You need two numbers: the target load and the acceptable latency.
Step 1: agree targets in requests per second and p95
Grafana Labs’ API load testing guide notes that load is usually reported as a request rate, per second or per minute. So instead of asking the client how many users they have, ask how many requests per second this endpoint receives at peak. A user count has to be converted; requests per second can be measured directly.
For latency, Grafana’s standard template is that 95% of requests must finish in under 200ms. Write your target in that form.
If a frontend calls your API alongside many other backends, set a tighter limit. Google’s SRE book warns that one backend’s p99 can easily become the median latency at the frontend, because a page that calls many backends will regularly hit at least one slow request.
Check after this step: you have a line that can go into the test plan, such as “endpoint /orders, 500 requests per second, p95 under 200ms, errors under 1%”, and the client has agreed to it.
Step 2: turn the targets into k6 thresholds
k6 expresses pass/fail criteria as thresholds. Its documentation describes a threshold that evaluates the HTTP error rate through the http_req_failed metric, and an abortOnFail property that, when set to true, stops the test as soon as the limit is crossed.
The script below is a simplified version. It uses a fixed number of virtual users (VUs) for readability, and you should check the syntax against the documentation for the k6 version you run.
// load.js — simplified sketch
import http from 'k6/http';
export const options = {
vus: 20,
duration: '2m',
thresholds: {
http_req_duration: [{ threshold: 'p(95)<200', abortOnFail: true }],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
http.get('https://staging.example.com/api/orders');
}
abortOnFail earns its place when a system is plainly broken: there is no need to wait out the full two minutes to learn that p95 has jumped to several seconds. Setting an error threshold alongside the latency one matters just as much, because an API that returns 500 errors very quickly still has a handsome p95.
VUs and requests per second are different things
Step 1 set the target in requests per second, while the script declares 20 VUs. A simple calculation connects them. Each VU runs the loop continuously, so throughput is roughly the number of VUs divided by the time one iteration takes. With 20 VUs and 50ms per request, you get about 400 requests per second in theory.
The trap lies in the other direction. When the system slows and each request takes 200ms, the same 20 VUs generate only about 100 requests per second. The test eases off just when the system is weakest, and p95 looks kinder than it really is.
Use the conversion only to estimate a starting number of VUs. After each run, compare the requests per second you achieved with the target in the test plan. If you fell short, add VUs and run again until you reach it.
Step 3: run it and let the exit code decide
k6 run load.js
echo $?
Grafana Labs stresses that when a test fails, the k6 CLI returns a non-zero exit code, which is a requirement for automation. You can place this command in the CI/CD pipeline before the deploy step: if a threshold breaks, the pipeline goes red and the build does not ship.
Check after this step: deliberately lower the threshold to p(95)<1 and run it again. If echo $? still prints 0, your gate is not blocking anything.
Step 4: raise the load in steps, then spike it
A single run at a fixed load tells you only that the system is fine at one level. Go-live day usually looks different: load arrives suddenly when the client emails an announcement or opens up a feature. Grafana classifies tests by purpose, and a spike test shows how a system reacts to a sudden, large rise in traffic.
The simplest approach needs no new configuration: edit vus in load.js and rerun at each step, recording the requests per second achieved, p95 and error rate. This is a manual ramp-up, simplified so it is easy to follow. The table below shows the shape of the curve to look for on a single endpoint.
| Load step | Requests per second achieved | p95 | Error rate |
|---|---|---|---|
| 10 VUs | 180 | 60ms | 0% |
| 20 VUs | 350 | 75ms | 0% |
| 40 VUs | 600 | 140ms | 0% |
| 60 VUs | 640 | 380ms | 0.2% |
| 80 VUs | 610 | 900ms | 3% |
Read the table through the four golden signals proposed by Google SRE: latency, traffic, errors and saturation. Between 40 and 60 VUs, traffic barely moves, staying within 600–640 requests per second, while p95 nearly triples and errors only edge up. The SRE book observes that rising latency is often an early indicator of saturation, and this is what it looks like.
The endpoint’s real ceiling is about 600–640 requests per second, well before errors spill out at 80 VUs. When p95 starts to bend, open the staging dashboard and find which resource is running out: CPU, the database connection pool or a queue. k6 gives you only the first three signals.
For the spike, hold a base load at the 10 VU step, then jump straight to several times the peak the client expects instead of climbing gradually. The question to answer is whether the system returns errors en masse or merely slows down and recovers.
Step 5: add caching and run it again
When p95 breaks its threshold on a read endpoint, caching is usually the first lever. Nordic APIs gives a simple reason: caching responses avoids unnecessary database queries. On a client project, though, caching comes with a data trap.
MDN is explicit: if a response contains personalized content and you want it stored only in a private cache, you must specify the private directive. Without it, a shared cache such as a CDN or proxy can return one person’s orders to another. Check the current headers:
curl -I https://staging.example.com/api/orders
The second layer is revalidation with ETag. According to MDN, the server returns 304 Not Modified when the ETag it computes for a resource matches the If-None-Match value in the request, so it does not have to resend the payload. Try it by copying the ETag from the command above:
curl -I -H 'If-None-Match: "<etag-just-received>"' https://staging.example.com/api/orders
Check after this step: personalized responses carry private, requests with If-None-Match receive a 304, and rerunning the load steps from Step 4 shows the bend in p95 moving to the right. A k6 script that calls one URL repeatedly will have a higher cache hit rate than real traffic, so vary the parameters if you want honest numbers.
Common mistakes
The most common mistake is reporting average latency, as in the 167ms case at the top. The second is setting a latency threshold and forgetting the error rate. The third is reporting the number of VUs in place of the requests per second achieved, when fixed VUs ease off by themselves as the system slows.
The fourth is running a load test against a shared environment without telling the client’s team, leaving others to think the system is down. The fifth is measuring after the cache is on without keeping the earlier results table, which makes it impossible to show how much things improved.
How do you take this skill to a client?
Say the client’s CTO asks: “If we open this to every branch tomorrow, will it hold?” The most convincing answer you can give is a test plan in requests per second, a script that lives in the pipeline, and a table of p95 at each load step before and after the cache.
If you are moving into an FDE role, look for phrases such as “production readiness”, “performance” or “go-live support” in job descriptions. On your CV, replace “experienced in load testing” with a specific line: “set p95 and error-rate thresholds in k6 as a CI gate before deploy, found the load ceiling through ramp-up and spike tests, reduced p95 with Cache-Control and ETag”.
On launch day, the client will not ask you for the average. They will only notice whether anyone has to wait.
Was this article useful?
Thanks for the feedback!
6 sources
- API load testing: A beginner's guide (Grafana Labs) · 2024-01-31
- Thresholds | Grafana k6 documentation
- Monitoring Distributed Systems (Google SRE Book)
- Simulate user traffic to test your API performance (Postman Docs)
- 10 Tips for Improving API Performance (Nordic APIs) · 2023-11-08
- HTTP caching (MDN Web Docs)