Scrivio Interview library
ConceptPrincipal7 min read · updated 12 November 2026

As systems moved from one large JVM to many services, deadlocks shifted from lock cycles inside a process to waits across services: call cycles, exhausted pools and retries. What long-term architecture principles would you set to prevent these?

The principles I would set are: keep the synchronous call graph between services acyclic; bound every wait with a timeout derived from the caller's deadline; isolate resources per dependency so one slow dependency cannot exhaust shared pools; prefer asynchronous events for workflows that span several services; and test failure modes with fault injection before production finds them. Together they remove the cycles, bound the waits and contain the pools that produce deadlocks in distributed systems.

Why this question is asked

Inside one JVM, deadlocks are cycles of locks, and the JVM can at least detect them. Across services, the same failure appears in forms no tool detects: a synchronous call cycle, a shared connection pool drained by a slow dependency, or retries that keep a service from ever catching up. Each looks like a performance problem rather than a deadlock, and each is created by architecture decisions made long before the incident. Interviewers ask senior engineers this question to see whether they can turn incident lessons into principles that prevent the next one. In this answer, we will recap how deadlock conditions map onto distributed systems, and then go through each principle, what it prevents and how to make it stick.

1A Quick Recap: The Deadlock Conditions Across Services

A deadlock needs four things: resources that only one party can hold at a time, parties that hold one resource while waiting for another, no way to take a resource away, and a cycle of waits. In a monolith, the resources were locks. Across services, they are threads in bounded pools, connections, rate-limit tokens and queue slots, and the waits are synchronous calls and retries.

So the conditions are still there, only spread out. Service A's thread holds itself while waiting for B; B's thread holds itself while waiting for A; nobody can preempt them; and the call graph forms the cycle. A principle that removes any of the conditions, or bounds how long the waits last, removes the whole class of failure. That is the logic behind each standard below.

From in-process deadlocks to distributed ones
In one JVMAcross servicesPrinciple that breaks it
Lock cycleSynchronous call cycleAcyclic call graph
Wait forever on a lockWait on a call with no deadlineTimeouts from the caller's deadline
One lock for everythingOne shared pool for all dependenciesResources isolated per dependency
Nested locks held across callsThreads held across long workflowsAsynchronous events between services
Found by a thread dumpFound only by testing failureFault injection before production

2Keep the Synchronous Call Graph Acyclic

If service A calls B synchronously, then B, and every service B calls, must never call A synchronously. That is the distributed form of a global lock order: waits only point one way, so no cycle can form. It also keeps the system understandable, because each service has a clear set of dependencies below it.

To make it stick, we need the graph. Distributed tracing data or service catalogue entries give the edges, and a regular check reports any cycle, ideally in CI when a team adds a new client. Breaking a cycle usually means passing data down in the request instead of calling back, keeping a local copy from events, or merging two services that cannot really work apart.

3Bound Every Wait With a Deadline

Every network call, every pool acquisition and every lock wait must have a limit, and the limit should come from the caller's remaining time, not from a library default that may be 30 seconds or infinite. If a user request has a 2-second budget, the edge passes a deadline down; a service that has already used 800 ms gives its downstream call at most what remains minus a margin. Then inner calls fail before outer ones and the whole chain stops within the budget, instead of every layer waiting its full default.

Java
// the edge sets the deadline; each hop derives its timeout from it
Instant deadline = Instant.now().plusMillis(2000);
Duration remaining = Duration.between(Instant.now(), deadline).minusMillis(100); // 100 ms margin
if (remaining.isNegative()) throw new DeadlineExceededException();
Quote q = pricingClient.quote(req, remaining); // the downstream call cannot outlive our caller

gRPC propagates deadlines natively; for HTTP we pass the deadline in a header and enforce it in a shared client library. Timeouts are what turn a would-be permanent deadlock into a brief, bounded failure.

4Isolate Resources per Dependency

If one thread pool or one connection pool is shared by calls to every dependency, the slowest dependency decides the fate of all the others: its calls hold resources longer, the shared pool fills, and calls to healthy dependencies wait too. This is starvation rather than a strict cycle, but it produces the same frozen service. The principle is a bulkhead per dependency: each downstream service gets its own bounded concurrency, for example a semaphore or a separate client connection pool sized for it, so a slow payments API can exhaust only the capacity assigned to payments.

