Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A team wraps a client as new MetricsClient(new RetryClient(new CachingClient(realClient))). Dashboards show fewer failures than the remote service reports, and cache hits are counted as very fast remote calls. Explain what the order does and propose a better order.

Each decorator sees the combined behaviour of everything inside it. With MetricsClient outermost, metrics record one call per caller request: when retry turns two failed attempts into a final success, metrics see one success, so failures are hidden, and when the cache answers, metrics see a very fast successful call, so latency looks better than reality. A better order is new CachingClient(new RetryClient(new MetricsClient(realClient))), so every real attempt is measured, plus a separate counter for cache hits and misses, and the chosen order should be documented and tested.

Why this question is asked

Decorators are easy to combine, which is their strength, and the order is easy to get wrong, which is their trap. The bug in this question is not in any single class: each decorator is correct, and the dashboard still lies. Interviewers like it because it tests whether we can reason about a call travelling through layers and what each layer can observe. In this answer, we will recap how a call flows through stacked decorators, trace the current order and see exactly why failures and latency are distorted, propose a better order, add the metrics that are still missing, and make the order something we test.

1A Quick Recap: How a Call Flows Through Decorators

A decorator implements the same interface as the object it wraps and passes each call on to it, adding behaviour on the way in and on the way out. When decorators are nested, a call enters the outermost one first, goes inward layer by layer to the real client, and the result travels back out in reverse order.

The key fact is what each layer can see. A decorator only sees the calls it receives and the results that come back to it. It cannot see what happens further inside, such as how many attempts a retry layer made or whether a cache answered without calling the remote service. So the outermost decorator observes the combined behaviour of everything inside it.

2Tracing the Current Order

The current wiring is new MetricsClient(new RetryClient(new CachingClient(realClient))). Let us follow two calls through it.

Java
Client client = new MetricsClient(new RetryClient(new CachingClient(realClient)));
// Call 1: key not cached, remote fails twice, then succeeds
client.get("k1");
// Metrics -> Retry -> Caching (miss) -> real: 500 error
// Retry -> Caching (miss) -> real: 500 error
// Retry -> Caching (miss) -> real: 200 OK, 180 ms
// Metrics records: 1 call, success, about 400 ms total
// Remote service records: 3 requests, 2 failures
// Call 2: key cached
client.get("k1");
// Metrics -> Retry -> Caching (hit), the real client is never called
// Metrics records: 1 call, success, 0.2 ms
// Remote service records: nothing
Step throughCall 1 through the current order
MetricsRetryCachingReal client
get(k1), timer starts
attempt 1
miss, call remote
500 error
attempt 2, then attempt 3
miss, call remote
200 OK
success: one call, failures never seen

8 messages in order. Play them, or step through them one at a time.

Metrics stands outside the retry and the cache, so it records one successful call.

Now the dashboard symptoms are easy to explain. In call 1, two attempts failed, and the remote service counted both, but MetricsClient sits outside RetryClient and only sees the final outcome, which was a success. So the dashboard shows fewer failures than the remote service reports; with a 30 percent per-attempt failure rate and three attempts, the dashboard would show under 3 percent of calls failing.

In call 2, the answer came from the cache in a fraction of a millisecond. MetricsClient sits outside CachingClient as well, so it records a remote call that took 0.2 ms. With a high hit rate, the average and even the p95 latency of remote calls look excellent, while the real remote latency is hidden among cache hits.

3Choosing a Better Order

We decide the order by asking what each layer should observe. Metrics about the remote service should observe every real attempt, so they belong closest to the real client. Retry should repeat real calls, not cache lookups, so it sits above metrics. The cache should answer before anything remote happens, so it is outermost.

Java
Client client = new CachingClient(new RetryClient(new MetricsClient(realClient)));
client.get("k1"); // miss: Caching -> Retry -> Metrics -> real, three attempts
// Metrics records: 3 calls, 2 failures, 1 success, each with its own latency
client.get("k1"); // hit: Caching answers, nothing below is called
// Metrics records: nothing, which is correct because no remote call happened
What each order makes metrics see
EventMetrics outermost (current)Metrics innermost (proposed)
Two failed attempts, then success1 call, success3 calls, 2 failures
Cache hit1 very fast remote callNot recorded as a remote call
Remote latency p95Mixed with cache hitsReal remote calls only
Matches remote service's own numbersNoYes

