Serverless Architecture Patterns
On this page
When serverless fits, and when it doesn't
You need a job that resizes every uploaded image. It runs a few hundred times a day, sometimes 10,000 times in an hour after a marketing email. A server running 24 hours a day for that is mostly idle. Serverless functions (AWS Lambda, Azure Functions, Google Cloud Run functions) run your code only when an event arrives, scale out automatically and charge per invocation and execution time. For this job, it's an excellent fit. For your main API, maybe not.
How serverless functions work
You deploy a function and attach a trigger: an HTTP request, a file landing in storage, a message on a queue, a database change, a schedule. The platform starts instances as events arrive, runs many in parallel under load, and scales to zero when idle. You don't manage servers, patching or capacity.
Serverless use cases
- Event processing: resize images, transcode files, parse uploaded CSVs.
- Glue and integration: react to a payment webhook, sync a record to the CRM.
- Scheduled jobs: nightly cleanups and reports.
- Spiky or low-volume APIs: internal tools, prototypes, traffic with long quiet periods.
- Queue consumers that scale with backlog (Day 73).
Serverless drawbacks: cold starts, limits and lock-in
Cold starts. An idle function has no running instance. The first request waits while the platform starts one and loads your code: from under 100 ms to a few seconds depending on runtime, package size and network setup. Mitigations: smaller packages, faster runtimes, keeping a minimum number of instances warm (which you pay for), or ahead-of-time compiled runtimes.
Statelessness and time limits. Instances can be recycled at any time, so nothing may live in memory between calls (Day 71). Execution time is capped (AWS Lambda allows up to 15 minutes); long work must be split up or orchestrated with a workflow service such as Step Functions or Durable Functions.
Database connections. A burst of 1,000 concurrent function instances can open 1,000 database connections and exhaust the database (Day 28). Use a connection proxy (RDS Proxy, PgBouncer), cap concurrency, or use databases built for many short connections.
Cost at steady load. Per-invocation pricing is cheap for spiky traffic and can be more expensive than containers for constant high traffic. Compare with your real traffic pattern.
Observability and testing. Many small functions mean many small logs and traces; distributed tracing (Day 88) becomes essential. Local testing needs emulators.
Lock-in. Triggers, permissions, packaging and configuration are provider-specific. Keep business logic in plain libraries and the provider-specific code in a thin handler, the hexagonal idea from Day 91.
At-least-once triggers. Storage events, queue triggers and retried invocations can run your function more than once for the same event; AWS Lambda retries a failed asynchronous invocation twice by default. Every function that changes state must be idempotent (Day 65).
Loops that bill you. A function triggered by uploads to a bucket that writes its output back to the same bucket triggers itself, forever. Write outputs to a different bucket or prefix, and set a concurrency cap and a budget alert on every function.
For .NET teams on Azure: the in-process hosting model for Azure Functions reaches end of support on November 10, 2026. New and existing functions should run on the isolated worker model (Microsoft).
Serverless patterns that work well
- Queue between producer and function, so bursts are buffered and the function's concurrency can be capped.
- Fan-out: one event triggers several functions through a topic (Day 45).
- Orchestrated workflows for multi-step processes, with retries and compensation handled by the workflow service.
- Containers on serverless platforms (Cloud Run, Azure Container Apps, AWS Fargate): scale-to-zero convenience with ordinary container images and fewer limits, a common middle ground.
Check yourself
A request arriving with no warm instance, so the platform must start one and load the code. Smaller packages, faster runtimes and keeping minimum instances warm reduce it.
Put a queue in front and cap function concurrency, and use a connection proxy so the database sees a bounded number of connections.
For steady, high traffic. Functions charge per invocation and duration, which is cheap for spiky or low traffic but adds up when a function runs constantly. An always-on container that's well utilized often costs less, and it avoids cold starts and execution time limits.
Each output triggered the function again, creating an endless loop. Write outputs to a separate bucket or prefix, and cap concurrency and alert on spend.