Retries

SMG retries failed attempts with exponential backoff and jitter, so transient worker failures are absorbed by the gateway instead of reaching clients, without overwhelming workers that are recovering.


Overview

Automatic Retries

Retry a failed attempt without client intervention. For local workers, each retry runs worker selection again over the workers that are currently available.

Exponential Backoff

Space out retry attempts with increasing delays to give services time to recover.

Jitter

Add randomness to backoff timing to prevent thundering herd problems.

Smart Selection

Only retry status codes that are likely to succeed on another attempt, and never a response whose cause cannot clear within a backoff window. A worker's own 500 is definitive: retries avoid the worker that answered it.


Why Retries?

Transient failures are common in distributed systems:

  • Network timeouts: Temporary network congestion or packet loss
  • Worker overload: Temporary capacity limits (429 responses)
  • Intermittent errors: Brief service interruptions during deployments
  • Connection issues: Worker restart or network partition

Without retries, every transient failure becomes a client-visible error. With retries, SMG handles these automatically.


How a Retry Works

  1. SMG dispatches the attempt and checks the response status as soon as the worker answers, before any of the body reaches the client.
  2. If the status is retryable, the response is not marked terminal, and attempts remain, SMG waits for the backoff delay.
  3. The next attempt runs worker selection again. Workers that are unhealthy, have an open circuit breaker, or are vetoed by overload protection are skipped. So are the workers the request already failed on, as long as another available worker is left.
  4. When a response is not retried, or no attempts remain, SMG returns it to the client.

Skipping the workers a request failed on applies even under deterministic affinity: when cache_aware has the prompt's prefix cached on the failed worker, or a consistent_hashing routing key maps to it, the retry moves to another worker instead of going straight back. When every available worker has already been tried, including when the pool has a single worker, the retry selects among all of them again — except a worker that answered the request with its own 500, which is never selected for that request again (see When a Worker Answers 500).

The skip applies in PD mode too, on the HTTP and gRPC routers alike. When a pair's answer is retried, both of its workers count as tried, and the next attempt re-selects one side at a time: the prefill among the untried prefill workers, then the decode among the chosen prefill's untried partners.

On each side, the skip is a preference among the available workers with room for the request, not an exclusion: a tried worker with room is not passed over for an untried worker that has no room. A side with nothing untried left selects from its whole pool again, so a deployment with a single pair keeps retrying it.

A worker that answered the request with its own 500 is excluded outright (see When a Worker Answers 500). On the gRPC router, so is a worker whose leg was given up on as silent. A lone pair's 500 or silence-verdict 504 therefore goes back to the client instead of being retried.

The one exception to the skip is a request targeted with X-SMG-Target-Worker: it is retried on the worker it targets. A worker's own 500 overrides even this exception.

--retry-max-retries counts attempts, including the first: the default 5 allows the initial attempt plus up to four retries, and 1 disables retries.

A worker that has returned a success status and started streaming its response is never retried, even if the stream fails later.

HTTP-router responses relayed from a worker carry an x-smg-routed-worker-id header with the URL of the worker that produced them (the decode worker in PD mode), so a client can see where the final attempt landed.

Responses served by gRPC and ZMQ workers carry the same header when the final attempt succeeds, naming that attempt's worker by its registered grpc:// or ipc:// URL (the decode worker in PD mode). Their error responses, and Responses API responses, do not carry it. The Routed Worker Header section covers the header in detail, including version availability.

When a Worker Answers 500

A 500 a worker produced itself — its own response on the HTTP wire, or a gRPC INTERNAL status from the engine — is the worker's definitive answer for the request: replaying the same payload on the same worker costs the same work for the same answer. Retries of the request move to other available workers and never return to a worker that answered 500, however many attempts remain. The exclusion holds even when every other worker has already been tried, and for a request targeted with X-SMG-Target-Worker. It is also per request: the worker stays selectable for other requests, and its circuit breaker takes the request's one failure as usual.

In PD mode, on HTTP and gRPC workers alike, the exclusion covers only the worker that answered the 500, not the whole prefill/decode pair: the partner stays selectable, and the next attempt selects a pair again with another available worker of the failed worker's kind — prefill or decode. When no worker of that kind is left to take the request, the 500 goes back to the client as the worker's own answer, not as a no_available_workers refusal.

The HTTP PD router adds one refinement — the one case where a request returns to a worker that answered its own 500. A side's answer can be caused by the handoff with its partner, so when the worker that answered is the last of its side that can still form a pair, the retry excludes its partner instead and tries the worker again with another partner. Each such pair gets one attempt; when no pair can form either way, the 500 goes back.

