Service headers: Retry-After, rate limits, Vary and security
Service headers carry no business data but steer the behaviour of the client, cache and browser. Retry-After in 429 and 503 responses says when to retry, Vary lists the request headers the response depends on, and HSTS, nosniff and stripped versions in Server protect the response. The X-RateLimit-* names are not a standard and come from the documentation. Security headers are checked on errors too and through the gateway.
API · Service headers
A client gets 429 and immediately retries in a loop. What is missing from the response?
An API under load returns 429 with no hint of when to retry. The client does not know how long to wait and knocks every second, making the overload worse. Elsewhere a CDN serves the Ukrainian version of a banner to English-speaking users because the response has no Vary. The bodies look right in both cases: the defect sits in service headers.
This lesson is about headers that report the state of the service, steer the cache between different clients and protect the response: what they mean, why they are easy to miss and how to check them.
How it is built
Service headers carry no business data, but they decide how the client, the browser and the cache treat the response. By purpose they fall into four groups.
Limits and retry. A 429 Too Many Requests or 503 Service Unavailable response should include Retry-After: the RFCs allow it but do not require it, so the contract sets the expectation. Per RFC 9110 it is either a number of seconds (Retry-After: 30) or an HTTP date (Retry-After: Wed, 11 Nov 2026 12:00:00 GMT). Many APIs also add informational limit headers: X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset or RateLimit-* from the IETF draft. There is no single final standard for their names and format, so a test follows the documentation of the specific API, not a "general" template.
The limit counter
Retry-After seconds the limit should recover.Cache between clients: Vary. The Vary header lists the request headers the response depends on: Vary: Accept-Language, Vary: Accept-Encoding, Vary: Origin. A shared cache (CDN, proxy) uses them as part of the key. Without Vary it stores the answer to the first request and serves it to everyone, whoever asks. The defect shows up only when a cache really sits between the client and the server, so a direct test does not see it.
Compression and downloads. The client sends Accept-Encoding: gzip, br, the server answers Content-Encoding: gzip and Vary: Accept-Encoding. For files, Content-Disposition: attachment; filename="report.pdf" asks the browser to save the file rather than display it, while Content-Length and Content-Type describe the content.
Response security.
Strict-Transport-Security(HSTS): from now on the browser visits the domain over HTTPS only;X-Content-Type-Options: nosniff: the browser does not guess the content type and trustsContent-Type;Content-Security-Policy(for HTML responses): restricts sources of scripts and resources;Cache-Control: no-storefor responses with personal data;- no
X-Powered-Byand no versions inServer: they hint at which vulnerabilities to look for.
Example: a limit that "exists" but does not work
Before. The rate-limit test only checks that 429 arrives after many requests. It is green even if Retry-After is missing or contains garbage. After: the test reads Retry-After, waits that long and proves the limit recovered.
test('429 carries Retry-After and the limit recovers', async ({ request }) => {
let res = await request.get('/v1/search?q=a');
for (let i = 0; i < 200 && res.status() !== 429; i++) {
res = await request.get('/v1/search?q=a');
}
expect(res.status()).toBe(429);
const wait = Number(res.headers()['retry-after']); // seconds form
expect(Number.isInteger(wait)).toBe(true);
expect(wait).toBeGreaterThan(0);
await new Promise((r) => setTimeout(r, wait * 1000 + 200));
expect((await request.get('/v1/search?q=a')).status()).toBe(200);
});
There are three checks in one scenario: 429 really arrives, Retry-After exists and has a sensible format, and after the wait the service answers 200 again. If the limit window in the test environment is long, shorten it through environment configuration instead of waiting for minutes. If the documentation allows the date form, the test parses that too.
Vary checked by hand:
curl -si "$BASE/v1/banners" -H "Accept-Language: uk" \
| grep -i -E "^(content-language|cache-control|vary)"
# content-language: uk
# cache-control: public, max-age=300
# (no Vary line: the CDN will serve "uk" to everyone)
Here the response depends on the language, carries a 5-minute public cache, and has no Vary: the CDN will serve "uk" to everyone. The right header would be Vary: Accept-Language. The same check is needed for compression:
curl -si "$BASE/v1/transfers?limit=100" -H "Accept-Encoding: gzip" \
-H "Authorization: Bearer $TOKEN" -o /dev/null -D - | grep -i -E "encoding|vary"
# content-encoding: gzip
# vary: Accept-Encoding
Security headers are checked on a successful response and on an error, because gateways often add them only on the "happy" path:
test('security and service headers', async ({ request }) => {
for (const path of ['/v1/health', '/v1/accounts/999999']) { // 200 and 404
const h = (await request.get(path)).headers();
expect(h['strict-transport-security']).toContain('max-age=');
expect(h['x-content-type-options']).toBe('nosniff');
expect(h['x-powered-by']).toBeUndefined();
expect(h['server'] ?? '').not.toMatch(/\d/); // no version numbers
}
});
Limits and alternatives
- Security headers are mostly set not by service code but by a gateway or reverse proxy. A service-level test that bypasses the gateway will not see them, so check the same URL users use.
Retry-Aftercan be checked only when the limit really triggers. On a shared test environment an aggressive request loop disturbs colleagues, so do it with a dedicated key or user.- The
RateLimitdraft changed between versions, so the value format may differ between services. Go by the documentation, not by memory. Content-Security-PolicyandHSTSconcern browsers. For a pure JSON API CSP makes little sense, whilenosniffand HSTS stay useful.- Without
Accept-Encodingin the request RFC 9110 treats any content coding as acceptable, so a compressed answer is not a violation, though a client that cannot decompress will not read it. A server may also skip compressing a small body, which is normal: the threshold is a configuration setting.
Common mistakes
- Checking only status 429. Without
Retry-Afterthe client retries in a loop and worsens the overload, and the test does not notice. - Treating
X-RateLimit-*as a standard. Names differ between APIs, so the expected names come from the documentation. - Skipping
Varybecause "everything works". The defect appears only with a cache between client and server, and a direct test does not show it. - Checking security headers on 200 only. On 404 and 500 they are often missing, and that is exactly where versions leak.
- Leaving
Server: nginx/1.18.0. A specific version helps pick a known vulnerability.
Check yourself
An API returns 429 without Retry-After. Why is it a defect if the status is right?
Answer
The client does not know how long to wait and retries at once or by its own heuristic, which increases the load. A proper 429 says when to retry. Without it rate limiting works as blocking, not as a managed delay.
An endpoint returns the user's language by Accept-Language, has Cache-Control: public, max-age=300 and no Vary. What goes wrong and how do you reproduce it?
Answer
A shared cache stores the answer to the first request (say, Ukrainian) and serves it to everyone who asks in the next 5 minutes. It can be reproduced only with a cache between client and server: send requests with different Accept-Language through a CDN or proxy and compare the responses. The fix is to add Vary: Accept-Language.
Retry-After, and Vary tells a cache which request headers the response depends on. For security I check HSTS, nosniff and the absence of versions in Server and X-Powered-By. Middle/Senior: For a limit I run a full scenario: the counter decreases, 429 arrives after zero with Retry-After, and after the wait the limit recovers. I take the X-RateLimit-* names from the documentation because there is no standard. A missing Vary on a publicly cached response mixes answers between users, so I check it through a real cache. I check security headers on errors too, and through the gateway users go through.