Azure API Management (APIM) offers powerful capabilities beyond just managing APIs. This video, “Azure APIM Load Balancing Explained: A Step-by-Step Guide,” explores how to leverage APIM’s inbound policies to achieve simple backend load balancing and detect large files.

In the video, I walk through setting up the inbound policy to route messages between two endpoints using a random number.

Plus, it takes a look at how to route larger messages, based on Content-Length, to a separate workflow for processing.

While this demo shows load balancing between two different backend Azure Logic App Standard deployments, it can be used for any backend.

Below is the code sample used in the video to route messages using an inbound policy.  The comments include more details on the specifics of each line.

APIM Inbound Policy Load Balancing and Large File Detection

<inbound>
<base />
  <!-- This creates a random number to use for routine to one of the two backends -->
  <!-- Returns a number eqeual or great than 1 and less than 3 -->
  <set-variable name="urlId" value="@(new Random(context.RequestId.GetHashCode()).Next(1, 3))" />
  <choose>
	<!-- Route to endpoint 1 -->
	<when condition="@(context.Variables.GetValueOrDefault<int>("urlId") == 1)">
	  <!-- Can set to a Endpoint, a Load Balancer, or a direct URL-->
	  <set-backend-service backend-id="Endpoint1-Policy" />
	  <!-- Set the actual web service URL with any query string parameters -->
	  <rewrite-uri template="/Orders/../triggers/invoke?api-version=2022-05-01..." />
	</when>
	<!-- Route to endpoint 2 -->
	<when condition="@(context.Variables.GetValueOrDefault<int>("urlId") == 2)">
	  <!-- Can set to a Endpoint, a Load Balancer, or a direct URL-->
	  <set-backend-service base-url="https://las-yourname.azurewebsites.net:443/api" />
	  <!-- Set the actual web service URL with any query string parameters -->
	  <rewrite-uri template="/Orders/../triggers/invoke?api-version=2022-05-01..." />
	</when>
  </choose>
  <!-- Grab the header value (string) -->
  <set-variable name="length" value="@(context.Request.Headers["Content-Length"][0])" />
  <!-- Parse it as an integer for comparisons -->
  <set-variable name="contentLength" 
                value="@(int.Parse(context.Variables.GetValueOrDefault<string>("length", "0")))" />
  <choose>
	<!-- Set the large file size in Bytes -->
	<!-- This wil route requests over 1 MB to a different location like an Azure Funcation -->
	<when condition="@(context.Variables.GetValueOrDefault<int>("contentLength") > 1000000)">
	  <set-backend-service backend-id="EndpointLarge" />
	  <rewrite-uri template="/LargeOrders/../triggers/invoke?api-version=2022-05-01..." />
	</when>
  </choose>
</inbound>

YouTube player

Watch for exciting content related to the Azure Integration Services, Azure Logic Apps, and Azure AI being created soon.

If you have any questions, feel free to reach out!