Now the per-attempt failure rate on the dashboard matches what the remote service reports, and the latency histogram contains only real remote calls. Notice another benefit: in the old order, RetryClient was outside the cache, so a retry would also look in the cache again, which is harmless but pointless. In the new order, a cache hit never reaches the retry layer at all.

4Adding the Metrics That Are Still Missing

Moving metrics inward answers one question, how the remote service behaves, but loses another, what the caller experiences. Both are useful, so we measure them separately with clearly named metrics. CachingClient records cache hits and misses, which gives the hit rate. An outer timer, or the existing request metrics of the web framework, records end-to-end latency as the caller sees it, including retries and backoff. RetryClient can count retries and calls that failed after all attempts.

With names such as pricing.remote.calls, pricing.cache.hits and pricing.client.latency, nobody confuses one view with another, which is exactly the confusion that caused this bug.

5Documenting and Testing the Order

Decorator order changes behaviour, so it deserves the same care as any other behaviour. We put the wiring in one place, a factory method or a Spring @Bean method, with a comment explaining why each layer is where it is. Then we add a test that fails if someone reorders it.

Java
@Test
void metricsCountEveryRemoteAttemptAndIgnoreCacheHits() {
var real = new FailingThenOkClient(2); // fails twice, then succeeds
var registry = new SimpleMeterRegistry();
Client client = ClientFactory.create(real, registry);
client.get("k1");
client.get("k1"); // served from the cache
assertEquals(3, registry.counter("pricing.remote.calls").count()); // 3.0
assertEquals(2, registry.counter("pricing.remote.failures").count()); // 2.0
assertEquals(1, registry.counter("pricing.cache.hits").count()); // 1.0
}

This test describes the intended semantics directly. If a future change moves MetricsClient back outside, the remote call count drops to 1 and the test fails with a clear message.

6Follow-up questions

Should retry ever be outside the cache?

Rarely. A cache lookup is local and does not fail transiently, so retrying it adds nothing. The only case is a remote cache, such as Redis, where we might want a separate, much shorter retry policy for the cache itself.

Where does a circuit breaker go in this stack?

Usually just inside retry, between retry and metrics, so that when the circuit is open, retry sees a fast failure and stops, and metrics still record real remote calls only. The exact order again depends on what we want each layer to see, so we write it down and test it.

How would you notice this kind of bug sooner?

By comparing our client-side metrics with the server-side metrics of the remote service regularly. When the two disagree by a large factor, the layering of our decorators is the first thing to check.

7Conclusion

In this answer, we saw that the outermost decorator observes the combined behaviour of everything inside it. With metrics outside retry, several failed attempts look like one success and failures are hidden; with metrics outside caching, cache hits look like very fast remote calls and distort latency. Putting caching outside, then retry, then metrics next to the real client measures every attempt, and separate cache hit and end-to-end metrics keep the other views. Because order changes semantics, we document it and test it.

What to say in the interviewAbout 80 seconds aloud

Open with

Each decorator only sees what comes back from the layers inside it, so the outermost one sees the combined behaviour. Here MetricsClient is outermost. When the remote call fails twice and the third attempt succeeds, RetryClient hides those failures and Metrics records one successful call, while the remote service counts three requests and two failures, so the dashboard under-reports failures.

Then, if they want more

And because Metrics is also outside CachingClient, a cache hit that takes a fraction of a millisecond is recorded as a remote call, so the latency numbers look much better than the real service. I'd reorder it to Caching outermost, then Retry, then Metrics right next to the real client.

Then every real attempt is counted with its own latency, the numbers match the server's, and cache hits never reach the remote metrics. Since that drops the caller's view, I'd add a separate hit and miss counter in the caching layer and an end-to-end timer outside, with distinct metric names.

Finally, I'd put the wiring in one factory or bean method with a comment, and add a test that checks a twice-failing client produces three remote calls and a cache hit produces none, so a future reorder fails the build.

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 official documentation for the version you use. 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 "A team wraps a client as new MetricsClient(new RetryClient(new...": https://scrivio-interview-library.netlify.app/java/structural-patterns/2a090436b5/