Video in progress
How do you divide timeouts in a chain of synchronous calls, and what is deadline propagation?
Each lower layer's timeout must be smaller than the one above it, otherwise the layers below keep going with work the upper layer has already given up on. Deadline propagation means passing the remaining time budget along with the request, so each service knows how much time it has left.
📖 Answer
A typical mistake: the gateway timeout is 5 seconds, order-service's is 10 seconds, inventory-service's is 30 seconds. The gateway returned a 504 to the client after 5 seconds, but the layers below go on working for another 25 seconds: wasted resources, and it even makes a reservation that never gets cancelled.
The right order decreases going down:
gateway 5s -> order 4s -> inventory 1.5s
-> pricing 1.5s
If order-service makes two calls in sequence, their sum plus its own work has to fit in 4 seconds.
The problem with static values: a request may reach order after 3.8 seconds (queueing, GC), and it still gives inventory a full 1.5 seconds. Deadline propagation solves this: a "final deadline" or "remaining milliseconds" is passed along with the request.
In gRPC this is built in: the deadline automatically passes to the next calls. In HTTP there's no standard header, so you usually agree on one yourself:
long remaining = deadline.toEpochMilli() - System.currentTimeMillis();
if (remaining <= 50) {
throw new DeadlineExceededException(); // no point in trying
}
client.call(request, Duration.ofMillis(remaining - 50));
One more benefit: if a service pulls a request off the queue whose time has already run out, it can drop it without executing it at all. This speeds up recovery under load considerably.
Sign up to mark studied questions and save your progress.