Put a limit on consumption based azure functions scaling

Needless to say, one of the coolest features and biggest advantage of consumption based azure functions is the fact that how they scale out without we having to do anything about it. As of writing this post, consumption based functions can scale out up to 200 instances and premium ones up to 100. Apparently, if you need even more instances, that's also possible but you need to contact azure support team.

So far so good but in real world scenarios we need to control scaling of instances. More often than not, azure functions deal with databases or other services which either can't scale as much or financially is not cost effective to do so. Over-scaling azure functions could put other resources under pressure and ultimately exceed the threshold and bring everything down.

During the past few years, Azure team introduced several workarounds to limit the scaling which was mostly adding some settings to the configuration, but truth be told, they never worked well for me and some others I know.

One good old technic to control the load is to put the requests in a Queue and then start processing them from there. It's possible to limit the number of items which could be picked up from a Queue in parallel and that kind of lets us control the processing load. This is a good proven technic but not applicable everywhere and sometimes is a bit of overhead.

Now I will tell you a true story that occurred to me while ago. An external client suddenly pushed (they were not supposed to do) massive amount of data to our EventHub. That caused azure functions listening to the EventHub scale out dramatically and they all put requests into a Queue. Since the consumption of the data from the Queue was much slower than enqueuing items, the Queue after some time ran out of capacity and rejected receiving new items. That caused us a bit of work to recover the possible lost data.

Fortunately, recently a new feature was added to azure functions to tackle this issue and it works quite well. IF you open an azure function page in the portal, you will find Scale out menu item in the left. There is possible to put a limit oon maximum number of instances if you need.

Of course, it could also be done via Azure Cli

az resource update --resource-type Microsoft.Web/sites -g <resource_group> -n <function_app_name>/config/web --set properties.functionAppScaleLimit=<scale_limit>

More info here.

Last word

While architecting Serverless applications it's wise to put a cap limit on all possible resources because of Murphy's law. Anything that can go wrong will go wrong!

No Comments