Video in progress

Staff 🌱 Spring

In a chain of services (A → B → C → D), if one slows down, what layers of protection do you build so the whole system doesn't stall?

A timeout on every call and a total deadline budget, bounded retries, a circuit breaker, bulkheads (isolating resources), fallbacks and load shedding. The strongest protection is shortening the synchronous chain or making it asynchronous.

📖 Answer

How the problem develops: D slows down → C's threads get stuck waiting for D → C slows down → then B, then A. Nobody has crashed, but nobody is answering. This is a cascading failure.

The layers of protection:

  1. A deadline budget. The user request gets 2 s. A gives B 1.8 s of that, B gives the remainder to C, and so on. If the budget is spent, the next call isn't made at all. gRPC has deadlines built in; over HTTP you can pass it in a header.

  2. Timeouts at every layer, getting smaller going down. Retries have to fit within the total time.

  3. A circuit breaker (Resilience4j): if 50% of calls to D fail, you don't call it at all for 30 s and return a fallback right away. D also gets room to breathe in the meantime.

  4. A bulkhead: a separate, bounded pool or semaphore for D (say, 20 parallel calls). If D slows down, only those 20 are tied up, and the other features keep working.

  5. Fallback and degradation: if the recommendations service is down, you show popular products from a cache. Agree with the business in advance which features are mandatory and which are "nice to have".

  6. Load shedding: when a service is saturated, return new requests immediately with 503 instead of choking them in a queue.

  7. Architecture: shorten the synchronous chain by pulling the data you need in advance through events and storing it locally, and make long jobs asynchronous.

Test all of this with chaos testing: add an artificial delay to D and see whether the system degrades the way you expect.