Azure Functions Advanced

Cold starts, performance and cost

Functions can be very cheap or surprisingly expensive, and fast or slow, depending on a few habits: reusing connections, staying async, tuning concurrency and choosing the right plan.

Where speed and cost come from
  1. ConnectionsReuse HttpClient and SDK clients.
  2. Async codeNo blocking; threads stay free for more work.
  3. ConcurrencybatchSize and maxConcurrentCalls in host.json.
  4. Cold startsReadyToRun builds and always-ready instances.
  5. DurationShort invocations cost fewer GB-seconds.
  6. Side costsLogs, Storage transactions and data transfer.

Most wins come from connection reuse and short invocations.

Reuse connections

Creating a new HttpClient or SDK client per call is the most common performance bug. It exhausts sockets under load and adds latency. Register clients once and inject them.

C#
// Program.cs
builder.Services.AddHttpClient<IRatesClient, RatesClient>();
builder.Services.AddSingleton(_ =>
    new ServiceBusClient("shop-bus.servicebus.windows.net", new DefaultAzureCredential()));
builder.Services.AddSingleton(_ =>
    new CosmosClient("https://shop-db.documents.azure.com", new DefaultAzureCredential()));

Tune concurrency in host.json

Higher concurrency means more work per instance and fewer instances; lower concurrency protects slow dependencies.

JSON
{
  "version": "2.0",
  "extensions": {
    "queues": { "batchSize": 16, "newBatchThreshold": 8, "maxDequeueCount": 5 },
    "serviceBus": { "maxConcurrentCalls": 16, "prefetchCount": 0 }
  },
  "functionTimeout": "00:10:00"
}

Habits that pay off

  • Be async all the way down; never block with .Result or .Wait().
  • Keep each invocation short. Split long jobs into queue messages or a Durable Functions workflow.
  • Process batches where the trigger supports them, such as Event Hubs and Service Bus batch triggers.
  • Publish with ReadyToRun and avoid heavy work at start-up to shorten cold starts.
  • Use always-ready or pre-warmed instances only for latency-sensitive HTTP apps, because they bill all the time.

Keeping the bill low

Serverless plans bill for executions and for memory multiplied by duration (GB-seconds), so shorter, leaner functions cost less. The Consumption plan includes a monthly free grant of executions and GB-seconds.

Watch the hidden costs: Application Insights ingestion, Storage transactions from busy queues and timers, and outbound data.

Use the Azure pricing calculator and set budgets with alerts in Cost Management before going live.

Interview question

Your function app fails under load with socket exceptions. What is the likely cause and fix?

Most likely a new HttpClient or SDK client is created on every invocation, which exhausts outbound sockets. I register clients once, with IHttpClientFactory or as singletons for SDK clients such as ServiceBusClient and CosmosClient, and inject them. I also check for blocking calls and tune concurrency in host.json.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.