Video in progress
`order-service` calls `pricing-service`, and `pricing-service` calls `order-service` again to calculate a discount. What problems does this circular dependency cause, and how do you break it?
A cycle amplifies failures (both services spend their threads waiting for each other) and makes deploys and changes harder. It usually means the boundary was drawn wrong. Ways to break it: send the needed data in the request itself, copy it with events, or move the responsibility into one service.
📖 Answer
The flow: POST /orders → order-service calls pricing.quote(cart) → pricing, to learn "the customer's number of previous orders", calls GET /orders/count?customer=42 → order-service.
What happens:
- The threads wait on each other. order-service's 200 threads wait for pricing, and pricing waits for a response from order-service, which has no free thread left. Under load this looks like a deadlock: both services hang until the timeout.
- Retries multiply around the cycle.
- A contract change requires a coordinated deploy on both sides.
- Running locally and integration tests need both together too.
Ways to break it (from simple to complex):
- Pass the data in the request. order-service knows the order count itself, so it sends it along in the
quoterequest:
record QuoteRequest(List<Item> items, long customerId, int previousOrders) {}
pricing no longer calls order-service. This is the fastest and often the most correct solution.
-
A copy through events. If pricing needs order statistics in many places, it keeps its own table from
OrderCompletedevents. The synchronous cycle becomes a one-way asynchronous flow. -
Moving the responsibility. Whose job is the "loyalty discount" really? Maybe it belongs to a separate
loyaltyservice, or order-service should apply the discount itself after getting the price. -
If two services depend on each other this much, maybe they should be one service.
To find cycles ahead of time, a service graph built from tracing data helps: in the Jaeger or Tempo service graph a cycle is visible immediately. Writing an architecture rule like "no cycles in the graph of synchronous calls" and checking it in review works too.
Sign up to mark studied questions and save your progress.