liveBook · Manning

Retry

As explained in gRPC Microservices in Go

Retry is the repeated execution of a failed operation according to specified conditions or limits.

Retry repeats a failed operation when the failure may be temporary. In a microservice system, a consumer can retry a gRPC request when a dependent service is unavailable, giving communication another chance to succeed. The decision must depend on the response and the operation, not merely on the fact that a call failed. section 6.4 section 5.0

First check whether repeating the operation is safe. An idempotent operation has no additional effect when called more than once. For example, if a response to a user deletion is lost in a network failure, repeating an idempotent deletion does not change the resource again. Where idempotency is unsuitable, return validation errors that tell the client when to stop retrying. section 1.1.3

Set the retry policy around the failure and the time the caller can afford to wait:

  • Retry selectively. gRPC status codes such as Unavailable or ResourceExhausted can justify another attempt; a validation error caused by an invalid payload calls for correction instead. section 6.4 section 6.2.2
  • Limit waiting with a context timeout or deadline. Retry blocks the gRPC call while the client waits for its dependency, and recovery time may be hard to predict. section 6.4
  • Consider a circuit breaker when repeated attempts burden an unready service. After a failure limit, it can stop calls temporarily and try again later. section 6.4

Order and Shipping service failure

If the Shipping service cannot reach its database, shipping calls from the Order service can fail and potentially contribute to a cascading failure. Time out the shipping operation or fail fast and retry later, rather than leaving dependent calls waiting indefinitely and impairing unrelated requests. section 6.1

ctx, cancel := context.WithTimeout(context.Background(), 
10 * time.Second)
defer cancel()                                                #1
payment := pb.NewPaymentClient(conn)
result, err := payment.Create(ctx, &CreatePaymentRequest{})   #2
if err != nil {
    // handle Payment create an error
}

A Go gRPC Payment client passes a context with a ten-second timeout to Create. The listing shows a time limit for a call, not a complete retry loop or retry policy. section 2.5.3

Structured errors make the choice actionable: a client can inspect gRPC status and error details to distinguish a condition worth retrying from a request that needs new input. Retry works alongside timeouts and circuit breakers; it does not replace them. section 6.2 section 6.4

How gRPC Microservices in Go covers it

How deep it goes
Explains retry selection, idempotency, waiting limits and circuit-breaker trade-offs, but does not supply a complete retry-policy implementation.
Examples
Illustrates retry decisions with a lost user-deletion response, an Order-to-Shipping dependency failure and gRPC error statuses.
What it assumes
Assumes familiarity with service-to-service calls and gRPC clients; introduces the idempotency and error distinctions needed to choose retries.
What you can do after
Apply it via a gRPC client policy that retries eligible failures while bounding waits and avoiding invalid-request retries.
How it connects
Links retry to idempotency, gRPC status codes, validation errors, context deadlines, fault tolerance and circuit breakers.

Also explained in

Compare how 21 books explain retry →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of gRPC Microservices in Go. The section links point to the parts of the book it draws on.