System Design Fundamentals latency-budget deadline-propagation api-fan-out

How to Budget Latency for Parallel API Calls

Bookmark 0

When an API calls several services in parallel, its response time depends on which results it waits for. A latency budget sets limits on that wait and reserves time to assemble and return the response.

The worked example uses a shipment API with a 250 ms deadline, a required delivery estimate, and optional tracking events. It compares three tracking policies to show how often each returns tracking and how much time it adds.

Table of Contents

Calculate the deadlines at dispatch

All times below are milliseconds since the API accepts the request. The example reserves 30 ms for response assembly and transmission. The downstream work must finish by 250 − 30 = 220 ms. With a 120 ms tracking cutoff, the budgets at dispatch are:

Authorization endsEstimate budgetTracking budget
54 ms220 − 54 = 166 ms120 − 54 = 66 ms
130 ms220 − 130 = 90 msSkip the call
222 msReturn a deadline errorSkip the call

At 54 ms the estimate and tracking calls start together, so the 66 ms tracking window sits inside the 166 ms estimate window. If the caller only has 140 ms left at admission, the downstream deadline moves to 140 − 30 = 110 ms. Tracking uses the earlier of its cutoff and that deadline, so each branch has at most 110 − 54 = 56 ms. Recalculate remaining time before each outbound call and count connection-pool wait against it.

Work through four requests

With the 120 ms cutoff, the API waits until both the estimate is ready and tracking has either returned or reached its cutoff. A missing estimate at 220 ms produces an error. The table assumes authorization ends at 54 ms and response assembly and transmission take the full 30 ms.

Estimate readyTracking readyJoin decisionResponse completes
85 ms112 msJoin at 112 ms with tracking. The estimate waits 27 ms.142 ms
90 msWould finish at 138 msJoin at 120 ms and cancel tracking. The estimate waits 30 ms.150 ms
140 ms110 msJoin at 140 ms with tracking. Tracking adds 0 ms.170 ms
Still pending at 220 ms90 msFail at 220 ms and cancel the estimate.250 ms

The second response carries tracking_status: "unavailable". A successful tracking response with zero events carries tracking_status: "available" and an empty list. The client can then show “Tracking temporarily unavailable” without claiming the shipment has no events.

Compare tracking cutoffs on 10,000 requests

Suppose the product requires tracking on at least 89% of successful summaries and allows at most 35 ms of added wait on 95% of them. Use the estimate-ready time as the baseline for each request. The figures below use synthetic timing groups, with 9,800 successful estimates and 200 estimate deadline failures.

PolicySuccessful summaries with trackingMean added waitP95 added wait
Return when the estimate is ready7,000 / 9,800 = 71.43%0 ms0 ms
Wait for tracking until 120 ms8,800 / 9,800 = 89.80%7.41 ms30 ms
Wait for tracking until 145 ms9,600 / 9,800 = 97.96%8.88 ms48 ms

The 120 ms cutoff gains tracking on 1,800 responses whose estimates arrive at 85 ms and tracking arrives at 112 ms. Another 800 requests spend 30 ms waiting and still miss tracking. The mean is (1,800 × 27 + 800 × 30) / 9,800 ≈ 7.41 ms. The 145 ms cutoff recovers those 800 results, but their wait rises to 138 − 90 = 48 ms and pushes P95 past the 35 ms target. Of the three policies, only 120 ms meets both targets.

Replay the cutoffs in Python

Run this with Python 3. Each row groups requests with the same outcome. The tracking column records when tracking settled, even if it settled with an error. The calculation uses the P95 of each request's added wait.

from math import ceil

# Count, estimate ready (ms), tracking settled (ms), tracking usable.
groups = [
    (4_000, 100, 80, True),
    (3_000, 140, 110, True),
    (1_800, 85, 112, True),
    (800, 90, 138, True),
    (200, 180, 77, False),  # Tracking returned an error.
    (200, None, 90, True),  # Required estimate missed its deadline.
]

for cutoff in (None, 120, 145):
    covered = 0
    extra_wait = []
    for count, estimate, tracking, usable in groups:
        if estimate is None:
            continue
        # With no optional wait, use tracking only if ready at the join.
        tracking_limit = estimate if cutoff is None else cutoff
        if usable and tracking <= tracking_limit:
            covered += count
        join_at = max(estimate, min(tracking, tracking_limit))
        extra_wait.extend([join_at - estimate] * count)

    extra_wait.sort()
    n = len(extra_wait)
    p95 = extra_wait[ceil(0.95 * n) - 1]
    print(cutoff, f"{covered / n:.2%}", f"{sum(extra_wait) / n:.2f} ms", f"{p95} ms")

For production comparisons, collect estimate-ready time, the tracking outcome and settle time, and the actual join time per request. Calls cancelled at 120 ms reveal nothing about whether a usable result would have arrived by 145 ms. Measure the longer cutoff with a small live canary under the same outer deadline. Watch estimate deadline failures as well as coverage. Longer tracking calls can compete with estimates for capacity, an effect the replay holds fixed.

Check whether a retry fits

Suppose the estimate read returns a transient error at 163 ms. With 8 ms of backoff, 220 − 163 − 8 = 49 ms remains for a second attempt. If load testing puts the minimum useful attempt at 40 ms, the retry fits. The same error at 183 ms leaves 220 − 183 − 8 = 29 ms, so the API returns the dependency error without launching another call.

Keep the retry under the original 220 ms deadline, including connection-pool time. On deadline expiry or caller cancellation, cancel outstanding reads. The downstream service must observe cancellation to stop its own work. See Timeouts, Retries, and Exponential Backoff for the retry policy.

Conclusion

Key Takeaway. Reserve the return time first. Then choose the tracking cutoff by counting how many responses gain a usable result and how long each request waits after its estimate is ready. In the example, 120 ms delivers 89.80% tracking coverage with 30 ms of P95 added wait.

Comments (0)

More articles from this author

View all