Azure Functions
Azure Functions is Microsoft's serverless compute service. You write a small piece of code (a "function") that runs in response to an event, and Azure handles provisioning, scaling, and patching. On the Consumption and Flex Consumption plans, you pay only for execution time and resources used, and it scales to zero when idle.
Core concepts:
- Triggers start a function. Common ones: HTTP request, timer (cron), Blob Storage change, Queue message, Service Bus, Event Hubs, Cosmos DB change feed.
- Bindings declaratively connect to input and output data (e.g., read a blob, write to a queue) without boilerplate SDK code.
- Languages: C# (isolated worker model for modern .NET), JavaScript/TypeScript, Python, Java, PowerShell.
- Hosting plans:
- Consumption / Flex Consumption: scale-to-zero, pay-per-use. Flex adds faster scaling, VNet support, and configurable instance memory.
- Premium: pre-warmed instances (no cold starts), VNet integration.
- Dedicated (App Service plan): run on VMs you already pay for.
- Durable Functions: an extension for stateful workflows (chaining, fan-out/fan-in, human approval, long-running orchestrations) written as code.
A minimal C# example (isolated worker, HTTP trigger)
public class HelloFunction
{
[Function("Hello")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req)
{
var name = req.Query["name"].ToString();
return new OkObjectResult($"Hello, {string.IsNullOrEmpty(name) ? "world" : name}!");
}
}
When to use them
Good fits:
- Event-driven processing: resize an image when it lands in Blob Storage, process messages from a queue, react to database changes.
- Scheduled jobs: nightly cleanup, report generation, periodic data sync (timer trigger).
- Lightweight APIs and webhooks: receive callbacks from Stripe, GitHub, etc., or expose a few small endpoints.
- Glue and integration: connect services together with minimal code.
- Spiky or unpredictable workloads: traffic that's idle most of the time or bursts heavily, where scale-to-zero saves money.
- Workflows: multi-step orchestrations with Durable Functions.
Consider something else when:
- You have a full web app or large API with many routes, middleware, and shared state. App Service or Container Apps is more natural.
- You need consistently low latency and can't tolerate cold starts. Use the Premium plan, or App Service.
- Long-running, CPU-heavy work that exceeds function time limits (the Consumption plan defaults to 5 minutes, max 10). Look at Container Apps jobs, Batch, or Durable Functions to break the work up.
- You need fine control over the runtime or OS, or want to run arbitrary containers. Container Apps or AKS fits better.
- Steady, high-volume traffic where always-on compute can end up cheaper than per-execution billing.
Functions vs. App Service
| Azure Functions | App Service | |
|---|---|---|
| Unit of deployment | Individual functions | Whole web app |
| Scaling | Event-driven, to zero | Plan-based, always running |
| Billing | Per execution (Consumption) | Per plan, per hour |
| Best for | Event handlers, jobs, small APIs | Full web apps and APIs |
They're not mutually exclusive: many systems use App Service for the main app and Functions for background processing around it.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.