Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Design the standard for cross-cutting HTTP headers across services: correlation ids, idempotency keys, caching and rate limit headers.

I would write one short HTTP header standard that fixes names and formats: W3C traceparent for tracing plus one correlation header, Idempotency-Key for retriable POST operations backed by server-side storage, Cache-Control and ETag rules for read endpoints, and one rate limit format set only at the gateway. Then I would implement the service-side behaviour once, in a shared Spring Boot starter with its own tests, so every service gets it by adding a dependency.

Background to this question

Headers are where services agree on things that are not part of any single API: how to trace a request across ten services, how to retry safely, how long a response may be cached, how close a client is to its rate limit. When each team invents its own names, logs cannot be joined, retries create duplicate payments and clients need special code per service. A senior engineer is expected to make these decisions once and make them easy to follow. In this answer, we will go through each kind of header, decide its name, format and owner, and then see how a shared starter turns the standard into code.

1Why a Header Standard Is Worth Writing

Cross-cutting headers are headers whose meaning is the same on every endpoint, as opposed to headers specific to one API. The cost of inconsistency shows up later and far away: an incident where the payment service logs X-Correlation-ID, the order service logs X-Request-Id and the gateway logs neither, so nobody can follow one request. A standard of two or three pages, owned by the platform or architecture group, prevents this. For each header it says the name, the format, who sets it, who forwards it, and what a service does when it is missing.

The header standard at a glance
ConcernHeaderSet byService behaviour
Tracingtraceparent, tracestatefirst service or gatewaypropagate, log trace id
CorrelationX-Correlation-Idgateway if absentlog in MDC, forward, echo back
Retry safetyIdempotency-Keyclientstore result per key, replay
CachingCache-Control, ETag, Varyservicerules per endpoint type
Rate limitsRateLimit, Retry-Aftergateway onlyservices never set them

2Correlation Ids and Trace Context

For distributed tracing, the W3C Trace Context standard defines the traceparent header, which carries a trace id, a span id and flags, for example traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01. Micrometer Tracing in Spring Boot reads and writes it automatically for incoming requests, RestClient, WebClient and Kafka, so there is no reason to invent a custom trace header. The standard should simply say: propagate traceparent, never strip it at the gateway, and put the trace id in every log line.

Many companies still want a separate business correlation id, a value a support person can read from a customer's error page and search for, which may span several traces such as a retry the next day. If we keep one, we name it once, X-Correlation-Id, fix its format, for example a UUID, have the gateway create it when the client does not send one, and echo it in every response including errors.

3Idempotency Keys for Retried POST Requests

A POST that creates a payment is not idempotent: sending it twice charges twice. Yet clients must retry when a timeout hides whether the first call succeeded. The standard answer is an Idempotency-Key header: the client generates a unique key, such as a UUID, for each logical operation and sends the same key on every retry.

The server stores, per key, a hash of the request and the response it produced, for a fixed retention period such as 24 hours. On a repeat with the same key and the same request, it replays the stored response instead of running the operation again. On a repeat with the same key but a different body, it answers 422, because the client reused a key by mistake. While the first call is still running, a repeat gets 409, so two copies never run at once.

Java
POST /payments
Idempotency-Key: 5d0f6a7e-2b1c-4f7e-9a51-1f0c2e9b7c11
{"orderId": 42, "amount": "19.99"}
-- first call -> 201 Created, stored under the key with the request hash
-- retry, same body -> 201 Created, replayed from storage, no second charge
-- same key, new body -> 422 Unprocessable Content
-- retry while first still running -> 409 Conflict

The storage has to be shared by all instances of the service, for example a database table with a unique constraint on the key, so the guarantee holds behind a load balancer. The standard says which POST endpoints require the header: anything with external side effects, such as payments, orders and emails.

4Caching Rules for Read Endpoints

