Serverless vs containers
Both let teams deploy code without managing servers directly. The difference is how much of the runtime you control, and how you pay for it.
- Serverless functions (for example AWS Lambda (opens in a new tab), Azure Functions (opens in a new tab), Google Cloud Run functions (opens in a new tab)): you deploy a function; the platform runs it in response to events, scales it from zero to many copies, and bills per invocation and duration.
- Containers (for example on Kubernetes (opens in a new tab), Amazon ECS (opens in a new tab) or Azure Container Apps (opens in a new tab)): you package an application with its dependencies and run long-lived processes on a platform, paying for the capacity you provision.
The line is blurring: serverless container platforms such as Google Cloud Run (opens in a new tab), AWS Fargate and Azure Container Apps run containers with scale-to-zero and per-use billing. The comparison below is about the two operating models.
At a glance
Section titled “At a glance”| Aspect | Serverless functions | Containers |
|---|---|---|
| Unit of deployment | A function | A container image |
| Scaling | Automatic, per request or event, including to zero | You configure autoscaling; usually a minimum number of instances |
| Billing | Per invocation and execution time | Per provisioned capacity, used or not |
| Cold starts | Yes, when a new instance starts | Only when scaling out |
| Execution limits | Maximum duration, memory and package size | Few; long-running processes are normal |
| State and connections | Stateless; connection pooling to databases needs care | Long-lived connections and in-memory caches are normal |
| Portability | Tied to the provider’s events and APIs | High; the same image runs almost anywhere |
| Operations | Little: the provider patches and scales | More: cluster, networking, scaling and patching, unless managed |
| Local testing | Harder; emulators approximate the platform | Straightforward |
Choose serverless when
Section titled “Choose serverless when”- Traffic is spiky, unpredictable or often zero: webhooks, scheduled jobs, file processing, back-office tools.
- The work is event-driven glue between managed services.
- The team is small and wants to spend as little time as possible on operations.
- Each piece of work finishes within the platform’s time limit.
Choose containers when
Section titled “Choose containers when”- Traffic is steady and high, where paying for provisioned capacity is cheaper than paying per request.
- You need long-running processes, WebSockets, background workers or large in-memory state.
- Latency must be consistent and cold starts aren’t acceptable.
- Portability across clouds or on-premises matters, or you already run a container platform well.
Cost: run the numbers
Section titled “Cost: run the numbers”Per-request pricing is cheap at low volume and expensive at high, steady volume. Estimate monthly cost both ways from your real request rate, duration and memory, and include what each needs around it: an API gateway for functions, a load balancer and idle capacity for containers, and people’s time for both. The total cost of ownership template helps compare them over several years.
Things that catch teams out
Section titled “Things that catch teams out”- Database connections. Hundreds of function instances each opening connections can exhaust a database. Use a connection proxy or a database built for it.
- Hidden coupling. A system of many functions connected by events can be harder to understand than one service. Keep function boundaries aligned with business capabilities.
- Running Kubernetes for three services. A container platform has a real operating cost. Use a managed or serverless container service unless you need the control.
The verdict
Section titled “The verdict”Use serverless functions for event-driven, spiky or low-volume work where operations time is scarce. Use containers for steady, high-volume, long-running or latency-sensitive services, preferably on a managed platform. Many systems use both: containers for the core services and functions for the glue around them.
Read more
Section titled “Read more”Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.