Course contentsShow
← System Design
Lesson 1110 of 1,91928. Timeouts and DeadlinesFree lesson

Calculating Remaining Time

How each service computes its local timeout from the original deadline and elapsed time.

Calculating Remaining Time

What you'll learn: How each service in a distributed call chain computes its local timeout from the original deadline and elapsed time.

The Core Challenge

When a request travels through multiple services, each service needs to know how much time it has left to complete its work. Simply forwarding the original timeout value isn't enough—you must account for time already spent.

Think of it like a relay race where runners pass a stopwatch. Each runner needs to know how much time remains for the entire team to finish, not just restart their own timer from scratch.

How Services Calculate Remaining Time

Each service performs this calculation when receiving a request:

remaining_time = original_deadline - current_time

The process:

  1. Extract the deadline from incoming request context (header, metadata, etc.)
  2. Check the current time using your local clock
  3. Subtract to get remaining budget
  4. Use that budget for downstream calls and local operations

Example Flow

  • Client sets deadline: T₀ + 5000ms
  • Service A receives request at T₀ + 100ms
    • Calculates: 5000ms - 100ms = 4900ms remaining
    • Passes 4900ms budget to Service B
  • Service B receives at T₀ + 300ms
    • Calculates: 5000ms - 300ms = 4700ms remaining
    • Uses this for database call

Why Absolute Deadlines Matter

Services pass absolute deadlines (wall-clock timestamps), not relative timeouts. This prevents arithmetic errors and handles clock precision differences. Each service independently calculates remaining time from the same reference point.

Key Takeaway: Every service must calculate its remaining timeout budget by subtracting elapsed time from the original deadline—never blindly reuse the initial timeout value across service boundaries.