Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

Service A calls service B synchronously, and B, while handling some requests, calls back to A. Both use bounded thread pools. During a traffic spike, both services stop responding until timeouts fire. Explain and propose architectural fixes.

During the spike, A's request threads are all waiting for responses from B, and B's threads are all handling requests that call back to A. When both bounded pools are full, A has no free thread to serve B's callbacks and B has no free thread to finish A's requests, so everything waits until timeouts fire. It is a distributed deadlock that only timeouts can break. The architectural fix is to remove the synchronous call cycle and keep the dependency graph acyclic, by passing the needed data in the original request or by replacing the callback with asynchronous events; in the short term, set timeouts shorter than the caller's and isolate thread pools per inbound path.

Why this question is asked

Deadlocks used to live inside one JVM, where a thread dump could find them. In a system of services, the same cycle can span two processes, and no tool on either side sees it: each service just looks slow, with every thread waiting on an HTTP call. Interviewers ask this scenario to see whether we can recognise a deadlock across network boundaries and fix it in the architecture rather than by adding threads. In this answer, we will recap thread-per-request services, trace how the cycle forms during a spike, explain why only timeouts break it, and then go through the architectural fixes and the short-term mitigations.

1A Quick Recap of Bounded Pools in Services

A typical Spring MVC service handles each incoming request on a thread from a bounded pool, for example Tomcat's default maximum of 200, and makes outgoing HTTP calls synchronously: the thread sends the request and waits for the response. While it waits, the thread is unavailable for other work. If all threads are waiting, new requests queue in front of the server until a thread is free.

That is fine as long as the threads we wait for are in a different pool that is not waiting for us. The question describes the case where they are.

2How the Cycle Forms During a Spike

Say A and B each have 200 threads. A request to A calls B. While handling some of those calls, B calls back to A, for example to fetch a customer's details, and waits. At normal traffic, A has spare threads to answer the callback, and everything completes.

Step throughA distributed deadlock between two services
ClientsService A (200 threads)Service B (200 threads)
spike: more requests than threads
200 calls; all A threads now wait
callback for customer details
no free thread; callback queues
B threads wait for callbacks
after timeout: errors

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

Every thread in both services is waiting for a thread in the other.

During a spike, A receives more requests than it has threads, and each of its 200 threads is soon waiting for B. B receives those 200 calls, and its threads call back to A. But A has no free threads: all of them are waiting for B. The callbacks queue in front of A. B's threads wait for the callbacks, and A's threads wait for B's threads. Every thread in both services waits, and no thread will ever finish, because each finish depends on a thread that is itself waiting. That is a cycle of waits, the same shape as two locks taken in opposite order, with thread pools as the resources.

3Why Only Timeouts Break It

Inside a JVM, a lock deadlock lasts forever; here, the cycle is broken eventually by timeouts on the HTTP calls or at the load balancer. When B's callbacks to A time out, B's threads fail their requests and free up; when A's calls to B time out, A's threads free up. Then the next wave of the spike fills the pools again and the cycle reforms. So the visible symptom is both services stopping together, with periodic waves of timeout errors, until traffic falls.

No single tool shows the cycle. A's thread dump shows 200 threads waiting on socket reads to B, and B's shows 200 threads waiting on socket reads to A. The deadlock is only visible when we put the two together, or in distributed traces that show requests from A to B to A. That is why the real fix has to be in the design.

4Fix 1: Make the Synchronous Call Graph Acyclic

The lasting fix is to forbid synchronous call cycles: if A calls B synchronously, B must never call A synchronously, directly or through other services. Then waits always point in one direction, just like a global lock order, and a cycle cannot form. Most callbacks exist because B needs some data that A has. The simplest way to remove them is to pass that data in the original request.

