Handle the HTTP 500 Response in APIM

Handle the HTTP 500 Response in APIM

APIM Add Retry Policy using Policy Fragment

A connection that was expected to be kept alive was closed by the server

We were seeing many API responses with status code 500 in APIM, with Exception type = BackendConnectionFailure:

The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.

Upon investigating further, Microsoft suggest implementing a retry policy: Scenario 5: Unable to connect to the remote server

The Fragment

<fragment>
    <retry condition=“@(context.Response.StatusCode == 500)”
count=“5” interval=“10” max-interval=“50” delta=“10” first-fast-retry=“false”>
        <forward-request buffer-request-body=“true” />
    </retry>
</fragment>

The API Policy

<policies>
    <inbound>
        <base />
    </inbound>
    <backend>
        <!–<base />–>
        <include-fragment fragment-id=“StandardRetry” />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>

Leave a Reply

Your email address will not be published. Required fields are marked *