When no other worker can take the attempt — the pool has a single worker, or every alternative is unavailable, unable to serve the request, or has itself answered 500 — the 500 is returned to the client at once, skipping the remaining attempts and their backoff. In regular mode the HTTP router relays the response as the worker sent it, body included; in PD mode the worker's status and Retry-After are kept, but the body is the gateway's error envelope naming the failed side, with the message from the worker's error body.

Only the worker's own 500 is definitive. The 502 the gateway returns when the worker's connection fails, a 500 the gateway synthesizes for a fault of its own, and a worker-sent 502, 503, or 504 all stay replayable on the same worker under the normal rules. The rule covers HTTP workers in regular and PD mode, and gRPC and ZMQ workers; for external providers, a 500 is retried like any other retryable status.

Where Retries Apply

Traffic Router retries
HTTP workers: JSON requests with a buffered body (regular and PD mode) Yes. Each retry selects a worker again, or a prefill/decode pair in PD mode
HTTP workers: JSON requests with a streamed body No. One attempt; see Retries and Request Bodies
HTTP workers: audio transcriptions No
gRPC and ZMQ workers: Chat Completions, Completions, /generate, Messages Yes. The request is prepared once (tokenization, tenant rate limiting); each retry selects workers again and re-sends the prepared request
gRPC and ZMQ workers: Responses, Embeddings, Classify No
External providers: OpenAI Chat Completions, Gemini Interactions Yes. OpenAI Chat Completions retries against the same provider endpoint

Exponential Backoff with Jitter

SMG uses exponential backoff with jitter to space out retry attempts:

base  = min(initial_backoff_ms * backoff_multiplier ^ n, max_backoff_ms)
delay = base * (1 + uniform(-jitter_factor, +jitter_factor))

n is 0 before the first retry, 1 before the second, and so on. The cap applies before jitter, and SMG refuses to start if --retry-jitter-factor is outside 0.0–1.0.

Example Progression

With default settings (5 attempts, so up to 4 retries):

Retry Before attempt Base delay With ±20% jitter
1 2 50 ms 40–60 ms
2 3 75 ms 60–90 ms
3 4 112 ms 90–134 ms
4 5 168 ms 134–202 ms

Why Jitter?

Without jitter, if multiple requests fail simultaneously, they all retry at exactly the same time—potentially overwhelming the recovering service. Jitter spreads out retries randomly to prevent this "thundering herd" problem.


Retryable Status Codes

SMG retries responses with these status codes. The list is global: per-worker resilience settings cannot add or remove codes from it.

Code Meaning Why Retryable
408 Request Timeout Temporary network issue
429 Too Many Requests Worker temporarily overloaded
500 Internal Server Error Transient server issue; retries avoid a worker that answered it
502 Bad Gateway Upstream temporarily unavailable
503 Service Unavailable Service temporarily down
504 Gateway Timeout Upstream timeout

SMG's own errors use these codes too. In regular HTTP mode, a failed connection to the worker — refused, reset or closed before the response head, or cut off mid-body while the gateway buffers the response — is a 502, and a connect timeout, or a worker connection the OS gives up on, is a 504; in PD mode a transport failure on either leg, those timeouts included, is a 502; on the gRPC router, a worker connection that fails under the call (a reset or a close with the call in flight, a GOAWAY of either side, a stream reset below gRPC, or a call the worker's side cancelled) is a 502. On the HTTP router (regular and PD) and the gRPC router, "no available workers" (every candidate unhealthy or circuit-open) is a 503 with Retry-After: 1, returned without a retry. The request's own timeout — --request-timeout-secs, or the worker's timeout_secs, running out — is a 504 in both modes, also returned without a retry (see Never Retried).

These 502s are the gateway's answer to a worker's transport failure, not the worker's own verdict on the request: the worker's circuit breaker counts them as failures, but they stay replayable under the normal rules, even on the same worker. A 500 the gateway synthesizes for a fault of its own stays replayable the same way. See When a Worker Answers 500.

Requests with other status codes (e.g., 400 Bad Request, 401 Unauthorized) are not retried because they would likely fail again.

Never Retried

