For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
Request retries
Verified Code examples on this page have been automatically tested and verified.Set up retries for requests.
Specify the number of times and duration for the gateway to try a connection to an unresponsive backend service. You might commonly use retries alongside Timeouts to ensure that your apps are available even if they are temporarily unavailable.
About request retries
A request retry is the number of times a request is retried if it fails. This setting can be useful to avoid your apps from failing if they are temporarily unavailable. With retries, calls are retried a certain number of times before they are considered failed. Retries can enhance your app’s availability by making sure that calls don’t fail permanently because of transient problems, such as a temporarily overloaded service or network.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
Step 1: Set up request retries
Set up retries to the sample app.
Install the experimental Kubernetes Gateway API CRDs.
kubectl apply --server-side -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.5.0/experimental-install.yamlCreate your retry rules. You can choose to apply a retry on an HTTPRoute by using the Kubernetes Gateway API-native approach, or to add a retry to a specific HTTPRoute rule or Gateway listener by using an AgentgatewayBackend resource.
Create an HTTPRoute that routes requests along the
retry.exampledomain to the sample app.kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: retry namespace: httpbin spec: hostnames: - retry.example parentRefs: - name: agentgateway-proxy namespace: agentgateway-system rules: - matches: - path: type: PathPrefix value: / backendRefs: - name: httpbin port: 8000 retry: attempts: 3 backoff: 1s codes: - 500 - 503 EOFReview the following table to understand this configuration.
Field Description hostnamesThe hostnames to match the request, such as retry.example.parentRefsThe gateway to which the request is sent. In this example, you select the agentgateway-proxygateway that you set up before you began.rulesThe rules to apply to requests. matchesThe path to match the request. In this example, you match any requests to the sample app with path prefix /.pathThe path to match the request. backendRefsThe backend service to which the request is sent. In this example, you select the httpbinservice that you set up in before you begin.retry.attemptsThe number of times to retry the request. In this example, you retry the request 3 times. retry.backoffThe duration to wait before retrying the request. In this example, you wait 1 second before retrying the request. retry.codesThe HTTP status codes for which the gateway retries the request. In this example, the request is retried if the backend returns 500 or 503. Verify that the gateway proxy is configured to retry the request.
Port-forward the gateway proxy on port 15000.
kubectl port-forward deploy/agentgateway-proxy -n agentgateway-system 15000Find the route configuration for the cluster in the config dump. Verify that the retry policy is set as you configured it.
Example
jqcommand:curl -s http://localhost:15000/config_dump | jq '[.binds[].listeners | to_entries[] | .value.routes | to_entries[] | select(.value.name == "retry" and (.value.inlinePolicies[]? | has("retry"))) | .value] | .[0]'Example output:
http://localhost:15000/config_dump1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37... { "key": "httpbin/retry.0.0.http", "name": "retry", "namespace": "httpbin", "hostnames": [ "retry.example" ], "matches": [ { "path": { "pathPrefix": "/" } } ], "backends": [ { "weight": 1, "service": { "name": "httpbin/httpbin.httpbin.svc.cluster.local", "port": 8000 } } ], "inlinePolicies": [ { "retry": { "attempts": 3, "backoff": "1s", "codes": [ "500 Internal Server Error", "503 Service Unavailable" ] } } ] }
Send a request to the sample app. Verify that the request succeeds.
curl -vi http://$INGRESS_GW_ADDRESS:80/headers -H "host: retry.example"Example output:
... < HTTP/1.1 200 OK ...Verify that the request was not retried.
kubectl logs -n agentgateway-system \ -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \ --tail=1 | grep -E 'retry.example'Example output:
info request gateway=agentgateway-system/agentgateway-proxy listener=http route=httpbin/retry endpoint=10.244.0.13:8080 src.addr=127.0.0.1:34300 http.method=GET http.host=retry.example http.path=/headers http.version=HTTP/1.1 http.status=200 protocol=http duration=0ms
Step 2: Trigger a retry
Simulate a failure for the sample app so that you can verify that the request is retried.
Send another request to the httpbin app along the
/status/500path. This path returns a 500 HTTP response code. Because the agentgateway proxy is configured to retry a request when a 500 HTTP response code is received, the proxy starts retrying the request.curl -vi http://$INGRESS_GW_ADDRESS:80/status/500 -H "host: retry.example:80"Example output:
... < HTTP/1.1 500 Internal Server Error ...Verify that the request was retried. Look for
retry.attempt=3in the output.kubectl logs -n agentgateway-system \ -l gateway.networking.k8s.io/gateway-name=agentgateway-proxy \ --tail=1 | grep -E 'retry.example'Example output:
info request gateway=agentgateway-system/agentgateway-proxy listener=http route=httpbin/retry endpoint=10.244.0.21:8080 src.addr=127.0.0.1:59284 http.method=GET http.host=retry.example http.path=/status/500 http.version=HTTP/1.1 http.status=500 protocol=http retry.attempt=3 duration=1ms
Cleanup
You can remove the resources that you created in this guide.Delete the HTTPRoute resource.
kubectl delete httproute retry -n httpbinIf you created an AgentgatewayPolicy, delete it from the namespace you created it in.
kubectl delete AgentgatewayPolicy retry -n httpbin kubectl delete AgentgatewayPolicy retry -n agentgateway-system