Understanding Azure Functions with App Service Plan - azure

While creating an Azure Function. It provides an option to create an App Service Plan.
Let's say we select P2V2 which has 7GB Ram and 2 Cores. Here are the questions:
Let's say when the function is triggered, and each invocation consumes 1GB Ram. Does it mean that the same instance at maximum can concurrently run ~6 (leaving aside 1GB for OS let's say). Where all the 6 concurrent triggered functions re-use the same cores?
When does the App Service plan decide to scale out to multiple instances?

Yes, probably. As stated in Azure Functions hosting options - Service limits the number of Function apps per plan is unbounded, but:
The actual number of function apps that you can host depends on the activity of the apps, the size of the machine instances, and the corresponding resource utilization.
By default, an App Service Plan doesn't scale. In the same article I linked to before, it states that for a Dedicated Plan you can use Manual scaling or Autoscale. For autoscale, you control the rules.
For more information, see the documentation Juunas linked to in this comment.
Best practices for Autoscale

Related

How to reuse Azure Functions premium plan?

I have an Azure Function Premium plan for a C# function. I need this because my function needs to be deployed inside a VNET. Now I need to deploy another Python function. Can I reseuse my Premium plan? If I understand correctly it's a "server" which is running forever but which is only "used" when a function is triggered. Running two such a servers will be expensive and overkill for my functions.
Yes, you can run multiple Azure Function Apps in the same premium hosting plan, but the limitation is they should both run on same operating system either windows or Linux.
Running two such a servers will be expensive
Yes, it is expensive because of pre-warmed instances and always ready instances but predictable pricing.
The main 3 factors considered on Azure Functions are Application Insights, Network Traffic and Storage Account.
As per my experience,
Storage account is used by Azure functions for its internal state in which these costs are negligible.
When the functions are sending the traffic the outside world (Egress traffic), then the networking fees would occur which is usually low but may raise cost for high-volume data sending by the Azure Function.
Application Insights cost is the main factor we have to monitor and optimize. Refer to these few workarounds (Thread1 & Thread2) for optimizing cost in Azure Function - App Insights.

Azure function app scaled out but the performance of each instance are not equal

I have a durable function app to handle xml file in blob which size is between a few megabytes and hundreds of megabytes.
The requirement requires up to 20 files to be process at the same time.
I've scaled out the durable function app to 4 instances, but when requests increase rapidly, only 2 instances encountered the problem of too high CPU, while the other 2 did not.
This results in very slow file processing.
Is there a problem with azure's built-in load balance?
See this picture to check the high CPU issue
Generally, the scaling logic in Azure Functions currently works well when the function is triggered by things like queues or Event Hubs.
if you're running your Function in an App Service (also known as a Dedicated Plan), it will by default only scale instances within the possibilities of the App Service Plan you defined.
Using an App Service plan, you can manually scale out by adding more VM instances. You can also enable autoscale, though autoscale will be slower than the elastic scale of the Premium plan. [...] You can also scale up by choosing a different App Service plan.
If you run your Function App on Consumption Plan (the true serverless hosting plan option since it enables scaling to zero)
For further information check the below provided links.
Azure Functions Scalability Issue.
Azure Functions Hosting Plan.
Consumption Plan Scaling Issues.

Azure Function workers vs instances

I'm currently using an Azure Premium Service App (P3v3). I found that my function needs some boost, so I decided to look at how I can configure scaling.
I found two available options:
Set FUNCTIONS_WORKER_PROCESS_COUNT in the configuration section
Configure the number of instances in Scale-Out menu
But what is the difference between instances and workers? Haven't found any information about that or how it can affect costs.
what is the difference between instances (Scale-Out menu) and workers (FUNCTIONS_WORKER_PROCESS_COUNT)?
These are Microsoft Documentations to understand Workers and Instances in terms of Cost Management (Billing) and Functionality:
MSFT Sources of FUNCTIONS_WORKER_PROCESS_COUNT:
Azure Functions - Functions App Settings - functions_worker_process_count
Azure Functions - Best Practices - FUNCTIONS_WORKER_PROCESS_COUNT
MSFT Sources of Azure Functions Premium Plan Scale-Out Instances:
Azure Functions Hosting options Information
Azure Functions Premium Plan Instances Billing
AFAIK, FUNCTIONS_WORKER_PROCESS_COUNT limits the max no. of worker processes per Function host instance. These instances are kind of separate VMs where the FUNCTIONS_WORKER_PROCESS_COUNT limit is applied to each of them individually.
For example, If the FUNCTIONS_WORKER_PROCESS_COUNT is set to 10, it means 10 Individual Functions concurrently run by each host instance.
Multiple Workers means Multiple Process Ids of the Same Function App which is a logical collection of Functions.
One Worker Process can host all functions of one Function App where the Single Host has the default value as 1 to the FUNCTIONS_WORKER_PROCESS_COUNT and the Function Host means it is the Physical/Virtual Host where Function App runs as Windows/Linux Process.
Refer here for more information on mechanism of FUNCTIONS_WORKER_PROCESS_COUNT.
how it can affect costs.
As this Microsoft Azure Services Pricing Calculator Says, you'll be charged per instance when you scale-out.
When it comes to Cost Management, the incremental nature of the Scale-Out methodology is extremely beneficial.
Cost increases should be somewhat predictable because the components are identical.
Scaling out also allows you to respond more quickly to changes in demand.
In most cases, services can be quickly added or deleted to satisfy resource requirements. By just using (and paying for) the resources required at the time, this flexibility and speed effectively minimize spending.
Refer to this article for more information on cost management and the benefits of Scale-Up and Scale-out.

