I am new to Azure Functions.I need to write some C# code that scales out Azure Functions based on some condition.
For example:
I have total 10 customers for which Azure Function will be called. But out of these 10 there is one high priority customer. If any function request for that high priority customer comes in, I want scale out my Azure Function and run that request on a new instance. Once that request is completed, I want to destroy that scaled out instance. This way my high priority customer gets the full server resource to use.
My questions are:
- Is it possible what I want?
- If yes:
How?
What should be the C# code to scale?
How to forward my request to new instance?
How can I destroy current scaled out instance? - If No:
What should be the best way to give high priority to selected customers in Azure Function?
Before I try to give an answer to your questions, I would first like to explain something. Azure Functions can run in two different ways: on a Consumption Plan, and on an App Service Plan. For you to take full advantage of Functions (or actually Serverless), you should run your Functions in a Consumption Plan.
and
About scaling:
More interesting information on scaling: Azure Functions scale and hosting
Looking at your requirements, my idea would be to leverage the real power of Functions by running them in a Consumption Plan. If you have a high-priority customer, give them their own instance of the Function, triggered by its own container in the Blob Storage (take a look at the link in @Gonzo345 's comment). This ensures that it gets processed as soon as it comes in, and it will only incur costs if a trigger comes in for the high-priority customer. Because of the idea of a Consumption Plan, it will instantiate the Function, run whatever needs to run and destroy the instance.
By the way: Blob Triggers in the end are polling triggers. When there's a lot of movement, they're pretty fast. But when there's not a lot going on, changes in the storage account might take up to a few minutes to actually trigger the function. If you want real-time event handling, have a look at Event Grid.
Now to get back to your questions:
This is not possible. You can't directly affect the scaling algorithm of Azure Functions (on Consumption Plan).
Blob Trigger specifically has some known limitations as described in documentation. Particularly, there are might be delays in processing blob.
For faster scaling, I suggest you using Event Grid triggers, which should scale pretty well for both "priority" and "non-priority" customers of yours. See Image Resizing sample.