Search code examples
pollingazure-api-managementlong-pollingazure-durable-functions

Poll a Durable Function inside an Azure API Management request


Is it possible to do a short polling internally in an APIM request then respond with sync response?

The flow goes like this:
1. APIM receives a request
2. APIM triggers a durable function orchestration.
3. APIM waits and polls the durable function response (through a policy?)
4. APIM responds with the actual result

To the API Consumer, it will be a single request.

Is this a common pattern? Or is it better to just provide the 202 Response polling URL to the API consumer? I'm expecting the request to take only around a few seconds to under a minute.

Is there a more standard way of doing this that I haven't considered yet?


Solution

  • It is subjective to how you want your API to be presented to the consumers.

    1. If you want the front end users to poll the API then 202 Accepted response with a polling url in location header is the way.
    2. If you want to disguise your API as a synchronous API , you will have to implement the polling pattern itself inside the APIM policy. For this refer following post

    Use API-M To Mask Async APIs When Moving Implementation to Logic Apps, even though it pertains to logic app, you can implement the same pattern for any async API

    excerpt from the link:

    <outbound>
       <base />
       <retry condition="@(((IResponse)context.Variables["var"]).StatusCode == 202)" count="10" interval="30">
          <send-request mode="new" response-variable-name="var" ignore-error="false">
             <set-url>@(context.Response.Headers["location"][0])</set-url>
             <set-method>GET</set-method>
          </send-request>
       </retry>
       <return-response response-variable-name="var" />
    </outbound>
    

    As I pointed out earlier, it really depends upon how you want your API to be presented to your consumers. Both patterns are valid.