Retries, backoff and idempotency
A retry repeats an operation after an eligible failure. Backoff spaces attempts to avoid overloading a struggling service. A deadline, a maximum attempt count and a retry classification must be defined together. Do not retry every nonzero status: invalid credentials, malformed data and permanent errors usually require correction, not more requests.
Retrying safely
HTTP idempotency concerns the intended effect of repeating a request, not identical responses. GET is defined as safe and idempotent by HTTP semantics, but real APIs can still have side effects or rate limits. PUT and DELETE are defined as idempotent methods; a POST is not inherently idempotent. A lost response does not prove that a server performed no action. Do not automatically repeat non-idempotent operations unless the service provides a verified deduplication contract or you can establish the first operation was not applied. An idempotency key is useful only with a documented server implementation.
For the local read-only endpoint, retry only HTTP 503 and at most three total attempts. Each request has its own timeout; the sample stops on transport failure and never sleeps in the mock demonstration. Real retries should follow documented Retry-After (seconds or HTTP date), cap delays, add jitter, and enforce an end-to-end deadline. A 429 may be eligible under service policy; 401/403 normally require credential or permission correction. curl's --retry counts retries after the initial attempt, not total attempts; --retry-max-time is not an absolute bound on a final in-progress request. Avoid streaming partially retried bodies into an append-only pipeline. The fixtures keep body and status separate. The successful mock response must contain exactly one JSON object with boolean ok equal to true. A successful last value in a stream is not sufficient. This drill uses only a read-only local endpoint.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. HTTP examples require the included lab-fixtures/v25/mock_api.py server: run python3 on that file in a separate terminal, export BM_API_BASE with its printed loopback URL, and stop the server with Ctrl-C when done. Use a fresh server for each HTTP example. curl and jq are external dependencies.
Observe two failures and success
umask 077[[ ${BM_API_BASE-} =~ ^http://127\.0\.0\.1:[0-9]{1,5}$ ]] || exit 2for ((attempt=1; attempt<=3; attempt++)); do code=$(curl -q --noproxy "*" --proto "=http" -sS --connect-timeout 1 --max-time 2 -o body.json -w '%{http_code}' "$BM_API_BASE/retry") || exit 1 printf 'attempt=%s http=%s\n' "$attempt" "$code" case $code in 200) jq -es 'length==1 and (.[0] | type=="object" and .ok==true)' body.json >/dev/null || exit 2; exit 0;; 503) ;; # lab: no real delay *) exit 2;; esacdoneexit 1Expected stdout:
attempt=1 http=503attempt=2 http=503attempt=3 http=200A fresh mock server provides two 503s, then success. A production client must also enforce a global deadline and pacing.
Plan bounded delays without sleeping
delay=1for ((attempt=1; attempt<=5; attempt++)); do printf 'retry=%s delay=%s\n' "$attempt" "$delay" delay=$((delay*2)) ((delay<=4)) || delay=4doneExpected stdout:
retry=1 delay=1retry=2 delay=2retry=3 delay=4retry=4 delay=4retry=5 delay=4This computes a capped schedule only. Actual delays, jitter and Retry-After handling are not executed here.
Classify a fixed response sequence
for code in 503 429 401 400 200; do case $code in 503|429) decision=consider-retry;; 200) decision=validate-body;; *) decision=stop;; esac printf '%s %s\n' "$code" "$decision"doneExpected stdout:
503 consider-retry429 consider-retry401 stop400 stop200 validate-bodyThis policy is a teaching example for read-only requests, not permission to retry every 503 or 429.
Common mistakes
Three retries may mean four requests. Never assume a missing response means no mutation occurred. Avoid retrying permanent authentication errors.
Practice
Classify 503, 429, 401 and a timeout after a POST. Specify a maximum attempt count, a total deadline and a policy for ambiguous mutations without contacting a real API.
Next: API credentials and TLS verification