Azure Functions timeout
Everything about function app timeout
Issue:
· Timeout value
of 00:05:00 exceeded by function?
· What is azure
functions timeout?
· What is azure
functions execution timeout?
· What is default
timeout in azure function?
· Consumption
plan timeout?
· Premium plan
timeout?
Solution:
What is timeout? In business logic language, a timeout is a specified period of time that will be allowed to complete an operation and if that operation doesn’t complete within specified period of time then it will throw some exception. Timeouts allow for more efficient usage of limited resources without requiring additional interaction from the agent interested in the goods that cause the consumption of these resources.
A common example is your smart phone
turn off backlight after a certain time when idle which you can set.
Similarly, Azure has set the
timeout for function app and it is very important to understand it as you can develop/write
your business logic and optimize it accordingly.
In Azure functions, timeout means
if your business logic or code should be optimized enough to complete with timeout
duration otherwise it throws exception like “Timeout
value of 00:05:00 exceeded by function”
In Azure functions, timeout varies according to hosting
plan and runtime versions.
Azure functions have 3 hosting plans,
- Consumption plan (Dynamic plan)
- Premium plan (Elastic premium plan)
- App Service plan (Dedicated plan
·
1.x
·
2.x
·
3.x
Where 1,2 and 3 is the major version and x stand
for minor version e.g. 2.09234.
Now we know what azure functions execution timeout
is? hence we should focus on
·
Default timeout,
· maximum
timeout
for each hosting plan.
You can check this information in the below list.
That means if you see exception like “Timeout value
of 00:05:00 exceeded by function” then you quickly need to check if your
function is hitting the default timeout.
Here you have 2 options,
1. Investigate
why your code execution is taking that much of time and optimize your code
2. Increase
the default timeout.
For first case you better know how to optimize the
code.
How to modify the default timeout
Azure functions timeout value is stored and handled
in host.json
file and attribute name is functionTimeout
{ "functionTimeout":
"00:05:00" } |
functionTimeout
indicated the timeout duration for all the functions and not for
the single function.
You can update the default timeout value, save it,
and test your function again.
In dedicated app service plan
there is no limit but you should keep a fixed upper bound limit as unlimited
timeout can have some disadvantages like your execution will keep executing and
you will have to pay for it.
For unlimited timeout you can use
-1
{
"functionTimeout":
"-1"
}
Important note
Regardless of the function app timeout setting, 230
seconds is the maximum amount of time that an HTTP triggered function can
take to respond to a request. This is because of the default
idle timeout of Azure Load Balancer. For longer processing times,
consider using the Durable
Functions async pattern or defer
the actual work and return an immediate response. |
Function
timeout at function level
We know now that we can set
function timeout in host.json but it is for all the function inside it. What about
setting timeout for one or 2 function only.
While developing the functions
from Visual Studio, you can now set the timeout at the function level.
Adding the attribute [Timeout("00:00:10")],
above the function definition would set the timeout value for the function. more
details.
I did test the function executions
to confirm that it doesn’t affect executions of other functions in the same
function app.
With the test deployment I made –
I deployed multiple function to the Function App on Azure, with only one of
them (say function A) having the timeout set. While executing the functions, we
see that A stops after the threshold timeout value and the others continue as
expected.
Imp Note:
This is for C# language only.
Output:
Hope this helps. Please let me
know if you have any query
References:
https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale
https://docs.microsoft.com/en-us/azure/azure-functions/functions-host-json#functiontimeout
Very well explained. Thanks
ReplyDeleteazure training
ReplyDeleteazure online training
The function level timeout mentioned doesn't work for now (2021/10/27). There is a bug still active in github - https://github.com/Azure/Azure-Functions/issues/1900, and it indeed doesn't work in my local machine. (win10 + vs2019)
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteInteresting...but what about capturing the timeout in the code? I mean, is there a way to capture a timeout in the azure function to do something if this happens? this was possible in regular apps but seems like Azure functions don't allow devs to detect if a TimeOut exception has happened.
ReplyDelete