Introduction
Azure Functions in Consumption plan scale to zero when idle. When a new request arrives, a cold start occurs: the runtime must be initialized, dependencies loaded, and the function compiled. This causes latency spikes from milliseconds to seconds.
Symptoms
High initial response time:
```bash # First request after idle period $ curl -w "Time: %{time_total}s\n" https://my-function.azurewebsites.net/api/http
Time: 5.234s # Cold start
# Subsequent requests $ curl -w "Time: %{time_total}s\n" https://my-function.azurewebsites.net/api/http
Time: 0.150s # Warm ```
Application Insights showing latency:
# Spikes indicate cold starts ```
Timeout during cold start:
"Function execution took too long"
"Function invocation failed after 10 seconds"Common Causes
- 1.Consumption plan scale-to-zero - No pre-warmed instances
- 2.Large function size - Many dependencies to load
- 3.Heavy initialization - Slow dependency injection setup
- 4.Runtime version - .NET functions slower cold start than other runtimes
- 5.Memory pressure - High memory allocation during startup
- 6.Missing bundle optimization - Not using function runtime optimization
- 7.Startup code complexity - Too much code in function initialization
Step-by-Step Fix
- 1.Check logs for specific error messages
- 2.Verify configuration settings
- 3.Test network connectivity
- 4.Review recent changes
- 5.Apply corrective action
- 6.Verify the fix
Step 1: Measure Cold Start Impact
# If ColdStarts > 20% of total, consider mitigation ```
Step 2: Upgrade to Premium Plan
```bash # Create Premium plan az functionapp plan create \ --name my-premium-plan \ --resource-group my-rg \ --sku EP1 \ --location eastus
# Migrate function app to Premium az functionapp update \ --name my-function \ --resource-group my-rg \ --plan my-premium-plan
# Set pre-warmed instances az functionapp plan update \ --name my-premium-plan \ --resource-group my-rg \ --min-instances 2 ```
Step 3: Configure Pre-Warmed Instances
```bash # Set minimum instances for Premium plan az functionapp plan update \ --name my-premium-plan \ --resource-group my-rg \ --min-instances 2 \ --max-burst 10
# Pre-warmed instances = min-instances # These stay warm and ready for requests ```
Step 4: Optimize Function Size
```csharp // For .NET functions, use isolated worker for better cold start // Program.cs var host = new HostBuilder() .ConfigureFunctionsWorkerDefaults() .ConfigureServices(services => { // Only register services you actually need services.AddSingleton<IMyService, MyService>(); }) .Build();
host.Run(); ```
```python # For Python, minimize imports # Only import what you need in function import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse: # Import heavy libraries inside function if not always needed import pandas as pd # ... ```
Step 5: Use Warmup Trigger
// Add warmup function to pre-initialize dependencies
[FunctionName("Warmup")]
public static void Warmup(
[WarmupTrigger] object warmupContext,
ILogger log)
{
log.LogInformation("Function app warming up");
// Initialize any singleton services here
}```python # Python warmup function import azure.functions as func
def main(warmupContext: func.WarmupTrigger) -> None: # Pre-initialize any heavy resources pass ```
Step 6: Configure Warmup Settings
```bash # Set function app to always be ready az functionapp config appsettings set \ --name my-function \ --resource-group my-rg \ --settings WEBSITE_ALWAYS_READY=1
# For specific functions, use: az functionapp config appsettings set \ --name my-function \ --resource-group my-rg \ --settings "AzureWebJobs.HttpTrigger.Disabled=false" ```
Step 7: Use Azure Functions Proxies
# Create a proxy that calls a warm endpoint
# proxies.json
{
"$schema": "http://json.schemastore.org/proxies",
"proxies": {
"warmup": {
"matchCondition": {
"methods": ["GET"],
"route": "/api/warmup"
},
"backendUri": "https://my-function.azurewebsites.net/api/health"
}
}
}Step 8: Monitor Cold Starts
```bash # Set up alert for high latency az monitor metrics alert create \ --name function-coldstart-alert \ --resource-group my-rg \ --scopes /subscriptions/SUB/resourceGroups/my-rg/providers/Microsoft.Web/sites/my-function \ --condition "avg HttpResponseTime > 2" \ --window-size 5m
# Monitor with custom metric # In function code, log cold starts log.LogInformation($"Cold start: {Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID") == null}"); ```
Step 9: Use Dedicated Plan
```bash # For consistent performance, use Dedicated (App Service) plan az functionapp plan create \ --name my-dedicated-plan \ --resource-group my-rg \ --sku P1v3 \ --is-linux
# No cold starts but no scale-to-zero either # Fixed cost regardless of usage ```
Step 10: Implement Client-Side Retry
// Client code with retry for cold start
async function callFunctionWithRetry(url, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const response = await fetch(url, {
timeout: 30000 // Longer timeout for cold start
});
return await response.json();
} catch (error) {
if (i === maxRetries - 1) throw error;
await new Promise(r => setTimeout(r, 1000 * (i + 1))); // Exponential backoff
}
}
}Azure Functions Plan Comparison
| Plan | Cold Start | Cost | Scale |
|---|---|---|---|
| Consumption | Yes (5-10s) | Pay-per-use | Auto |
| Premium | No (with min instances) | Fixed + usage | Auto |
| Dedicated | No | Fixed | Manual |
Verification
```bash # After upgrading to Premium with min instances # Test cold start behavior for i in {1..10}; do curl -w "Request $i: %{time_total}s\n" -o /dev/null -s https://my-function.azurewebsites.net/api/http sleep 60 # Wait for potential idle done
# Should show consistent low latency (< 500ms)
# Check instance count az functionapp plan show \ --name my-premium-plan \ --resource-group my-rg \ --query '{MinInstances:sku.capacity,MaxBurst:maximumElasticWorkerCount}' ```
Related Issues
- [Fix Azure Functions Memory Limit Exceeded](/articles/fix-azure-functions-memory-limit-exceeded)
- [Fix Azure Functions Timeout](/articles/fix-azure-functions-timeout)
- [Fix Azure Functions Premium Instance Warmup](/articles/fix-azure-functions-premium-instance-warmup)
Related Articles
- [Technical troubleshooting: Fix Azure Aks Pod Crashloopbackoff Issue in Azure](azure-aks-pod-crashloopbackoff)
- [Technical troubleshooting: Fix Azure Api Management Policy Expression Runtime](azure-api-management-policy-expression-runtime-error)
- [Technical troubleshooting: Fix Azure App Configuration Feature Flag Not Refre](azure-app-configuration-feature-flag-not-refreshing)
- [Technical troubleshooting: Fix Azure App Service 503 Always On Disabled Issue](azure-app-service-503-always-on-disabled)
- [Technical troubleshooting: Fix Azure Application Gateway Err SSL Unrecognized](azure-application-gateway-err-ssl-unrecognized-name-alert)
<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "Fix Azure Functions Consumption Cold Start Latency", "description": "Reduce Azure Functions cold start latency. Upgrade to Premium plan, use warmup triggers, and optimize function configuration.", "url": "https://www.fixwikihub.com/fix-azure-functions-consumption-cold-start", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2026-04-02T18:35:49.447Z", "dateModified": "2026-04-02T18:35:49.447Z" } </script>