Azure Functions
medium 31 minAzure Functions runs small pieces of code when something happens: a request, a message, a timer, a stored file.
Video lesson · Chapter 1 of 5
A customer gets a receipt before the order finishes
Azure Functions in 85 seconds
1:25Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A function as a service runs your code on demand and looks after the machines. Azure Functions is Microsoft's. The unit you deploy is a function app: one project with one or more functions that share settings and a plan. Each function has exactly one trigger, the event that starts it.
Two things set it apart from AWS Lambda and Cloud Run functions. Bindings declare inputs and outputs, so that the platform reads a document or writes a message for you. And Durable Functions lets one function coordinate others over minutes or months, with its progress stored.
Why it exists
Glue code is everywhere: resize, validate, notify, import. It is small, uneven, and needs a connection to a queue or a database. A function with bindings is a few lines, and it costs nothing in computing while idle on a plan that goes to zero.
Processes with several steps are harder: pay, then reserve, then ship, with retries and a wait for a person. Writing the state by hand in tables and queues is slow and easy to get wrong. Durable Functions keeps the state and lets the code read from top to bottom.
A short history
2016, March
Microsoft presents Azure Functions on 31 March.
2016, November
General availability is announced on 15 November.
2024, November
The Flex Consumption plan becomes generally available on 19 November: scale to zero together with private networks, a choice of memory and always-ready instances.
2026, September
Support for runtime version 1.x ends on 14 September. Only version 4.x is supported.
2026, November
Support for the .NET in-process model ends on 10 November. C# functions use the isolated worker model.
2028, September
Announced: the Linux option of the old Consumption plan retires on 30 September 2028.
Words to know
- Function app
- The unit you deploy and configure: functions that share settings, identity and a plan.
- Trigger
- What starts a function. Each function has exactly one.
- Binding
- A declared connection that reads input or writes output for you.
- Host
- The Functions runtime in an instance. Your language runs in a worker process beside it.
- Plan
- Where it runs: Flex Consumption, Premium, Dedicated, Container Apps, or the older Consumption.
- Orchestrator
- A durable function that says which steps follow which. It is replayed, so it must be deterministic.
- Activity
- A durable function that does one real step, such as charging a card.
- Poison message
- A message that fails every time.
How it works
Step through the idea, or press Play.
Trigger. A request, message, timer or file
Understand it
Start here
A shelf of music boxes
Picture a shelf with music boxes. Each has its own key, and only that key starts it. A box knows one tune.
The boxes remember nothing. The notebook does.
The model
Triggers and bindings
| Trigger | Starts when | Typical use |
|---|---|---|
| HTTP | A request arrives | API, webhook |
| Timer | A schedule of six fields, seconds first | Clean-up, reports |
| Queue Storage | A message is in a storage queue | Simple background work |
| Service Bus | A message in a queue or topic | Orders, sessions, dead letters |
| Event Hubs, Kafka | Batches from a stream | Telemetry |
| Blob Storage, through Event Grid | A file is stored | Pictures, imports |
| Cosmos DB | Documents change | Keep a second view up to date |
| Event Grid | An event from many Azure services | React to changes |
import jsonimport azure.functions as funcapp = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)@app.route(route="orders", methods=["POST"])@app.queue_output(arg_name="msg", queue_name="orders", connection="OrdersStorage")def accept(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse: order = req.get_json() msg.set(json.dumps(order)) # the binding sends it return func.HttpResponse(status_code=202)@app.queue_trigger(arg_name="msg", queue_name="orders", connection="OrdersStorage")def work(msg: func.QueueMessage) -> None: order = json.loads(msg.get_body()) print("order", order["id"], "try", msg.dequeue_count)Accept fast, work later
Plans
Where it runs decides how it scales
Hosting plans
| Plan | Zero? | Most instances | Time limit: default, most | Choose it when |
|---|---|---|---|---|
| Flex Consumption | Yes | 100 by default, up to 1,000 | 30 min, unbounded | The default choice for new serverless apps |
| Premium | No, at least one | 20 to 100 | 30 min, unbounded | Nearly always busy; no cold start wanted |
| Dedicated (App Service) | No | 10 to 30 | 30 min, unbounded with Always On | Spare capacity on existing machines |
| Container Apps | Yes | 300 to 1,000 | 30 min, unbounded | Own container, next to other containers |
| Consumption (legacy) | Yes | 200 on Windows, 100 on Linux | 5 min, 10 min | Only Windows needs; move otherwise |
Which plan?
Long work
The 230-second rule
Answer now, work later
Scale
How instances are added
A scale controller watches each trigger. For queues and streams it uses target-based scaling: the backlog divided by how much one instance should hold. On Flex Consumption each function scales on its own, except that HTTP functions form one group, Event Grid blob functions another, and durable functions a third. On the other plans the whole app scales together.
Target-based scaling
HTTP instances on Flex Consumption
Four settings to know
Instance memory
512, 2,048 or 4,096 MB on Flex Consumption. Default HTTP concurrency is 4, 16 and 32.
Always-ready instances
Kept warm per scale group. They do not count towards the maximum.
Maximum instances
Counted per scale group, not for the whole app. Protects the systems behind.
Concurrency
Fixed per trigger by default. Dynamic concurrency can tune some triggers if you turn it on.
Errors
Retries and poison messages
A storage queue message that fails
Who retries?
| Trigger | Retried by | When it gives up |
|---|---|---|
| HTTP | Nobody: the caller decides | The caller sees the error |
| Queue Storage | The queue, 5 tries by default | Copy in the poison queue |
| Service Bus | The queue's own rules | Dead-letter queue |
| Event Hubs, Kafka, Cosmos DB, Timer | A retry policy that you set on the function | Event Hubs: the position moves on and the batch is gone |
| Event Grid | The event subscription | Dead-letter storage, if set |
Durable
Long processes as code
Durable Functions has three roles. A client starts a flow. An orchestrator says what happens in which order. Activities do the work. The runtime saves a history event for each step. When a step finishes, the orchestrator runs again from the top, but steps that are in the history return their saved result at once. This is called replay.
One flow, three activities
Replay after a crash
Six patterns
| Pattern | Shape | Example |
|---|---|---|
| Chaining | One after another | Reserve, charge, ship |
| Fan-out and fan-in | Many at once, then combine | Resize 100 pictures; see fan-out, fan-in |
| Async HTTP | Answer 202 and a status address | A long export |
| Monitor | Look, wait, look again | Until a job is finished |
| Human interaction | Wait for an event or a time limit | Approval within three days |
| Aggregator | An entity that collects | A counter per device |
In an orchestrator, do
- Use the context's clock and new IDs
- Call activities for every network or disk access
- Use durable timers to wait
- Start a fresh history with continue-as-new in endless loops
In an orchestrator, never
- Read the real clock or a random number
- Call an API or a database directly
- Sleep in a thread or start threads of your own
- Read settings that may change between replays
The state lives in a storage provider. Microsoft recommends the managed Durable Task Scheduler; Azure Storage and SQL Server are the ones you bring yourself. The pattern behind compensation, undoing step one when step two fails, is the saga.
Operate
Security, releases and cost
Before production
- 1
Who may call
Function keys are a light barrier. For real protection put authentication in front, with Microsoft Entra ID or API Management.
- 2
What it may do
A managed identity with few rights, also for the bindings. Secrets belong in Key Vault.
- 3
Network
Flex Consumption, Premium and Dedicated can join a virtual network.
- 4
Storage account
The host needs one for its own work. Give each app its own, or a busy neighbour slows you.
- 5
Releases
Deployment slots on Premium and Dedicated. Flex Consumption has no slots; rolling updates are in preview there.
- 6
Watching
Application Insights: failures, run time, queue age, poison queue length, instance count.
Cost. On Flex Consumption you pay for running time by memory size and for the number of runs, and for always-ready instances while they wait. Premium and Dedicated are billed by the instance, busy or not. Storage, Application Insights and outgoing traffic are separate, and logs are often a surprise: sample them. The runtime itself is open source under the MIT licence, so it also runs in your own container.
Failure
What happens if
| Failure | Result | Prepare |
|---|---|---|
| An instance dies during a run | Queue message comes back after its hidden time (10 minutes if the host itself died) | Repeatable work |
| The database slows | More instances make it worse | Limit instances and concurrency |
| A poison message | Five tries, then aside | Alert on the poison queue |
| The host's storage is unreachable | Triggers and keys stop working | Own account, right redundancy |
| A Region is lost | The app is gone | A second app, and a plan for the data |
Decide
When to choose it, and when not
Choose Azure Functions
- Work that reacts to Azure services
- Uneven load
- Processes with steps, written as code
- C#, Python, Java, JavaScript, PowerShell or Go teams
- Small APIs and webhooks
Choose something else
- Steady heavy traffic: containers
- Hours of computing: Azure Batch or container jobs
- A flow drawn by non-programmers: Logic Apps
- Very short, very near the user: an edge platform
- No Azure in sight
Neighbours
| Option | Same | Different | Choose it when |
|---|---|---|---|
| AWS Lambda | Function per event | Step Functions for flows, no bindings | On AWS |
| Cloud Run functions | Function per event | A container service underneath | On Google Cloud |
| Logic Apps | Steps and connectors | Designed in a picture, little code | Connecting business systems |
| Azure Container Apps | Scale to zero | Any container, long-running services | Whole services |
| Task schedulers | Run work later | You build and run it | Special rules |
Summary
The whole topic in one minute.
A function app holds functions, each with one trigger and any bindings. The plan decides scale: Flex Consumption is the recommended serverless plan, and Premium keeps copies warm. Events are retried by the trigger's own rules, and messages that keep failing end in a poison queue.
Durable Functions stores each finished step and replays the orchestrator to continue. Keep orchestrators deterministic and activities safe to repeat, and use it when steps depend on each other or take long.
- One trigger per function; bindings move data without client code.
- Flex Consumption is the default; Premium stays warm; the old Consumption plan is legacy.
- HTTP answers within 230 seconds, whatever the plan.
- Queue messages get five tries, with no wait by default, then go to the poison queue.
- Orchestrators are replayed and must be deterministic; activities run at least once.
- Python on Flex Consumption serves one HTTP request per instance unless you change it.
Sources
- Azure Functions: overview
- Azure Functions: hosting options
- Azure Functions: Flex Consumption plan
- Azure Functions: event-driven scaling
- Azure Functions: concurrency
- Azure Functions: errors and retries
- Azure Functions: storage queue trigger
- Durable Functions: overview
- Durable Task: orchestrator code constraints
- Azure Functions: runtime versions