Microservice Patterns
Deployment Patterns
Several services per host, one per VM, one per container, or serverless functions: how each packs, isolates and scales services — and why serverless trades idle cost for cold starts.
What you will learn
- Compare the main ways to deploy service instances.
- Understand isolation, density and startup time trade-offs.
- See the cold-start cost of scale-to-zero platforms.
- Choose a deployment platform for a given workload.
Before you start
Overview
You can keep a car in the garage (always ready, paid for even when parked), rent one for the week, or call a taxi (pay per trip, but you wait for it to arrive). Running software has the same choices.
How service instances are packaged and run decides their isolation, density, start-up time and cost. The main options: multiple services per host (cheap, no isolation), one service per VM (strong isolation, slow to start, coarse), one service per container on a platform such as Kubernetes (good isolation, seconds to start, dense packing — the common default), and serverless functions (no servers to manage, scale to zero, pay per request — with cold starts).
Run it
A webhook endpoint gets three 4-second bursts and silence in between. Compare Instances running (what you pay for) and p99 for always-on containers (A) and serverless functions (B).
Comparison
| Pattern | Isolation | Start-up | Idle cost | Good for |
|---|---|---|---|---|
| Several services per host | Weak (shared runtime/OS) | Fast | Low | Small systems, legacy app servers |
| Service per VM | Strong | Minutes | High | Strict isolation, special OS needs |
| Service per container | Good | Seconds | Medium (min replicas) | Most microservices |
| Serverless function | Good (managed) | 0.1–2 s cold start | None | Spiky or rare, event-driven work |
Reducing cold starts
Resources:
WebhookFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: java21
Handler: com.example.Webhook::handle
MemorySize: 1024
SnapStart: { ApplyOn: PublishedVersions } # restore a snapshot instead of booting the JVM
AutoPublishAlias: live
ProvisionedConcurrencyConfig: { ProvisionedConcurrentExecutions: 2 } # two always warmTrade-offs
| Benefits | Costs | |
|---|---|---|
| Cost | Serverless costs nothing when idle; containers pack densely. | Serverless can cost more at sustained high load; VMs waste capacity. |
| Latency | Always-on instances answer immediately. | Scale-from-zero adds cold starts to the first requests of a burst. |
| Control | Containers and VMs let you tune runtime, networking and long-running work. | Functions limit duration, memory and runtime; platform lock-in grows. |
Production considerations
- Default to containers on a managed platform; use functions for glue, events and spiky jobs.
- Measure cold starts with your real dependencies (DB connections, SDK init), not a hello-world.
- Keep a small warm minimum for latency-sensitive paths.
- Whatever the platform, keep services stateless and configurable from outside.