Chapter 20: Retries, Hedging, and Retry Policies

Automatic Retries (Service Config)

// Service config JSON (set on channel or via name resolver):
{
  "methodConfig": [{
    "name": [{"service": "mypackage.MyService"}],
    "retryPolicy": {
      "maxAttempts": 3,
      "initialBackoff": "0.1s",
      "maxBackoff": "1s",
      "backoffMultiplier": 2,
      "retryableStatusCodes": ["UNAVAILABLE", "DEADLINE_EXCEEDED"]
    }
  }]
}

Hedging (Speculative Retries)

Send the same request to multiple backends simultaneously. Use the first response, cancel the rest. Reduces tail latency at the cost of extra load.

// Hedging policy:
{
  "hedgingPolicy": {
    "maxAttempts": 3,
    "hedgingDelay": "0.5s"  // send next attempt after 500ms if no response
  }
}

Retry vs Hedging

AspectRetryHedging
WhenAfter failureBefore failure (speculative)
Extra loadOnly on failureAlways (sends duplicates)
Latency benefitOnly avoids total failureReduces p99 tail latency
RequirementIdempotent RPCsIdempotent RPCs
⚠️ Only Retry Idempotent Operations

Retrying a non-idempotent RPC (like "create order") can cause duplicates. Only enable retries for safe-to-repeat operations, or use idempotency keys.