Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Design how a Spring Boot service configures RestClient instances for five downstream services, each with different base URLs, timeouts, authentication and observability needs.

We put shared concerns such as observation and common headers on Spring Boot's auto-configured RestClient.Builder through a RestClientCustomizer, then create one RestClient bean per downstream from builder.clone() or a fresh builder, so settings never leak between clients. Each client gets its own request factory with explicit connect, read and connection-acquire timeouts and pool sizes, its base URL and timeouts come from typed @ConfigurationProperties, and authentication such as OAuth2 client credentials is added by an interceptor, with secrets kept out of logs.

Why this question is asked

A service that talks to five downstream services needs five slightly different clients: one is fast and internal, one is a slow partner, one needs OAuth2, one has strict rate limits. Configuring them ad hoc leads to copy-pasted code, missing timeouts and, worst of all, an interceptor or base URL meant for one client quietly appearing on another. Interviewers ask this design question to see whether we can separate shared and per-client concerns cleanly. In this answer, we will separate the two kinds of settings, put the per-client values into configuration properties, build each client with its own request factory, add authentication and logging safely, and then see why mutating a shared builder is risky.

1Separating Shared and Per-Client Concerns

Some settings are the same for every outbound call: observation, so every call shows up in metrics and traces, a correlation-id header, perhaps a User-Agent. Others differ per downstream: the base URL, timeouts, pool size, authentication, and sometimes the error handling.

Spring Boot gives us a natural place for each. The shared concerns go onto the auto-configured RestClient.Builder via RestClientCustomizer beans, which Boot applies to every builder it hands out. The per-client concerns go into one bean definition per downstream, starting from a copy of that builder.

Java
@Bean
RestClientCustomizer commonOutboundSettings(CorrelationIdInterceptor correlation) {
return builder -> builder
.defaultHeader(HttpHeaders.USER_AGENT, "orders-service")
.requestInterceptor(correlation); // adds X-Correlation-Id to every call
}

2Typed Configuration Per Downstream

Base URLs, timeouts and pool sizes differ between environments, so they belong in configuration, not in code. A record bound with @ConfigurationProperties gives us type safety, durations parsed from values like 3s, and validation at startup.

