How Two Three-Attempt Limits Become Nine Calls
Review generated retry patches for nested loops, missed debits at timer fire, and shipment identity that must survive lost replies and worker replacement.
In a fictional shipment service, a generated patch puts attemptsLeft = maxAttempts inside the stock client method. Every gateway replay resets that counter. If the gateway times out while the original handler is still running, the replacement can start spending a full allowance before the original worker finishes.
Put the allowance in a durable ledger keyed by shipment intent. The ledger bounds the stock calls a replacement can authorize when an earlier send has an unknown outcome. A separate budget caps retry traffic to each shard across all shipments. A retry spends shard credit when its timer fires, even when the ledger still has attempts left.
Who gets to send again?
In Figure 1, the handler must complete the stock check before calling the carrier. Each retrying layer allows three total attempts, including the first, and the transport adds no retries of its own. The shard keeps returning a retryable failure, the gateway's policy retries the handler's final error, and every attempt fits within the deadline and available shard credit. The model omits the attempt ledger. With both retry policies disabled, the shard sees one call. Either policy on its own produces three. With both enabled, each of the gateway's three handler invocations reaches the shard three times, for nine calls.
Figure 1 — Illustrative counts, not measured data. The stock shard sees one call with retries off, three with either layer retrying, and nine when the gateway starts a fresh stock loop on every attempt.
Trace the loop from the gateway's route policy to the SDK's transport settings. If the route treats the stock client's final error as retryable, the gateway starts the entire handler again. Let the handler own retries of the stock read so each retry skips repeated authorization and unrelated calls. Limit gateway retries to failures that establish the handler never ran, such as a refused direct connection before sending request bytes. RFC 9113 §8.7 gives two signals on a direct HTTP/2 hop to the handler. The peer either resets the stream with REFUSED_STREAM or sends a GOAWAY with a last-stream ID lower than the request's stream ID. A timeout after dispatch leaves execution uncertain.
In the repaired policy for that shipment, cap stock attempts at three across all handler invocations. Before dispatch, the gateway registers the reservation in the ledger under the stable shipment identity. A transport retry after proven non-execution carries the same reservation. Each stock send requires a durable debit, and the ledger serializes that debit with any transfer of ownership. A crash between debit and dispatch can cost an unused attempt. Charging only after the reply would erase ambiguous sends from the accounting. To hand off an unfinished shipment, invalidate the original worker's authority to obtain new debits and transfer only the undebited balance in the same ledger transaction. An already authorized send stays charged even if the old worker dispatches it after the transfer. Queue redelivery must claim its allowance through the same ledger.
Withhold the original worker's reply after it has spent part of its allowance. Transfer ownership and attempt a further debit from each worker. Verify that only the replacement can acquire new debits and that it inherits the remaining balance.
The gRPC A6 client retry specification treats negative or unparseable grpc-retry-pushback-ms as a stop signal. If the HTTP gateway maps a gRPC UNAVAILABLE response to HTTP 503 and drops negative pushback, a blanket 503 retry rule can replay the handler after the server asked it to stop. Preserve the stop decision across the translation and test the route with enough time and credit left to expose an unintended replay.
HTTP Retry-After carries either delay-seconds or an HTTP-date. RFC 9110 §10.2.3 describes its use with 503 and 3xx responses, and RFC 6585 §4 allows it on 429. For an otherwise eligible retry, treat the requested wait as a floor. If it pushes the next attempt past the shipment's usable time window, return the failure.
Keep retries from consuming the shard's recovery
When the shard starts accepting requests again, shipments waiting on backoff timers can compete with fresh stock checks. A connection permit returns when its lease ends. Retry credit is spent on a send and replenished by policy, even if that send produces no reply. Fund and spend it against the same shard across its callers so healthy shards cannot bankroll repeated sends to the one draining its queue. If a circuit breaker admits a half-open probe to test recovery, count its underlying sends against the probe allowance. An SDK that retries the probe internally can send several stock checks under one permit, and a successful retry can hide the failed first attempt from the breaker.
Google's Site Reliability Engineering, in “Handling Overload,” describes a per-client ratio budget alongside a per-request cap. For the shipment service, consider an illustrative policy that allows up to eight retry starts per hundred initial stock dispatches to the shard in the same accounting window. If customers resubmit stalled shipments and those requests reach the shard as fresh operations, they create more retry credit even if the shard never succeeds. Reusing the shipment identity lets the service recognize those resubmissions before another dispatch earns credit.
Under gRPC A6 retry throttling, each successful RPC adds tokenRatio to a client balance capped at maxTokens, and qualifying failures debit it. At or below half of maxTokens, the throttle blocks policy-driven retries. First attempts still go out, and their successes refill the balance during recovery. The client tracks the balance per server name, so successes on healthy shards behind the same name can mask trouble on the hot shard. Enforce the shared limit where routing has identified the destination shard.
In a token bucket that charges one token per retry start, a fixed refill sets the sustained retry rate, and bucket capacity sets the ceiling on an immediate burst. Cap banked credit in a traffic-funded bucket too. Otherwise healthy traffic before the outage can fund a burst just as the shard begins rejecting work. If workers hold local slices of a fixed-rate bucket, carve a joining worker's slice out of existing allocations. Reduce the old workers' refill rates and capacities before activating the new slice, and transfer only unspent credit that the old slices can no longer spend. That keeps both the aggregate refill rate and the burst capacity fixed as workers join.
Debit at timer fire. Find where the patch debits the budget relative to the timer. A read before sleeping only avoids a pointless wait. Several timers can all read the same last token. After the timer fires, check cancellation and the remaining attempt window, then check and debit shard credit atomically as part of admitting the retry. If admission fails, return the failure so old timers cannot pile up behind the refill. Keep the debit next to dispatch. If admission itself blocks long enough to consume the attempt window, check it again before sending. Account for transport resends at the same boundary or disable them.
Make competing timers observe the same available token before they sleep, then wake them together with that token still available and no refill. Give both shipments enough time and their own ledger allowance. Count arrivals at the shard. Exactly one retry should reach it.
Test the delay the patch will actually wait
A generated loop can check the nominal backoff against the deadline and sample jitter on the next line. Review those lines together. Sample first, include any server-requested minimum wait, then budget for the scheduled wait, the next attempt, and time to return the result. At timer fire, check again. Scheduler delay can consume the attempt window while the process is waiting to run.
Figure 2 models the stock check under an illustrative policy that requires a full 150-millisecond retry window and a 30-millisecond reserve to return the stock result to the handler. At entry, the stock stage has 450 milliseconds of the shipment's deadline after the handler sets aside time for the carrier. The initial attempt uses 120 milliseconds, leaving 330. The first retry's delay draw is 80 milliseconds. With no server-requested wait, it needs 260 milliseconds and can be scheduled. In this model, the timer fires after exactly 80 milliseconds and dispatch takes no additional time. The retry fails at the end of its 150-millisecond window, leaving 100 milliseconds. The next draw is 160 milliseconds, bringing the requirement to 340. The loop skips that retry before arming a timer.
Figure 2 — Illustrative timing, not measured data. The first retry needs 260 of the 330 milliseconds left. The next needs 340 with only 100 left, so the client never arms that timer.
A library timeout can begin anew each time the wrapper calls it. Derive that timeout from the ingress deadline as well as the per-attempt cap, and propagate cancellation where supported. If short attempts can still do useful work, give them an explicit minimum window. Verify downstream concurrency separately. A locally serial loop can overlap work on the server when a timed-out request is still executing there.
Keep the idempotency key with the intent
A gateway can replay the same HTTP headers while a redelivered queue message runs with a different delivery identifier. Assign the idempotency key when the shipment intent is created and persist it in the message. A worker that derives the key from the delivery identifier defeats deduplication across redeliveries. At the receiving service, bind the key to the caller and operation, reject a conflicting payload, and retain the record through the longest permitted replay window. Concurrent requests with the same key must share one execution. If the receiver owns the shipment record, commit it with the deduplication result so a crash cannot leave a created shipment behind an apparently unused key. If the receiver calls an external carrier, its local transaction cannot close that gap. It needs carrier-side deduplication or evidence from reconciliation before submitting again.
Drop the carrier's reply after shipment creation, then redeliver the message under a different delivery identifier. For a carrier with durable deduplication, verify that the replacement presents the original key and the carrier records a single shipment. For a carrier without it, verify that the worker resolves the prior submission before sending another. If neither a lookup nor reconciliation evidence can establish the outcome, leave the submission unresolved.
What a replacement worker must inherit
Losing a reply changes what the worker knows about the shipment. Every replay inherits the same intent key, the debits already recorded, and the original deadline. The replacement earns another stock send only when the ledger has an attempt left, the deadline still leaves time to use the result, and the destination shard grants credit at dispatch.