TechBuzzPro

Microservices: Scale to Zero or Keep Capacity Warm?

· 5 min read · Apps & Browsers

In this article
  1. Background: The Serverless vs. Always-On Debate
  2. How Scale-to-Zero Works
  3. How to Choose: Key Factors
  4. Common Problems and Misconceptions
  5. Cost Comparison: Real Numbers
  6. Hybrid Approaches and Alternatives
  7. Safety and Reliability Considerations
  8. Frequently Asked Questions

You should scale to zero only for non-critical, event-driven workloads with tolerant latency, while keeping capacity warm for user-facing services that require predictable performance. The right choice depends on your traffic patterns, cost constraints, and tolerance for cold starts.

Background: The Serverless vs. Always-On Debate

The scale-to-zero model became popular with serverless platforms like AWS Lambda, which automatically spin up resources only when a request arrives. This offers dramatic cost savings during idle periods, but introduces cold starts—the delay while the platform provisions a container and initializes your code. Cold starts can range from a few hundred milliseconds to several seconds, depending on the runtime, package size, and platform. In contrast, keeping capacity warm (via provisioned concurrency, reserved instances, or a traditional always-on cluster) eliminates cold starts but incurs continuous costs, even when traffic is low.

How Scale-to-Zero Works

In a scale-to-zero architecture, the platform monitors incoming requests and scales the number of running instances down to zero when there are no requests. When a new request arrives, the platform must:

  • Allocate a new execution environment (e.g., a container or a VM).
  • Load the runtime and your application code.
  • Execute any initialization logic (e.g., connecting to databases, loading config).
  • Return the response.

This process, known as a cold start, adds latency to the first request in a burst. Subsequent requests reuse the warm instance until it is idle for a timeout period (often 5–15 minutes), after which it is recycled.

How to Choose: Key Factors

Deciding between scale-to-zero and warm capacity requires evaluating several factors:

  • Traffic pattern: If your service experiences long idle periods (e.g., a nightly batch job, an internal tool used occasionally), scale-to-zero is cost-effective. If traffic is steady or unpredictable with spikes, warm capacity ensures responsiveness.
  • Latency sensitivity: User-facing APIs, payment processing, and real-time features typically require sub-second responses. Cold starts can break that requirement. Internal async jobs can tolerate seconds of delay.
  • Cost: Scale-to-zero charges only for compute time during execution. Warm capacity charges for the entire time the instance is allocated, even if idle. For low-traffic services, scale-to-zero can reduce costs by 70–90%.
  • Startup time: A simple Node.js function may start in under 200ms, but a Java/Spring Boot service with heavy dependencies can take 5–10 seconds. The longer the startup, the more painful the cold start.
  • State and dependencies: If your service holds in-memory state or needs to maintain connections to databases, frequent scaling to zero can cause connection churn and data loss. Warm capacity keeps these resources alive.

Common Problems and Misconceptions

A common misconception is that scale-to-zero is always cheaper. In reality, if your service has a steady baseline of traffic, the cost of repeated cold starts (and the extra compute time for initialization) can exceed the cost of keeping a small number of instances warm. Another issue is the thundering herd problem: when a burst of traffic hits a cold service, the platform must spin up many instances simultaneously, which can lead to resource contention and even longer cold starts. Additionally, some platforms have concurrency limits that can cause requests to be throttled or queued during scale-up events.

Cost Comparison: Real Numbers

Let's compare two hypothetical services with identical traffic: 100,000 requests per month, each taking 200ms of compute time.

  • Scale-to-zero (AWS Lambda): Compute cost = 100,000 × 0.2s = 20,000 GB-seconds. With the free tier (400,000 GB-seconds), the cost is $0.00. After free tier, at $0.0000166667 per GB-second, that's $0.33 per month.
  • Warm capacity (a t3.micro EC2 instance running 24/7): Cost = 730 hours × $0.0104/hour = $7.59 per month. That's 23 times more expensive.

However, if the service is idle only 50% of the time, the scale-to-zero cost remains $0.33, but the warm instance still costs $7.59. The gap widens as traffic drops. But if you have a service that runs continuously (e.g., a message consumer), the warm instance may be more cost-effective because you avoid paying for initialization time on every invocation.

Hybrid Approaches and Alternatives

You don't have to choose one extreme. Many teams adopt a hybrid strategy:

  • Provisioned concurrency: Cloud providers like AWS offer provisioned concurrency, which keeps a specified number of instances warm while still allowing scale-to-zero for the rest. This gives you the best of both worlds: low latency for a predictable baseline, and cost savings for spikes.
  • Warm-up triggers: Schedule a ping (e.g., a cron job) to hit your service every few minutes to keep it warm. This is a simple trick for serverless functions, but it consumes resources and may not prevent cold starts if the ping interval exceeds the idle timeout.
  • Mixed architecture: Use scale-to-zero for asynchronous, non-critical jobs (e.g., image resizing, report generation) and keep warm capacity for synchronous APIs that need consistent latency.

Safety and Reliability Considerations

Scaling to zero can introduce reliability risks if not handled carefully:

  • Timeouts: If your service has a long startup time, the client may time out before the function responds. Set appropriate timeouts and consider using asynchronous patterns (e.g., queues) for long-running tasks.
  • Data consistency: If your service writes to a database, cold starts can cause connection pool exhaustion. Use connection pooling that survives warm instances, or re-establish connections lazily.
  • Monitoring: Cold starts can be hard to detect without proper tracing. Use distributed tracing to identify cold start latency and set alerts for abnormal delays.
  • Failover: If your service scales to zero, ensure that the rest of your system can handle the absence of the service (e.g., via retries and fallbacks).

Frequently Asked Questions

What is a typical cold start time?

Cold start times vary widely by platform and language. For AWS Lambda, Python and Node.js functions typically start in 200–500ms, while Java and .NET can take 1–5 seconds. Container-based platforms like AWS Fargate can take 10–30 seconds to pull images and start.

Can I keep a serverless function warm for free?

No, keeping a function warm requires either provisioned concurrency (which costs money) or periodic pings (which consume compute time). The pings themselves incur costs, though they may be minimal if you use a free tier.

Does scale-to-zero work for databases?

Some serverless databases (like Aurora Serverless) can scale to zero, but this is not recommended for production because it causes connection delays and potential data loss. Most databases should remain warm.

How do I measure if scale-to-zero is cheaper?

Calculate your total compute time per month (requests × average duration) and compare it to the cost of a reserved instance that runs 24/7. Include cold start overhead in your compute time estimate. If your compute time is less than 10% of the month, scale-to-zero is likely cheaper.

What happens to in-flight requests during a scale-down?

Platforms typically allow in-flight requests to complete before shutting down an instance. However, if the instance is forced down (e.g., due to a timeout), requests may be dropped. Design your service to be idempotent and handle retries.