Azure Functions scalability issue

I am using Azure Functions on the App Service Plan. My understanding is for every new execution the Azure Function will create a new App Service, execute the function and then shut down the App Service. There would be nothing shared between the multiple App Services that are spawned due to multiple requests.
However when I do test my Function(which is a video processing one), for one request the time it takes is around 2-3 mins however for multiple simultaneous requests the time increases to 10-15 mins. My questions are whether my understanding above is correct? If not then what resource is shared amongst these App Services? How should I decide my scaling options(manual vs auto)?
"My understanding is for every new execution the Azure Function will create a new App Service" Nope it will not run new instance each time. Generally if there is no load on AF it will stop all instances.
Then if first request/event comes in it will start first instance. This is why we have ColdStart in Serverless. After that scale controller will measure your instance performance memory and CPU consumption and decide if it needs to scale but it wont be instant. So if lets say you sent N amount of requests to do smth with video they could go to same first instance and increase load. Then AF will scale, because of CPU spike but it wont help with old requests since they are handled at first instance. Keep in mind For non-HTTP triggers, new instances are allocated, at most, once every 30 seconds which means that your AF should have CPU spike for at least 30 second to add new instance https://learn.microsoft.com/en-us/azure/azure-functions/event-driven-scaling
I am not sure if Azure Functions are good option for video processing. Azure function should be used for quick stuff usually I would say not more than 30 sec. But there are some limitation of execution time depends how you run it https://learn.microsoft.com/en-us/azure/azure-functions/functions-premium-plan?tabs=portal
Not sure what type of video processing you doing but i would have a look into Azure Media Services
The other options as you mentioned is Batch jobs with low priority https://azure.microsoft.com/en-au/blog/announcing-public-preview-of-azure-batch-low-priority-vms/ it actually a good use case you have: Media processing and transcoding, rendering and so on
A small addition to Vova's answer: if you're running your Function in an App Service (also known as a Dedicated Plan), it will by default only scale instances within the possibilities of the App Service Plan you defined. This means that all of the instances of your Function App run on the same virtual machine. That is most probably the reason you're seeing increasing request times with more requests.
If you want your Functions to scale beyond the capabilities of that plan, you will need to manually scale or enable autoscaling for the App Service plan.
An App Service plan defines a set of compute resources for an app to run. These compute resources are analogous to the server farm in conventional hosting.
and
Using an App Service plan, you can manually scale out by adding more VM instances. You can also enable autoscale, though autoscale will be slower than the elastic scale of the Premium plan. [...] You can also scale up by choosing a different App Service plan.
If you run your Function App on Consumption Plan (the true serverless hosting plan option since it enables scaling to zero),
The Consumption plan scales automatically, even during periods of high load.
In case you need longer execution times than those available in Consumption Plan, but the App Service Plan doesn't seem to be the best hosting environment for your Functions there's also the Premium Plan.
The Azure Functions Elastic Premium plan is a dynamic scale hosting option for function apps.
Premium plan hosting provides the following benefits to your functions:
Avoid cold starts with perpetually warm instances
Virtual network connectivity.
Unlimited execution duration, with 60 minutes guaranteed.
Premium instance sizes: one core, two core, and four core instances.
More predictable pricing, compared with the Consumption plan.
High-density app allocation for plans with multiple function apps.
More info on all the different Azure Functions hosting options.

Azure Functions not Running Fast Enough

I have an azure function that reads jobs from a storage queue. It then executes these jobs and grabs more. I have been getting more jobs for it to run lately and noticed that the queue is building up.
What can I do from an Azure Perspective to get better performance out of this? Each job runs in its own little world so adding a new instance or adding threads or attaching to a "better" machine would all work fine.
Things come to mind with the information provided:
For more pure power: Host your Azure Function in a dedicated App Service plan instead of using the consumption plan. You can scale up (better hardware) or out (more hardware). Be aware that this could also be worse in theory. I would give it a try. Or try the "premium consumption plan" mentioned by Ken.
More parallelism: If your queue builds up even though you are not using most of your resources. Try playing with the configuration parameters batchSize and newBatchThreshold.
Changed execution logic: Depending where most of your time is spent during function execution, durable functions might help. Based on your comments you might also try to cache the external data using static or Azure Redis Cache.
Look at the most common performance considerations
Premium plan (Preview)
Azure Functions Premium plan provides customers the same features and scaling mechanism used on the Consumption plan (based on number of events) with enhanced performance and VNET access. Azure Functions Premium Functions plan is billed on a per second basis based on the number of vCPU-s and GB-s your premium functions consume.
In order to use the Azure Functions Premium Plan private preview your subscription needs to be added to an allowlist. Please apply for access via http://aka.ms/functionspremium.
More Info:
https://github.com/Azure/Azure-Functions/blob/master/functions-premium-plan/overview.md

Resources