Azure Resource Manager Batch API

DZone 's Guide to

Azure Resource Manager Batch API

Learn to use Azure Resource Manager's Batch API, which improves performance and data usage in Android and iOS apps when requesting resource statuses.

· Mobile Zone ·
Free Resource

The latest Azure Mobile App update has statuses on the resources list:

You probably want to ask why we didn't have them before. Great question! Currently, Azure Resource Manager (the public API we are using to get your Azure resources) requires making separate calls to get single resource statuses. It means that if you have 100-200 resources, you would have to make 100-200 extra calls. There are some people who have almost 2000 in one subscription! Taking performance and data usage into consideration, this is not ideal.

Both iOS and Android platforms allow us to address this problem to some extent by querying for status only resources that are currently visible. However, this is still an extra 5-10 calls. It is even worse when you start scrolling, and very bad if you scroll in your list containing 2000 resources.

Batch API

Some time ago, ARM added Batch API - you can send POST requests with up to 20 URIs in the body. The response will contain up to 20 packaged responses that you have to extract. Using Batch API, you can decrease the number of requests by up to 20x. This matters especially when the user has a lot of resources and keeps scrolling down the list.

When implementing batch requests, you need to figure out the optimal interval for sending requests. We started with 200ms, but then we changed it to 50ms. Additionally, every time new request is coming we delay sending the batch request by additional 50ms. This may cause indefinite delay. In order to solve this: we always submit a request if the queue has 20 or more pending requests. 20*50ms = 1000ms = 1s = long time! We tweaked it again and changed the interval to 20ms. With the current implementation, we wait any time between 20ms and 400ms to send the batch request.

Implementing Batch API

You're probably gonna say, "It all sounds great, but how do I implement it?" For your convenience, I created a small console application that demonstrates ARM Batch API in action, and I put it on GitHub.

Xamarin.iOS and Xamarin.Android does not have System.Threading.Timer. We created our own implementation OneShotTimer (thanks, William Moy!).

All the magic happens in ArmService. It has one public method GetResource that, instead of directly sending GET requests, is adding request, to ConcurrentQueue. OneShotTimer and BatchRequestDipatcher methods are responsible for sending the actual HTTP request.

In order to run the console app, you need to provide the ARM token, and (optionally) resource ids you want to request. In the demo app, I provided fake resource ids, which will be fine to issue requests, but you will not get resources back.

To get the ARM token, go to Azure Portal, open F12 tools, and inspect an ARM request. From request headers, copy Authorization header (string starting with Bearer rAnDoMcHaRacTErS...).

You can also get resources ids from the F12 tab. The best way is to go to the All Resources blade and find a batch request:

Once you paste the resource ids and ArmToken in Program.cs, you can run the app, and you should see the following output:

Requests are sent in random time, anytime from 0 to 5s after the program runs. This is done using Task.Delay:

var tasks = _resourceIds.Select(async resourceId =>
                await Task.Delay(new Random().Next() % 5000);   // simulate calling GetResource from different parts of UI
                var response = await _armService.GetResource(resourceId);

When you change randomness from 5s to 0.5s you can observe that there will be fewer batch requests (AKA more requests sent in a single batch).


Using Batch API for getting resource statuses visibly improves performance in the mobile app. It is noticeable, especially when using network data.

Azure Resource Manager has plans to add ARM API that will allow 1 request to get multiple resources with statuses. This should improve performance even more in the future.

If you are facing a similar problem with your app, consider implementing Batch API on your server!

azure ,mobile app development ,ios ,android ,mobile

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}