Video in progress

Strong Middle 🏗️ System design

What is deadline propagation, and why is it a problem when each service sets its own timeout independent of the chain?

A user request has an overall time limit (a deadline), and it is passed to each following call as the "remaining time". If each service waits 5 seconds on its own, work inside keeps going for a request the outer client has already given up on — wasting resources, and under load that chokes the system.

📖 Answer

The chain: gateway (2 s timeout) → order (5 s) → pricing (5 s) → DB.

Pricing slowed down. The gateway returned a 504 to the user after 2 seconds. But the order service is still waiting its 5 seconds, and pricing is running a heavy query in the DB. Nobody is waiting for the result, yet the threads, connections and DB resources are tied up. The user presses "retry" — another wave.

Deadline propagation: the outer layer sets the overall limit and passes it down. Each service computes the remaining time:

// incoming header: X-Deadline: 1730640000500 (epoch ms)
long remaining = deadlineMs - System.currentTimeMillis();
if (remaining <= 50) {
    throw new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT); // we don't even start
}
pricingClient.get(productId, Duration.ofMillis(remaining - 20));  // with a small margin

gRPC does this itself: the client sets a deadline, it travels to the following calls through metadata (grpc-timeout), and when it expires the context is cancelled on the server side.

The rules:

  • the inner timeout should always be smaller than the outer one;
  • check the deadline when you take a request off a queue too — there's no point working on a request that has already waited 3 seconds in the queue;
  • passing "remaining milliseconds" instead of an absolute time is less sensitive to clock differences between services.

This makes retries smarter as well: if the remaining time isn't enough for a second attempt, no retry is made.