Quick revision
The short answer
We move the filters into an internal Spring Boot starter with auto-configuration, so every service gets the same request id, logging and tenant behaviour by adding one dependency. The starter defines header names, logging fields and filter order once, explicitly positioned relative to Spring Security's filter; it makes behaviour configurable through properties and replaceable with @ConditionalOnMissingBean; it uses Micrometer tracing and observation wherever they already cover the need instead of custom code; and it is tested with context runners and sample applications on every supported Boot version.
What to remember
In this answer, we standardised request id, logging and tenant filters by moving them into an internal Spring Boot starter with auto-configuration. The starter defines header names, MDC fields and filter order once, relative to Spring Security; exposes the parts that vary as properties and makes beans replaceable with @ConditionalOnMissingBean; relies on Micrometer tracing and observation for correlation and metrics where they already cover the need; and is tested with context runners and sample applications on each supported Boot version.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We move the filters into an internal Spring Boot starter with auto-configuration, so every service gets the same request id, logging and tenant behaviour by adding one dependency. The starter defines header names, logging fields and filter order once, explicitly positioned relative to Spring Security's filter; it makes behaviour configurable through properties and replaceable with @ConditionalOnMissingBean; it uses Micrometer tracing and observation wherever they already cover the need instead of custom code; and it is tested with context runners and sample applications on every supported Boot version.
Why this question is asked
When fifty services each write their own request id filter, the company ends up with fifty slightly different ideas of what a request id is: different header names, ids that are not passed downstream, tenant headers checked before or after authentication depending on the team. The cost shows up during incidents, when logs from different services cannot be joined. Interviewers ask this scenario to see whether a senior engineer knows how Spring Boot's own extension mechanism can turn copy-pasted code into a shared, safe default. In this answer, we will look at what goes wrong today, design the starter, define the shared conventions, make it configurable, decide what to leave to Micrometer, and test it across Boot versions.
1What Goes Wrong With Per-Service Filters
A servlet filter for request ids, one for access logging and one for tenant headers are each about forty lines, so every team writes them. The differences are small but painful. One service uses X-Request-Id and another X-Correlation-Id, so ids are lost at the boundary. One puts the id into the MDC as requestId and another as reqId, so log searches miss half the lines. One reads the tenant header before Spring Security runs and trusts it, another after authentication and checks it against the user's tenants, which is a security difference, not a style difference. And each bug, such as forgetting to clear the MDC in finally, has to be found and fixed fifty times.
2Designing the Starter
A Spring Boot starter is a library that a service adds as a dependency to get a feature configured automatically. It has two parts by convention: an auto-configuration module with the code and conditional bean definitions, and a starter module that only pulls in dependencies. Boot discovers auto-configuration classes listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports in the jar (check this location against the Boot versions you support), and applies each one only when its conditions match.
- Service codecontrollers and business logic, no filter code
- company-web-starter dependencyone line in the pom
- Auto-configurationconditional beans, properties, fixed order
- Shared filtersrequest id at -110, access log, tenant (inside security chain)
- Micrometer tracing and observation
traceId,spanId, HTTP metrics, propagation
5 layers, from the top down. Step through them one at a time.
Notice the shape. Each bean is conditional on there being no bean of the same type already, so a service that needs something special can define its own and the default steps aside. The tenant filter only exists when a service turns it on. The order of the request id filter is a deliberate number relative to Spring Security, written once, in one place.
3Defining the Conventions Once
The starter is where the company decides, once, what the conventions are. Header names: for example X-Request-Id for the request id, X-Tenant-Id for the tenant. Logging fields: the MDC keys, such as requestId and tenantId, and which fields appear in JSON logs. Ordering: the request id and access log filters run before Spring Security, so even rejected requests are logged with an id; the tenant check, which must trust only authenticated users, runs inside the security chain after authentication, added through a small SecurityFilterChain customizer the starter provides.
Writing these down in the starter's documentation and code makes them the default for every new service, and turns a cross-team debate into a single reviewed decision.
4Making It Configurable and Replaceable
A starter that cannot be adjusted gets forked. So behaviour that legitimately varies is exposed as properties, bound to a @ConfigurationProperties class: header names, which paths to skip for access logging (health checks, for example), whether the tenant header is required. Teams change a property instead of copying code.
And for the rare service that needs completely different behaviour, @ConditionalOnMissingBean means it can declare its own RequestIdFilter bean and the starter will not create one. The service still gets the rest of the starter.
5Using Micrometer Where It Already Covers the Need
Some of what teams wrote by hand is now built in. With Micrometer Tracing and a tracing bridge on the classpath, Spring Boot creates a trace for each incoming request, puts traceId and spanId into the MDC, and propagates them to outgoing calls made with RestClient, WebClient and other instrumented clients through standard headers such as W3C traceparent. Spring's observation support also records HTTP server metrics for every request.
So the starter should rely on that for correlation and metrics, and only add what tracing does not give: perhaps a business-facing request id returned to clients, the tenant context, and the company's access log format. Less custom code means less to maintain and fewer differences from the wider ecosystem.
6Testing the Starter on Every Supported Boot Version
Because many services depend on it, the starter needs stronger tests than a single service. Spring Boot's ApplicationContextRunner and WebApplicationContextRunner start a tiny context with only our auto-configuration and let us assert which beans exist under which properties, in milliseconds.
On top of that, a few small sample applications, one per supported Boot line, for example 3.5 and 4.0, with and without Spring Security, run end-to-end tests in the starter's build: they send requests and check headers, MDC fields in captured logs, and that a request rejected by security still has an id. When a new Boot version arrives, these samples tell us before any service upgrades.
7Follow-up questions
How would you roll the starter out to existing services?
Gradually and with something in it for each team. First, make the starter's defaults compatible with the most common existing behaviour, so adopting it changes nothing visible. Then migrate a few willing services, delete their hand-written filters and compare logs before and after. Publish the result, add the starter to the service template so all new services get it, and offer a short migration guide. Services that need different headers during the transition can set properties instead of keeping their own code.
Who should own the starter?
A platform or developer-experience team, with a contribution model so service teams can propose changes. It should have semantic versions, release notes and a compatibility matrix of Boot versions, because it sits under many services.
Why not put the filters in a shared base class instead?
A base class still needs every service to wire it, order it and register it, which is where the differences came from. Auto-configuration does the wiring the same way everywhere and stays optional per bean.
8Conclusion
In this answer, we standardised request id, logging and tenant filters by moving them into an internal Spring Boot starter with auto-configuration. The starter defines header names, MDC fields and filter order once, relative to Spring Security; exposes the parts that vary as properties and makes beans replaceable with @ConditionalOnMissingBean; relies on Micrometer tracing and observation for correlation and metrics where they already cover the need; and is tested with context runners and sample applications on each supported Boot version.
Open with
I would turn the filters into an internal Spring Boot starter, so a service gets them by adding one dependency instead of copying code. The starter has an auto-configuration class, registered in the AutoConfiguration.imports file, conditional on a servlet web application.
Then, if they want more
In it I define the request id filter, the access log filter and the tenant filter as beans, each with @ConditionalOnMissingBean so a service that really needs its own can replace it, and the tenant filter only when a property enables it.
The important part is that the conventions are decided once: the header names, say X-Request-Id and X-Tenant-Id, the MDC keys and log fields, and the ordering.
The request id and access log filters are registered with a FilterRegistrationBean at an order just before Spring Security's minus 100, so rejected requests are logged with an id too, while the tenant check runs inside the security chain after authentication, because trusting a tenant header before knowing the user is a security issue.
What varies, like header names or paths to skip, is exposed through a @ConfigurationProperties record. I would also avoid writing what Micrometer already does: with Micrometer Tracing, Boot puts traceId and spanId into the MDC and propagates them downstream, and observations give HTTP metrics, so the starter only adds the business request id, tenant context and our log format.
Finally I would test it with WebApplicationContextRunner for conditions and replacement, and with sample applications on each supported Boot version, such as 3.5 and 4.0, with and without security. Rollout would be gradual, with defaults compatible with current behaviour.
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.
- Official documentation
- Jakarta EE specifications
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 "Every service in your company writes its own servlet filters for request ids, logging...": https://scrivio-interview-library.netlify.app/java/servlets/3d7a131d44/