Search code examples
c#azure-functionsazure-iot-hubprovisioningparallel.foreach

Should I use Parallel.ForEach to loop through the DPS linkedHubs?


Here is the scenario:

  1. DPS with enrollmentgroup with more than one linked IotHub
  2. The enrollment group is associated with the custom function app.

Custom function app:

  1. When the DPS request is made, a payload is sent to the DPS with the information that should determine what iothub(iothubhostname) the device should be registered to.

  2. The app will receive the payload along with list of linkedIotHubhostname in the requestbody

  3. App now needs to loop through the list of linkedIotHubHostname to determine which iothub the device belongs to based on the information provided.

For step 3, should I be using Parallel_foreach given the case that more than one device might be provisioning at the same time?


Solution

  • When multiple devices start provisioning at the same time, your Function will receive more requests. Depending on the function plan you chose, it will automatically create more instances to handle the load. The execution time of your Function will have some impact on when scaling is necessary, but unless you're talking execution time of seconds, Parallel.ForEach is not likely going to make a difference. It also depends on how many hubs and devices you're expecting to have.

    You can deploy your function and check the execution time, optimise it later if necessary.