Without a rule, some services return no caching headers and proxies guess, while others cache personal data by accident. The standard should define a few profiles. Personal or sensitive data gets Cache-Control: no-store. Data that changes but is worth revalidating gets Cache-Control: no-cache together with an ETag, so clients get cheap 304 responses. Public reference data, such as a list of countries, gets Cache-Control: public, max-age=3600. Any response that differs by a request header carries Vary for it, such as Vary: Accept or Vary: API-Version. Each read endpoint picks one profile in its API description, and reviewers check it.

5Rate Limit Headers at the Gateway

Rate limits are enforced in one place, the gateway, so their headers should be set there too, in one format. The IETF work on rate limit headers defines RateLimit and RateLimit-Policy fields; many gateways still emit the older X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset. The standard picks one, check what your gateway supports, and adds that a 429 Too Many Requests always carries Retry-After in seconds. Services behind the gateway never set these headers, so clients never see two conflicting sets.

6Building It Into a Shared Starter

A standard that lives only in a document drifts. So the service-side parts go into an internal Spring Boot starter: a dependency that brings auto-configuration, which Spring Boot applies when the library is on the classpath. It registers a filter that reads or creates X-Correlation-Id, puts it in the logging MDC and echoes it; a RestClient customizer that forwards it; the idempotency support for annotated endpoints with its storage; and helpers for the caching profiles.

Java
@AutoConfiguration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
class HeaderStandardAutoConfiguration {
@Bean
FilterRegistrationBean<CorrelationIdFilter> correlationIdFilter() {
var reg = new FilterRegistrationBean<>(new CorrelationIdFilter("X-Correlation-Id"));
reg.setOrder(Ordered.HIGHEST_PRECEDENCE + 10);
return reg;
}
}
// META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports lists the class

The starter has its own tests: a request without the header gets a generated id in the response, a request with one keeps it, the id appears in log output, and an idempotent endpoint replays the same response on a retry. Services then add one contract test each, so an upgrade of the starter that breaks the behaviour fails their build, not production.

7Follow-up questions

Should services trust a correlation id sent by an external client?

They can log it, but they should validate its format and length, because it ends up in logs and could carry injection attempts. Many gateways keep the client's value in a separate field and always create their own id, so internal ids remain unique.

How long should idempotency keys be stored?

Long enough to cover every realistic retry, including retries after a client restarts. Twenty-four hours is a common choice for payments; some providers keep keys for days. The standard should state it, and clients should know that a retry after the retention period is treated as a new operation.

Why not put rate limiting in each service as well?

Services may protect themselves with internal limits or bulkheads, but they should not advertise them with public rate limit headers. One owner for the client-facing limit keeps the numbers consistent, and the gateway sees all traffic, so it can count fairly across instances.

8Conclusion

In this answer, we designed a header standard: W3C traceparent and one correlation header with a fixed name and format, Idempotency-Key with shared server-side storage for POST operations clients may retry, caching profiles built from Cache-Control, ETag and Vary, and rate limit headers set only at the gateway in one format. We then put the service-side behaviour into a tested shared starter so every service follows the standard by default.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write a short standard that covers each header's name, format, who sets it and what a service does when it is missing. For tracing I would not invent anything: W3C traceparent, which Micrometer Tracing propagates automatically, and the trace id in every log line.

Then, if they want more

If the business wants a human-friendly correlation id, I would fix one name like X-Correlation-Id, a UUID format, created by the gateway when absent and echoed in every response. For retries, POST endpoints with side effects such as payments require an Idempotency-Key.

The server stores the request hash and the response per key in shared storage for something like 24 hours, replays the response on a retry, answers 422 if the same key comes with a different body, and 409 while the first call is still running.

For reads, I would define caching profiles: no-store for personal data, no-cache plus ETag for data worth revalidating, public with max-age for reference data, and Vary when the response depends on a header. Rate limit headers come only from the gateway, in one format, with Retry-After on 429.

Finally, I would implement the service side once in an internal Spring Boot starter with tests, so teams get the standard by adding a dependency.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design the standard for cross-cutting HTTP headers across services: correlation ids...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/896774ca9c/