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

AspectTraditional (Always-On VM)Serverless (Azure Functions)
Server ManagementManual — patch, scale, monitorNone — Microsoft manages everything
BillingPer hour, even when idlePer execution (per millisecond of runtime)
ScalingManual or scheduledAutomatic, instant, unlimited
Code UnitFull applicationSingle function (one task)
Best ForLong-running, stateful applicationsEvent-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 TypeWhat Fires the FunctionExample Use Case
HTTP TriggerAn HTTP request (GET, POST, etc.)Build a REST API endpoint
Timer TriggerA schedule (cron expression)Run a database cleanup every night at midnight
Blob TriggerA file is uploaded to Blob StorageAuto-resize an image when uploaded
Queue TriggerA message arrives in Azure Storage QueueProcess orders from a queue one at a time
Service Bus TriggerA message arrives in Azure Service BusHandle payment notifications from an external system
Event Hub TriggerEvents stream in from Azure Event HubProcess IoT sensor data in real time
Cosmos DB TriggerDocuments are added or modified in Cosmos DBSync 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

PlanBillingScaleBest For
Consumption PlanPer execution + memory timeAuto scale to any number of instancesEvent-driven, unpredictable workloads. Free tier: 1 million executions/month.
Premium PlanPer second of pre-warmed instancesAuto scale, no cold startFunctions that must start instantly (no delay), need VNet connectivity
Dedicated (App Service) PlanSame as App Service Plan pricingManual 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.

Leave a Comment