Java
// before: B calls back to A for the customer
POST /b/quotes { "customerId": 42 }
-> B calls GET /a/customers/42 // synchronous callback: cycle
// after: A sends what B needs
POST /b/quotes { "customerId": 42, "segment": "gold", "country": "DE" }
-> B computes the quote without calling A
Ways to remove the callback
OptionWhen it fitsCost
Pass data in the requestB needs a few fields A already hasSlightly larger request
B keeps a copy from A's eventsData changes rarely, read oftenEventual consistency, an event pipeline
Asynchronous event instead of callThe callback is a notificationBroker, idempotent handling
Merge the two servicesThey cannot work apartBigger service, a design decision

If B needs data that changes rarely, it can also keep its own copy, kept up to date from events A publishes, so it never needs to ask A at request time. And if the callback is really a notification, such as telling A the quote is ready, it should be an asynchronous event through a broker like Kafka rather than a synchronous call, so B's thread does not wait for A at all.

5Short-Term Mitigations

Until the cycle is removed, two measures reduce the damage. First, timeouts should get shorter as calls go deeper: if clients give A 5 seconds, A should give B about 3, and B's callback to A about 1. Then the innermost call fails first, its thread frees quickly, and the outer call can still fail cleanly within its own budget, instead of every layer waiting its full default timeout, which in some HTTP clients is unlimited.

Second, isolate thread pools per inbound path. If A serves B's callbacks from a separate, small pool, or from a separate deployment, then A's main pool being full cannot block the callbacks, and the cycle is broken at the resource level. This is a bulkhead: a partition of capacity so that one kind of traffic cannot starve another. It costs some capacity and configuration, which is why it is a mitigation rather than the design we want.

Note that virtual threads would remove the fixed thread pools, so this particular cycle would not freeze on thread capacity. But the same cycle can form on any other bounded resource, such as connection pools or rate limits, so the architectural rule still applies.

6Follow-up questions

How would you find such cycles before they cause an outage?

Distributed tracing shows the call graph between services. Building the graph of synchronous calls from trace data or from service manifests, and checking it for cycles in CI or in a regular report, finds them while they are still harmless. Load tests that push both services to their pool limits reproduce the freeze.

Would a circuit breaker help?

It helps the system recover faster. Once B's callbacks to A start failing, a breaker on B's client for A opens and fails them immediately instead of waiting for timeouts, which frees B's threads sooner. It does not remove the cycle, so the freeze can still start; it just ends sooner.

Is a cycle through a third service also a problem?

Yes. A calling B, B calling C and C calling A forms the same cycle, only harder to notice. The rule has to be about the whole synchronous call graph, not just pairs of services.

7Conclusion

In this answer, we saw that when A calls B synchronously and B calls back to A, a traffic spike fills both bounded pools with threads waiting for each other, so neither service can serve the other, and only timeouts break the cycle. It is a distributed deadlock that no single thread dump shows. The architectural fix is an acyclic synchronous call graph, achieved by passing needed data in the original request, keeping a local copy fed by events, or replacing the callback with an asynchronous event. In the short term, deadline-based timeouts that shrink with depth and separate pools per inbound path limit the damage.

What to say in the interviewAbout 90 seconds aloud

Open with

It is a deadlock across two services. A handles requests on a bounded pool and calls B synchronously, so each A thread waits for B. Some B requests call back to A, so those B threads wait for A.

Then, if they want more

At normal traffic A has spare threads for the callbacks. During a spike, all of A's threads are waiting for B, B's threads are waiting for callbacks to A, and the callbacks queue because A has no free thread. Each thread waits for a thread on the other side, so nothing finishes.

Unlike a lock deadlock in one JVM, it ends only when timeouts fire, then reforms with the next wave of traffic. Neither thread dump shows it alone; you see threads waiting on sockets in both. The real fix is architectural: no synchronous call cycles, the dependency graph between services must be acyclic.

Usually the callback fetches data A already has, so A should pass it in the original request, or B keeps its own copy updated from A's events. If the callback is really a notification, it should be an asynchronous event.

Short term, I would set timeouts from the caller's deadline, shorter at each hop, so inner calls fail first, and give B's callbacks their own pool in A, a bulkhead, so A's main pool filling up cannot block them.

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 "Service A calls service B synchronously, and B, while handling some requests, calls back...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/b58d3c31ea/