Scale azure function through C# code

2019-07-22 03:55发布

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:

  1. Is it possible what I want?
  2. 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?
  3. If No:
    What should be the best way to give high priority to selected customers in Azure Function?

2条回答
一夜七次
2楼-- · 2019-07-22 04:20

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.

  • Consumption plan - When your function runs, Azure provides all of the necessary computational resources. You don't have to worry about resource management, and you only pay for the time that your code runs.
  • App Service plan - Run your functions just like your web, mobile, and API apps. When you are already using App Service for your other applications, you can run your functions on the same plan at no additional cost.

and

When you're using a Consumption plan, instances of the Azure Functions host are dynamically added and removed based on the number of incoming events. This plan scales automatically, and you are charged for compute resources only when your functions are running. On a Consumption plan, a function execution times out after a configurable period of time.

About scaling:

Azure Functions uses a component called the scale controller to monitor the rate of events and determine whether to scale out or scale in. The scale controller uses heuristics for each trigger type. For example, when you're using an Azure Queue storage trigger, it scales based on the queue length and the age of the oldest queue message.

Scaling Azure Functions

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:

  1. Yes, it should be. It does require your Function App runs in an App Service Plan
  2. Have a look over here: Disable Autoscale and manually scale your instances
  3. See 2 :)
查看更多
霸刀☆藐视天下
3楼-- · 2019-07-22 04:29

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.

查看更多
登录 后发表回答