Retry
APPLIES TO: All API Management tiers
The retry
policy executes its child policies once and then retries their execution until the retry condition
becomes false
or retry count
is exhausted.
Note
Set the policy's elements and child elements in the order provided in the policy statement. Learn more about how to set or edit API Management policies.
Policy statement
<retry
condition="Boolean expression or literal"
count="number of retry attempts"
interval="retry interval in seconds"
max-interval="maximum retry interval in seconds"
delta="retry interval delta in seconds"
first-fast-retry="boolean expression or literal">
<!-- One or more child policies. No restrictions. -->
</retry>
Attributes
Attribute | Description | Required | Default |
---|---|---|---|
condition | Boolean. Specifies whether retries should be stopped (false ) or continued (true ). Policy expressions are allowed. |
Yes | N/A |
count | A positive number between 1 and 50 specifying the number of retries to attempt. Policy expressions are allowed. | Yes | N/A |
interval | A positive number in seconds specifying the wait interval between the retry attempts. Policy expressions are allowed. | Yes | N/A |
max-interval | A positive number in seconds specifying the maximum wait interval between the retry attempts. It is used to implement an exponential retry algorithm. Policy expressions are allowed. | No | N/A |
delta | A positive number in seconds specifying the wait interval increment. It is used to implement the linear and exponential retry algorithms. Policy expressions are allowed. | No | N/A |
first-fast-retry | Boolean. If set to true , the first retry attempt is performed immediately. Policy expressions are allowed. |
No | false |
Retry wait times
When only the
interval
is specified, fixed interval retries are performed.When only the
interval
anddelta
are specified, a linear interval retry algorithm is used. The wait time between retries increases according to the following formula:interval + (count - 1)*delta
.When the
interval
,max-interval
anddelta
are specified, an exponential interval retry algorithm is applied. The wait time between the retries increases exponentially according to the following formula:interval + (2^(count - 1)) * random(delta * 0.8, delta * 1.2)
, up to a maximum interval set bymax-interval
.For example, when
interval
anddelta
are both set to 10 seconds, andmax-interval
is 100 seconds, the approximate wait time between retries increases as follows: 10 seconds, 20 seconds, 40 seconds, 80 seconds, with 100 seconds wait time used for remaining retries.
Elements
The retry
policy may contain any other policies as its child elements.
Usage
- Policy sections: inbound, outbound, backend, on-error
- Policy scopes: global, workspace, product, API, operation
- Gateways: classic, v2, consumption, self-hosted, workspace
Examples
Request forwarding with exponential retry
In the following example, request forwarding is retried up to ten times using an exponential retry algorithm. Since first-fast-retry
is set to false
, all retry attempts are subject to exponentially increasing retry wait times (in this example, approximately 10 seconds, 20 seconds, 40 seconds, ...), up to a maximum wait of max-interval
.
<retry
condition="@(context.Response.StatusCode == 500)"
count="10"
interval="10"
max-interval="100"
delta="10"
first-fast-retry="false">
<forward-request buffer-request-body="true" />
</retry>
Send request upon initial request failure
In the following example, sending a request to a URL other than the defined backend is retried up to three times if the connection is dropped/timed out, or the request results in a server-side error. Since first-fast-retry
is set to true, the first retry is executed immediately upon the initial request failure. Note that send-request
must set ignore-error
to true in order for response-variable-name
to be null in the event of an error.
<retry
condition="@(context.Variables["response"] == null || ((IResponse)context.Variables["response"]).StatusCode >= 500)"
count="3"
interval="1"
first-fast-retry="true">
<send-request
mode="new"
response-variable-name="response"
timeout="3"
ignore-error="true">
<set-url>https://api.contoso.com/products/5</set-url>
<set-method>GET</set-method>
</send-request>
</retry>
Related policies
Related content
For more information about working with policies, see:
- Tutorial: Transform and protect your API
- Policy reference for a full list of policy statements and their settings
- Policy expressions
- Set or edit policies
- Reuse policy configurations
- Policy snippets repo
- Author policies using Microsoft Copilot in Azure