Some responses carry a retryable status but go straight back to the client, because another attempt cannot succeed within a backoff window:

  • Worker overload sheds: 503 with error code worker_overload_protection_shed and a Retry-After header. The overload veto only changes when worker loads are polled again. See Overload Protection.
  • "No available workers": 503 with no_available_workers and Retry-After: 1, when every candidate worker is unhealthy or has an open circuit breaker. Availability returns on a passing health check or at the circuit breaker's timeout, never within a backoff window; the Retry-After paces client retries and is not a recovery estimate. See Interaction with Circuit Breakers.
  • Tenant rate-limit denials: 429 with tenant_rate_limit_exceeded. On every router that enforces the limit, a tenant's budget is reserved once per request, before dispatch, so a denial is returned with its Retry-After, and retry attempts never reserve again. See Tenant Rate Limiting.
  • Admission rejections: 429 admission_queue_full and 503 admission_queue_timeout come from the admission layer in front of the router and never reach the retry loop. See Rate Limiting.
  • Request timeouts: 504 when the request's own timeout (--request-timeout-secs, or the worker's timeout_secs) runs out. The timeout bounds how long the request may take, so a replay on another worker could only hold the client through a second timeout. A 504 from a connect timeout, or from a worker connection the OS gives up on, is the worker's failure, not the request's bound, and is retried.
  • A few other gateway errors that another attempt cannot fix are marked terminal the same way.

Configuration

smg launch \
  --worker-urls http://w1:8000 http://w2:8000 \
  --retry-max-retries 5 \
  --retry-initial-backoff-ms 50 \
  --retry-max-backoff-ms 30000 \
  --retry-backoff-multiplier 1.5 \
  --retry-jitter-factor 0.2

Parameters

Parameter Default Description
--retry-max-retries 5 Maximum attempts per request, including the first; 1 disables retries
--retry-initial-backoff-ms 50 Base delay before the first retry (milliseconds)
--retry-max-backoff-ms 30000 Cap on the base delay (milliseconds), applied before jitter
--retry-backoff-multiplier 1.5 Factor applied to the base delay for each further retry
--retry-jitter-factor 0.2 Random jitter factor (0.0-1.0) to prevent thundering herd
--disable-retries false Disable router retries (same as --retry-max-retries 1)

Per-Worker Overrides

The worker spec's resilience block accepts retry fields (max_retries, initial_backoff_ms, max_backoff_ms, backoff_multiplier, jitter_factor, and disable_retry), and since v1.12.0 they apply: the worker keeps the spec it was registered with, and the retry fields set that model's retry policy at registration; the --retry-* flags and --disable-retries remain the defaults. In v1.11.0 registration left them out of the stored spec, so no per-model policy was created and every request used the router flags.

The same block's circuit-breaker settings do apply, including two status-code lists that affect the circuit breaker, not retries: retryable_status_codes (statuses counted as circuit-breaker failures) and capacity_status_codes (statuses treated as capacity pushback). Narrowing either list never makes a status non-retryable. See Interaction with Circuit Breakers.


Latency-Sensitive

At most one retry, after a short delay.

smg launch \
  --retry-max-retries 2 \
  --retry-initial-backoff-ms 10 \
  --retry-max-backoff-ms 100

Use when: Real-time chat, interactive UIs

High-Availability

Up to two retries with doubling delays.

smg launch \
  --retry-max-retries 3 \
  --retry-initial-backoff-ms 100 \
  --retry-backoff-multiplier 2.0

Use when: Production APIs, multi-worker deployments

Batch Processing

Up to nine retries for offline workloads.

smg launch \
  --retry-max-retries 10 \
  --retry-initial-backoff-ms 100 \
  --retry-max-backoff-ms 60000 \
  --retry-backoff-multiplier 2.0

Use when: Batch inference, non-interactive pipelines

✗ No Retries

Disable retries entirely.

smg launch --disable-retries

Use when: Client handles retries, testing failure scenarios


Retries and Request Bodies

The HTTP router can only retry a request whose body it buffered. For each request it decides whether to buffer the body or stream it to the worker unread:

  • With retries enabled, a body that nothing else needs to parse is buffered up to --max-buffered-request-bytes (default 1 MiB) so it stays retryable. A larger body streams to the worker and gets a single attempt.
  • Requests the router must parse anyway, for example under the default cache_aware policy, always buffer and keep their retries.
  • With retries disabled, eligible bodies stream at any size.

Raise --max-buffered-request-bytes to keep larger requests retryable, at the cost of router memory. smg_router_request_body_path_total{reason="retry_forfeited"} counts requests that gave up their retries to stream. See Request Streaming and Upstream Connections for the full decision.

Pre-Response Resends

Separately from these retries, the HTTP router resends a request once, immediately, when the send failed before the worker produced any response, most often because the backend had already closed the pooled connection. The resend uses no backoff and no retry attempt, happens even with --disable-retries, and is counted in smg_router_upstream_send_retries_total. Timeouts, streamed bodies, and upstream bodies of 1 MiB or more are not resent. See Resending Pre-Response Failures.


Interaction with Circuit Breakers

