When the Consumption plan for Azure Functions appeared, many customers started using it by default. The main reason is that this plan,unlike App Service mode, only charges you for the runtime and memory used.However, it is important to assess whether this model is suitable for your functions, since it has some important differences compared to other plans. In this article I want to tell you two points that you should keep in mind to have a good experience with Azure Functions and consumption mode.
Control how long your function takes to run
Many times, this service is used to carry out tasks that can last a long period of time. Depending on the type of plan you have chosen,the default timeout varies and it is important that you know this information:
As you can see, the Consumption plan, by default, has 5 minutes and a maximum of up to 10. Sometimes, in very long processes, this time is not enough and it is necessary to change the type of the plan. To know how long your functions last you can see it in the Monitor section of each one:
If you want to modify the default value, you can do it through the host.json file, through the functionTimeout property. However, it is preferable that the functions are executed in the shortest possible time. If you are very close to 10 minutes, perhaps the Consumption mode is not ideal for your function.
Can you use EventGrid Trigger instead of Blob Trigger?
It seems logical that when you start with Azure Functions,you use the blob trigger if what you want is to detect new files to process.Here, you should bear in mind that when you use it, it loads the content of the file into the memory of your Azure Function.
In Consumption mode this can cause unexpected behaviours (like an OutOfMemory Exception), since this plan uses virtual machines with 1.5GB of RAM and a single core, so if the file that your Azure Function has to manipulate is very large, it can cause your function to run out of memory and the process fails.
In this link you can see the limits of each of the plans.
Depending on the type of task you want to carry out in your function, it will be necessary to change the plan or use the trigger for Event Grid. Since this only brings the information related to the blob but does not load the file itself into memory. So you could continue enjoying the Consumption plan, which is much cheaper.
Finally, it is worth mentioning that not long ago a new plan called Premium was launched, which is a mix between Consumption mode and App Service. In addition to bringing on additional features, instead of billing for execution and memory consumed, the Premium plan is based on the number of seconds per core and the memory used in the instances used, in addition to the preheated ones. At least one instance must be turned on at all times in this plan. This means that there is a minimum monthly cost, regardless of the number of executions, but the capacities of the machines and the execution time is greater.
This can also be a good intermediate option between Consumption and App Service. More information here.