Keep Single Instance Of Azure Web Job During Scaling

Before there is discussion around how to keep single instance of Azure Web Job. First we have to understand, how app service plan works.

Think like app service plan in one kind of VM instance. In single app service plan, you can put multiple web app and web job. If there is only one instance on VM than it contains all the application that part of App Service Plan. Just remember that scaling works at App Service Plan level and not at the App Service level.

For example. App Service Plan with Web app , Web api and Web Job and Scale at 1 instance.

image.png

Now if it scale to 2 then it looks something like this.

image.png

Now this is brief information and I will cover how to create app service plan effectively to allow it scale correctly and application that not required to scale will not scale.

More detail available at : App Service Plan

Now Let's talk about point. As you have seen that when it get scale it has multiple instance of same service. Web Job is mainly used for background processing. It try to read from single source like single database table.

So what happen both instance try to run same query and get same result and process same result. So it may happen that same action performed multiple time. For example sending email so person receive duplicate email and as it get scale this frequency get increase. There are other mechanism by which this is not happen like Azure Service Bus but for simple Sql Database table scenario is different.

There are always multiple solutions and each address differently.

  1. Each Web Job instance will try to process different set of row based on some partition key. If this is possible then this is definitely a good solution and scalable. We will discuss about this in other article.

  2. Another and simple solution is to make Web Job instanc single_ton so only one instance of Web Job run in App Service plan.

To do that

  1. Go to web job project.

  2. Add file settings.job at root level.

  3. Make sure you include that as a content so it is part of deployment.

  4. At following line in that file.

{ "is_singleton" : true}

You can not control which instance it will run but it will only run on one instance. Also this is only applicable to Continuous web job.

Another way to do is via kudu REST Api.

  1. Url is similar to yoursite.scm.azurewebsites.net

  2. Full Url will be like

PUT https://yoursite.scm.azurewebsites.net/api/continuouswebjobs/{job name}/settings

Body 
{
   "is_singleton" : true
}
  1. It required credential and it is same as Deployment-Credential. More info on this. [Deployment Credential] (docs.microsoft.com/en-us/azure/app-service/..)