Azure Functions
Traditional applications run on servers that are always on and always billing, even when no one is using them. Serverless computing changes this model entirely. Azure Functions is Microsoft's serverless compute service — code runs only when triggered by an event, and billing happens only for the exact time that code runs, down to the millisecond.
What is Serverless Computing?
The word "serverless" does not mean there are no servers. It means the developer does not think about servers. Microsoft manages all the infrastructure — provisioning, scaling, and availability are all handled automatically. The developer only writes and deploys code.
Traditional vs Serverless Comparison
| Aspect | Traditional (Always-On VM) | Serverless (Azure Functions) |
|---|---|---|
| Server Management | Manual — patch, scale, monitor | None — Microsoft manages everything |
| Billing | Per hour, even when idle | Per execution (per millisecond of runtime) |
| Scaling | Manual or scheduled | Automatic, instant, unlimited |
| Code Unit | Full application | Single function (one task) |
| Best For | Long-running, stateful applications | Event-driven, short-lived tasks |
How Azure Functions Work
An Azure Function is a small piece of code that runs in response to a trigger. After the code runs, it stops. If 1,000 requests arrive simultaneously, Azure automatically runs 1,000 instances of the function in parallel — no configuration needed.
Function Components
Azure Function Structure: ┌──────────────────────────────────────────────────┐ │ Azure Function App │ │ │ │ ┌────────────────────────────────────────┐ │ │ │ Function: ProcessOrder │ │ │ │ │ │ │ │ Trigger → Code Runs → Output Binding │ │ │ │ │ │ │ │ Example: │ │ │ │ HTTP Request → Process → Write to DB │ │ │ └────────────────────────────────────────┘ │ │ │ │ ┌────────────────────────────────────────┐ │ │ │ Function: ResizeImage │ │ │ │ Blob Upload → Resize → Save New Blob │ │ │ └────────────────────────────────────────┘ │ └──────────────────────────────────────────────────┘
Triggers – What Starts a Function
A trigger is the event that causes an Azure Function to run. Each function must have exactly one trigger.
| Trigger Type | What Fires the Function | Example Use Case |
|---|---|---|
| HTTP Trigger | An HTTP request (GET, POST, etc.) | Build a REST API endpoint |
| Timer Trigger | A schedule (cron expression) | Run a database cleanup every night at midnight |
| Blob Trigger | A file is uploaded to Blob Storage | Auto-resize an image when uploaded |
| Queue Trigger | A message arrives in Azure Storage Queue | Process orders from a queue one at a time |
| Service Bus Trigger | A message arrives in Azure Service Bus | Handle payment notifications from an external system |
| Event Hub Trigger | Events stream in from Azure Event Hub | Process IoT sensor data in real time |
| Cosmos DB Trigger | Documents are added or modified in Cosmos DB | Sync changes to a search index |
Bindings – Connecting to Other Services
Bindings are a way to connect a function to other Azure services without writing connection code. Instead of writing SDK code to read from Blob Storage or write to a database, bindings handle the connection declaratively.
- Input Binding: Data is read from another service and passed into the function automatically (e.g., read a blob when an HTTP request arrives).
- Output Binding: Function output is written to another service automatically (e.g., write result to Cosmos DB after processing).
Example: HTTP Trigger with Cosmos DB Output Binding
Scenario: User submits a form → Function saves data to Cosmos DB
[HTTP POST Request]
↓
[Azure Function: SaveUserData]
Input: HTTP request body (name, email)
Output Binding: Write to Cosmos DB automatically
↓
[Cosmos DB: users collection]
{name: "Raj", email: "raj@example.com"} ← Saved automatically
Hosting Plans
| Plan | Billing | Scale | Best For |
|---|---|---|---|
| Consumption Plan | Per execution + memory time | Auto scale to any number of instances | Event-driven, unpredictable workloads. Free tier: 1 million executions/month. |
| Premium Plan | Per second of pre-warmed instances | Auto scale, no cold start | Functions that must start instantly (no delay), need VNet connectivity |
| Dedicated (App Service) Plan | Same as App Service Plan pricing | Manual or auto scale (like App Service) | Functions that run continuously, need predictable costs |
Cold Start Problem
When a function has not been invoked for a period of time on the Consumption Plan, Azure shuts down its instance to save resources. When a new request arrives, Azure must spin up a new instance — this startup delay is called a cold start and can take 1–10 seconds depending on the runtime.
The Premium Plan eliminates cold starts by keeping at least one instance always warm and ready to respond.
Durable Functions
Standard Azure Functions are stateless — they do not remember anything between executions. Durable Functions are an extension that allows writing stateful workflows in a serverless environment. They are used for long-running, multi-step processes:
- Function Chaining: Run functions in sequence — output of one becomes input of the next.
- Fan-Out/Fan-In: Run multiple functions in parallel, then aggregate results when all complete.
- Human Interaction: Pause a workflow and wait for an approval or external event.
Example: Order Processing with Durable Functions
Orchestrator Function: ProcessOrder
Step 1: ValidateOrder() → Check items are in stock
Step 2: ChargePayment() → Charge the customer card
Step 3 (Parallel):
│── SendConfirmation() → Email the customer
└── UpdateInventory() → Reduce stock count
Step 4: WaitForShipment() → Pause until shipper confirms
Step 5: NotifyDelivery() → Send delivery notification
Supported Languages
- C# (.NET)
- JavaScript / TypeScript (Node.js)
- Python
- Java
- PowerShell
- Custom handlers (any language via HTTP)
Key Takeaways
- Azure Functions is a serverless service — code runs in response to events, and billing is per execution.
- Triggers define what starts a function: HTTP requests, timers, blob uploads, queue messages, etc.
- Bindings simplify connections to other Azure services without writing SDK connection code.
- The Consumption Plan is free for 1 million executions/month; the Premium Plan eliminates cold starts.
- Durable Functions extend the serverless model to support stateful, long-running multi-step workflows.