Retries and circuit breakers meet in worker selection:

  • Open circuits are skipped. Every attempt, including the first, chooses only among workers whose circuit is closed or half-open, so a retry never goes to a worker whose circuit has opened in the meantime. A half-open circuit does not throttle traffic: its worker is as selectable as one with a closed circuit.
  • "No available workers." If every candidate is unhealthy or circuit-open, the request fails with 503 no_available_workers, carrying Retry-After: 1, and the gateway does not retry it internally (Never Retried) — on the HTTP router (regular and PD) and the gRPC router alike, whether it happens on the first attempt or a later one. An open circuit moves to half-open only after --cb-timeout-duration-secs or when its worker is promoted back to Ready, and an unhealthy worker returns only when a health check passes — never within a backoff window, so backing off in-process would only delay the same refusal.
  • A failed request charges a worker once. Every attempt's success is recorded, but however many of a request's attempts fail on the same worker, that worker's breaker takes one failure: --cb-failure-threshold counts failed requests, not attempts. This covers HTTP workers in regular mode and gRPC and ZMQ workers; for HTTP workers in PD mode, for external providers, and on every path in v1.12.0 and earlier, each failed attempt counts separately.
  • Capacity pushback is retried but not counted. A 429 (by default) is retried, but records neither a failure nor a success on the breaker, so a busy worker's circuit does not open under load. Per worker, resilience.capacity_status_codes replaces that list, for example to treat 503 as pushback. Whether a capacity status is retried still follows the global retryable list.
  • Client-caused aborts are not counted. Aborted streamed uploads (408, 413, 400) say nothing about the worker.

See Circuit Breakers for thresholds and state transitions.


Monitoring

Metrics

Metric Type Labels Description
smg_worker_retries_total Counter worker_type, endpoint Retries performed, one per backoff. PD mode counts each retry under both prefill and decode
smg_worker_retries_exhausted_total Counter worker_type, endpoint Requests whose last attempt still returned a retryable status; also counted when retries are disabled
smg_worker_retry_backoff_seconds Histogram attempt Backoff delay before each retry; attempt is the retry number, 1 for the first retry. Buckets of its own, from 10 ms to 60 s
smg_router_upstream_send_retries_total Counter router_type Immediate resends after a pre-response transport failure

worker_type is regular, prefill, or decode for local workers and external for the OpenAI provider router.

Useful PromQL Queries

Retry Rate

# Retries per second
sum(rate(smg_worker_retries_total[5m]))

# Requests that ran out of attempts, per second
sum(rate(smg_worker_retries_exhausted_total[5m]))

Backoff Distribution

# Average backoff delay
sum(rate(smg_worker_retry_backoff_seconds_sum[5m]))
  / sum(rate(smg_worker_retry_backoff_seconds_count[5m]))

# 99th percentile backoff per retry number
histogram_quantile(0.99, sum by (attempt, le) (rate(smg_worker_retry_backoff_seconds_bucket[5m])))

What to Watch

Signal Query What it suggests
Retry rate rising sum(rate(smg_worker_retries_total[5m])) Workers are returning retryable errors; check worker health and circuit breaker state
Exhausted retries sum(rate(smg_worker_retries_exhausted_total[5m])) Failures are reaching clients despite retries
Long backoffs histogram_quantile(0.99, sum by (le) (rate(smg_worker_retry_backoff_seconds_bucket[5m]))) Requests spend noticeable time waiting between attempts
Retries forfeited sum(rate(smg_router_request_body_path_total{reason="retry_forfeited"}[5m])) Large bodies are streaming with a single attempt
Pre-response resends sum(rate(smg_router_upstream_send_retries_total[5m])) Pooled connections are going stale; check --upstream-pool-idle-timeout-secs

Tuning Guidelines

Symptom Potential Adjustment
Excessive latency from retries Reduce --retry-max-retries, decrease backoff times
Thundering herd on recovery Increase --retry-jitter-factor
Retries exhausted too quickly Increase --retry-max-retries, --retry-max-backoff-ms
Clients seeing too many errors Increase retry count, check worker health
Large requests fail without a retry Raise --max-buffered-request-bytes so they stay buffered and retryable
Steady pre-response resends Lower --upstream-pool-idle-timeout-secs below the backend's keep-alive timeout

What's Next?

Circuit Breakers

Isolate failing workers to prevent cascade failures.

Circuit Breakers →

Health Checks

Proactive worker monitoring and failure detection.

Health Checks →

Rate Limiting

Protect workers from overload with token bucket rate limiting.

Rate Limiting →

Request Streaming

Which request bodies stream, and the upstream connection settings behind retries.

Request Streaming →