ThisCalculator

Everyday

Cloud Compute: Match Functions, Containers, or VMs to Workloads

Updated · Standard formula, shown below

Average pace 5.00 min/km
Average speed
12.00 km/h
Result
Finish time
50 min
You entered
Distance
10 km
You entered

To match a workload to the right cloud compute option, assess its duration, statefulness, scaling pattern, and dependency on the underlying OS: use Functions for short, event-driven, stateless tasks; Containers for portable microservices with moderate control needs; and VMs for long-running, stateful, or OS-specific applications. This article explains the trade-offs so you can choose confidently.

Formula

The decision isn’t a single formula, but a scoring model. For each workload, rate it on three axes: runtime duration (how long a single execution runs), statefulness (does it persist data between calls?), and infrastructure control (do you need to configure the OS, libraries, or network stack?). Then apply these rules:

  • Functions if duration is under ~15 minutes, stateless, and triggered by events (HTTP, queue, file upload).
  • Containers if duration is longer, stateful (with attached storage), or you need specific runtime versions but not full OS control.
  • VMs if you need a specific OS kernel, custom drivers, or legacy software that won’t run in a container.

For a quantitative approach, compute a “fit score” where you assign weights to your requirements and compare options. For example, if you weight “cold start latency” at 0.4, “cost at low traffic” at 0.3, and “control” at 0.3, Functions score high on cost but low on control, while VMs score the opposite. The highest total wins.

Worked Example

Suppose you have an image-resizing service. Users upload photos, and you need to create thumbnails. The job takes about 30 seconds per image, runs only when a user uploads, and doesn’t need any persistent state between calls.

  1. Duration: 30 seconds – well under the typical 15-minute function limit.
  2. Statefulness: none – each image is independent.
  3. Control: only needs a standard runtime (e.g., Python or Node.js).

Score: Functions = 3/3, Containers = 2/3 (you’d pay for idle time), VMs = 1/3 (overkill). So Functions is the clear winner. If the same job took 2 hours and needed a GPU, you’d move to containers or VMs.

Background: The Three Compute Models

Cloud providers offer compute at three abstraction levels. Virtual machines (VMs) emulate a physical computer, giving you a full OS (Linux, Windows) that you manage. Containers package your application with its dependencies, but share the host OS kernel; they start in seconds and are more resource-efficient than VMs. Functions (also called Function-as-a-Service, FaaS) let you upload code that runs only when triggered, with the provider managing all infrastructure. The key difference is control vs. convenience: VMs give the most control, Functions the least, with containers in between.

How They Work Under the Hood

VMs use a hypervisor to allocate virtualized hardware (CPU, RAM, disk) to each guest OS. This isolation is strong but overhead-heavy. Containers use OS-level virtualization: the host kernel runs multiple isolated user-space instances (containers), each with its own filesystem and network. They share the kernel, so they’re lighter but less isolated. Functions are essentially containers that are created on-demand, run to completion, then destroyed. The provider handles scaling, patching, and availability. For example, when you call a function, the platform spins up a container, runs your code, and tears it down—often in under a second.

How to Choose: A Decision Framework

Follow these steps for any workload:

  1. List requirements: max execution time, memory, GPU/CPU needs, state storage, startup latency, and team expertise.
  2. Check time limits: Functions typically cap at 5-15 minutes per invocation; containers and VMs have no such limit.
  3. Check state: If you need to write to a local disk, use a VM or container with persistent volumes; functions require external storage (e.g., object storage or databases).
  4. Check scaling: Functions scale to zero (no cost when idle); containers and VMs scale but keep running.
  5. Check OS needs: If you need a specific kernel module or a Windows-only app, you must use a VM.

For example, a batch data-processing job that runs nightly for 3 hours: VMs are a good fit because they’re cheap for long-running, predictable workloads. A web API with variable traffic: containers (or functions if the API is simple) work well.

Common Problems and Misconceptions

A common mistake is assuming functions are always cheaper. They are cost-effective at low traffic, but if your service runs continuously (e.g., a web server), a container or VM is cheaper because functions charge per invocation and per GB-second. Another misconception is that containers are less secure than VMs. While VMs offer stronger isolation (each has its own kernel), containers are safe for most multi-tenant scenarios if configured correctly. Also, don’t forget cold starts: functions may take several seconds to respond after being idle, which is unacceptable for latency-sensitive apps. Finally, avoid vendor lock-in: each provider has its own function syntax and container orchestration tools, so design with portability in mind.

Cost Comparison

Pricing models differ significantly. VMs are billed per second or per hour for the allocated resources, regardless of usage. Containers are billed similarly, but because they share the OS, you can pack more into a single VM, reducing cost. Functions are billed per invocation plus compute time (GB-seconds). For example, a function that runs 100 times a day for 1 second each costs almost nothing, while a VM running 24/7 costs a fixed monthly amount. To estimate, use your provider’s pricing calculator, but remember to include egress (data transfer) costs, which are often the hidden expense.

Safety and Security Considerations

Security responsibilities vary. With VMs, you patch the OS and applications. With containers, you patch the base image and dependencies, and you must secure the orchestration layer (like Kubernetes). With functions, the provider patches the runtime, but you’re responsible for your code and any third-party libraries. Always follow the principle of least privilege: give each compute unit only the permissions it needs. For sensitive workloads, prefer VMs for stronger isolation, or use dedicated hosts if required by compliance. Never store secrets in code or environment variables that are visible in logs.

Alternatives and Hybrid Approaches

You don’t have to pick just one. Many applications use a mix: a VM for the database, containers for the backend services, and functions for background jobs. Kubernetes itself can run on VMs or bare metal, and can even run functions (e.g., with Knative). Serverless containers (like AWS Fargate or Azure Container Instances) let you run containers without managing servers, blurring the line. Also consider “scale-to-zero” for containers if you need longer runtimes than functions allow but still want cost savings during idle periods.

Frequently Asked Questions

Can I run containers inside a VM? Yes, that’s common. You run a VM with a Linux OS, then install a container runtime (like Docker) inside it. This adds a layer of isolation and is often used for development or when you need both VM-level security and container portability.

What is a cold start and why does it matter? A cold start is the delay when a function or container is invoked after being idle. The platform must allocate resources and start the runtime. For functions, this can be 1-5 seconds; for containers, it’s similar. If your workload is latency-sensitive, use an always-on container or VM.

How do I choose between Kubernetes and plain containers? Kubernetes is for managing many containers across multiple machines, providing scaling, load balancing, and self-healing. If you have a single container or a few, you don’t need it. Use Kubernetes when you have microservices that need to scale independently and you’re willing to handle its complexity.

Are functions always stateless? Yes, by design. Functions should not store state locally because they may be destroyed after each invocation. Use external services like databases or object storage for persistent data. Some providers offer “durable functions” that maintain state, but that’s an extension.

What’s the best option for a small startup? Start with functions for any event-driven tasks and containers for your main web service. Avoid VMs unless you have a specific OS requirement. This keeps costs low and scales with your growth.

Can I migrate from functions to containers later? Yes, but it requires refactoring. Functions are written as single-purpose handlers, while containers are full applications. Plan your code structure to be portable: keep business logic separate from the function handler, so you can wrap it in a web server later.

Related

Formula used
in t | Finish time | min | 50 | 0
in d | Distance | km | 10 | 0
out pace | Average pace | min/km | t / d | 2
out speed | Average speed | km/h | d / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.