Java
@ConfigurationProperties("downstream")
public record DownstreamProperties(Map<String, Client> clients) {
public record Client(URI baseUrl, Duration connectTimeout, Duration readTimeout,
Duration acquireTimeout, int maxConnections) {}
}
# application.yaml
downstream:
clients:
inventory: { base-url: https://inventory.internal, connect-timeout: 1s, read-timeout: 2s, acquire-timeout: 200ms, max-connections: 50 }
partner: { base-url: https://api.partner.com, connect-timeout: 3s, read-timeout: 30s, acquire-timeout: 1s, max-connections: 10 }

Notice the different values. The internal inventory service answers in milliseconds, so two seconds is already generous; the partner API is slow, so it gets 30 seconds but only 10 connections, which also limits how many of our threads it can hold.

3One Client Bean Per Downstream, Each With Its Own Factory

Now each client is a bean built from a clone of the injected builder, with its own request factory. The factory is where timeouts and pooling live, so each downstream gets its own connection pool and cannot exhaust another's.

Java
@Bean
RestClient inventoryClient(RestClient.Builder builder, DownstreamProperties props) {
DownstreamProperties.Client c = props.clients().get("inventory");
return builder.clone()
.baseUrl(c.baseUrl().toString())
.requestFactory(pooledFactory(c))
.build();
}
static ClientHttpRequestFactory pooledFactory(DownstreamProperties.Client c) {
var pool = PoolingHttpClientConnectionManagerBuilder.create()
.setMaxConnTotal(c.maxConnections()).setMaxConnPerRoute(c.maxConnections())
.setDefaultConnectionConfig(ConnectionConfig.custom()
.setConnectTimeout(Timeout.of(c.connectTimeout()))
.setSocketTimeout(Timeout.of(c.readTimeout())).build())
.build();
var http = HttpClients.custom().setConnectionManager(pool)
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectionRequestTimeout(Timeout.of(c.acquireTimeout())).build())
.build();
return new HttpComponentsClientHttpRequestFactory(http);
}
Step throughHow each client is assembled
  1. HTTP interface (optional)typed methods used by business code
  2. RestClient bean per downstreambase URL, auth interceptor, status handlers
  3. builder.clone()copy of the shared builder, so nothing leaks back
  4. Shared RestClient.Builderconverters, observation, RestClientCustomizer settings
  5. Request factory per downstreamown pool, connect, read and acquire timeouts

5 layers, from the top down. Step through them one at a time.

Recent Spring Boot versions also offer ClientHttpRequestFactoryBuilder and related settings classes to build factories with timeouts in fewer lines; check your version. Whichever we use, all three timeouts are set explicitly: connect, read, and the wait for a pooled connection. For services we call a lot, we can put an HTTP interface on top of each RestClient, so business code depends on a typed interface.

4Authentication and Keeping Secrets Out of Logs

Authentication is per client, so it is an interceptor added only to the clients that need it. For OAuth2 client credentials, Spring Security provides an interceptor for RestClient that obtains and caches tokens through an OAuth2AuthorizedClientManager and adds the bearer header (OAuth2ClientHttpRequestInterceptor in Spring Security 6.4 and later; check your version for how it selects the client registration). A partner with an API key gets a small interceptor that adds the key header from a secret.

Request logging is useful for debugging, but it is a common way for credentials to leak. A logging interceptor must redact Authorization, cookies and API key headers, and log bodies only at debug level for services that carry no personal data. It also helps to keep the auth interceptor separate from the logging one and add logging first in the chain, so the logged request never contains the header at all. Observation metrics record the URI template and status, never header values.

5Why Mutating a Shared Builder Is Risky

RestClient.Builder is mutable: every call such as baseUrl() or requestInterceptor() changes the builder itself and returns the same object. Spring Boot's auto-configured builder bean has prototype scope, so each injection point gets its own fresh builder. The trouble starts when one builder is used to create two clients, or when someone declares their own singleton builder bean.

Java
@Bean
ClientsHolder clients(RestClient.Builder builder, PartnerAuthInterceptor partnerAuth) {
RestClient partner = builder.baseUrl("https://api.partner.com")
.requestInterceptor(partnerAuth).build();
RestClient inventory = builder.baseUrl("https://inventory.internal").build();
// inventory now sends the partner's API key to the inventory service
return new ClientsHolder(partner, inventory);
}

The second client inherits the interceptor added for the first one, so a partner credential is sent to an internal service, which then logs it. Nothing fails, which is why this bug survives. Calling builder.clone() before customising, or injecting a separate builder per bean method, gives each client its own copy and makes the leak impossible.

6Follow-up questions

Why is mutating a shared RestClient.Builder bean risky?

Because the builder is mutable and every method changes it in place. If one builder is reused for several clients, or a singleton builder is shared across the application, base URLs, default headers, interceptors and request factories set for one client appear on the next. That can send credentials to the wrong host or apply the wrong timeouts. Spring Boot's builder is prototype-scoped to avoid this across injection points, and builder.clone() avoids it within one method.

Should all five clients share one connection pool?

Usually not. A shared pool lets one slow downstream occupy connections the others need. Separate pools per downstream act as bulkheads, and their sizes can follow each service's capacity and latency.

7Conclusion

In this answer, we designed RestClient configuration for five downstream services. Shared concerns go on the auto-configured builder through RestClientCustomizer, each downstream gets its own RestClient bean from builder.clone() with its own pooled request factory and explicit connect, read and acquire timeouts, base URLs and timeouts come from typed @ConfigurationProperties, authentication is an interceptor on the clients that need it with secrets redacted from logs, and we saw why mutating a shared builder leaks settings between clients.

What to say in the interviewAbout 80 seconds aloud

Open with

I split the settings into shared and per-client. Shared concerns, like a User-Agent and a correlation-id interceptor, go on Spring Boot's auto-configured RestClient.Builder through a RestClientCustomizer bean; that builder already has the message converters and observation, so every call shows up in metrics and traces.

Then, if they want more

Per-client values, base URL, connect, read and connection-acquire timeouts and pool size, go into a typed @ConfigurationProperties record with one entry per downstream, so a slow partner can get thirty seconds and ten connections while an internal service gets two seconds and fifty.

Then I define one RestClient bean per downstream, always starting from builder.clone(), setting the base URL and a request factory of its own, for example Apache HttpClient 5 with its own pool, which also acts as a bulkhead.

Authentication is an interceptor added only where needed, such as Spring Security's OAuth2 client interceptor for client credentials or a small API-key interceptor reading from a secret, and any logging interceptor redacts Authorization and key headers. The reason for clone is that the builder is mutable.

Boot makes the bean prototype-scoped, but if I build two clients from one builder, the second inherits the first one's base URL and interceptors, and could send a partner's API key to an internal service without any error.

About this answer

Last updated
12 November 2026
Versions it names
Spring Security 6
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 how a Spring Boot service configures RestClient instances for five downstream...": https://scrivio-interview-library.netlify.app/spring-boot/http-clients/8a37fb7e32/