With virtual threads this becomes more important, not less, because there is no longer a thread pool acting as an accidental global limit. The limits must be explicit and per resource.

5Prefer Asynchronous Events for Multi-Service Workflows

A workflow such as placing an order, reserving stock, taking payment and arranging shipping can be written as a chain of synchronous calls, in which the first service holds a thread and a request open while four others do their work. The longer that chain, the more resources are held while waiting, and the more chances for a cycle. Running such workflows as asynchronous events, where each service does its step and publishes an event for the next, means nobody holds resources while waiting for others. Failures are handled with compensating steps, the saga pattern, rather than long-held transactions.

This is not a rule to make everything asynchronous. Simple queries that need an answer now are better as synchronous calls. The principle is about workflows that span several services and take longer than one request should.

6Test Failure Modes Before Production Does

Distributed deadlocks and starvation only appear under specific conditions: a slow dependency, a full pool, a traffic spike. We cannot find them by reading code, so we create those conditions deliberately. Fault injection adds latency or errors to a dependency in a test environment, or in production with care, and checks that the timeouts, bulkheads and fallbacks behave as designed: does the service stay responsive when payments takes 10 seconds? Does a spike with a pool limit reached produce fast errors rather than a freeze?

Making this a regular practice, for example a game day each quarter for critical paths and automated latency-injection tests in the release pipeline, turns the principles into verified behaviour. Each finding updates the standards, which is how they stay relevant.

7Follow-up questions

How do you introduce these principles into an existing system with cycles already in it?

Map the current call graph first, from tracing data, and list the cycles and the shared pools. Fix the highest-traffic cycles first, usually by passing data in requests, and add deadline propagation and bulkheads in the shared client library so every team gets them by upgrading. New services must follow the principles from the start, so the problem stops growing while the old cycles are removed.

Do retries fit into these principles?

Yes, and they need their own rules, because retries multiply load on a service that is already struggling, which can keep it from recovering. Retries should use exponential backoff with jitter, stay within a retry budget, happen at only one layer, apply only to idempotent operations, and respect the caller's deadline, so a retry never starts after the caller has given up.

What would you measure to know the principles work?

The number of synchronous call cycles in the graph, the share of outbound calls with deadline-derived timeouts, the number of incidents where one dependency's slowness spread to others, and the results of fault injection tests. Those numbers show whether the architecture is getting safer, independent of how many incidents happened to occur.

8Conclusion

In this answer, we saw how the four deadlock conditions reappear across services as call cycles, unbounded waits, shared pools and long-held workflows. The principles that address them are an acyclic synchronous call graph, timeouts derived from the caller's deadline for every wait, resources isolated per dependency, asynchronous events for workflows that span several services, and regular fault injection to prove the system behaves as designed before production tests it for us.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from the observation that the deadlock conditions did not go away with microservices; the resources changed. Instead of locks, we have threads in bounded pools, connections and rate-limit tokens, and instead of nested locks, synchronous calls.

Then, if they want more

So I would set five principles. First, the synchronous call graph must be acyclic: if A calls B, nothing B calls may call A synchronously. That is the distributed version of a global lock order, and I would check the graph from tracing data so new cycles are caught early.

Second, every wait has a bound derived from the caller's deadline. The edge sets a deadline, each hop passes it down and uses what remains, so inner calls fail first and nothing waits forever on a default timeout.

Third, resources are isolated per dependency, with bulkheads like a semaphore or connection pool per downstream service, so one slow dependency can only use its own share. That matters even more with virtual threads, where there is no thread pool acting as a hidden limit.

Fourth, workflows that span several services run as asynchronous events with compensations, so nobody holds threads while four other services work. Fifth, we test failure modes with fault injection, adding latency to dependencies and checking that timeouts and bulkheads behave, before production finds the problem for us.

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 "As systems moved from one large JVM to many services, deadlocks shifted from lock cycles...": https://scrivio-interview-library.netlify.app/java/concurrency-problems/40ce2086ad/