Balancing Cost and Latency: Using IBM Cloud Code Engine’s Scale‑to‑Zero for Serverless Containers
Learn how to balance cost savings and latency when using IBM Cloud Code Engine’s scale‑to‑zero. A step‑by‑step guide shows deployment, tuning concurrency, measuring cold starts, and handling trade‑offs.
21 May 2026, 20:54 UTC

Problem: Idle Resource Costs vs. Latency‑Sensitive APIs
Many teams deploy containerized services on IBM Cloud Code Engine (CE) to avoid managing Kubernetes clusters. When traffic spikes are unpredictable, the default “always‑running” behavior can lead to idle compute costs. At the same time, turning off instances too often can introduce cold‑start latency that hurts user experience.
Thesis: Tune Scale‑to‑Zero and Concurrency to Match Your Workload
Code Engine’s scale‑to‑zero automatically terminates container instances when no requests are received for a configurable period. By adjusting the --concurrency flag and the idle timeout, you can find the sweet spot between saving money and keeping latency low.
Section 1 – Understanding the Mechanics
When a request arrives after an idle period, CE must (1) pull the image, (2) start a new container instance, and (3) route the request to it. The time for these steps is the cold‑start latency. The platform’s default idle timeout is 5 minutes, but you can change it with the --scale-to-zero-duration option.
Concurrency controls how many simultaneous requests a single instance can handle. If concurrency is set to 1, each request will trigger a new instance after the previous one finishes. Setting it higher can reduce the number of scale‑up events but may increase per‑request latency if the instance is overloaded.
Section 2 – Worked Example: Deploy a Node.js Echo Service
Below is a minimal setup to illustrate the effect of scale‑to‑zero and concurrency. Replace placeholders with your values.
# 1. Build a simple Docker image
# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package.json .
RUN npm install --only=prod
COPY index.js .
CMD ["node", "index.js"]
# Build and push to IBM Container Registry
ibmcloud cr login
ibmcloud cr build --tag ${REGION}.cloud.ibm.com/${ORG}/${IMAGE_NAME}:latest .
ibmcloud cr push ${REGION}.cloud.ibm.com/${ORG}/${IMAGE_NAME}:latest
# 2. Deploy to Code Engine with concurrency=5 and 10‑minute scale‑to‑zero
ibmcloud ce app create \
--name my-echo \
--image ${REGION}.cloud.ibm.com/${ORG}/${IMAGE_NAME}:latest \
--concurrency 5 \
--scale-to-zero-duration 10m \
--memory 256Mi \
--cpu 1
After deployment, the console shows the app name and the current number of instances. When traffic stops, the Instances metric drops to zero after 10 minutes.
Section 3 – Measuring Cold Start Latency
To quantify the impact, use a simple curl test. Run these commands from a machine with network access to the app’s public endpoint.
# Warm request (after a load burst)
curl -w "%{time_total}\n" -o /dev/null -s https://my-echo.${REGION}.codeengine.appdomain.cloud/echo
# Cold request (after 15 minutes of inactivity)
curl -w "%{time_total}\n" -o /dev/null -s https://my-echo.${REGION}.codeengine.appdomain.cloud/echo
Typical cold starts on CE range from 1–3 seconds for small images, but larger images or network issues can push this higher. Compare the two times to see the cost of scaling to zero.
Section 4 – Trade‑offs and Limitations
- Cold Start Latency – Every scale‑to‑zero event adds 1–3 s latency. For latency‑sensitive APIs, consider a longer idle timeout or a higher concurrency setting.
- Stateful Workloads – CE instances use local, volatile storage. If your app requires persistent state, scale‑to‑zero is unsuitable.
- Thrashing – Rapid, erratic traffic can cause frequent scale‑up/scale‑down cycles, increasing cost and potentially degrading performance. Use a custom metric or a longer idle timeout to mitigate.
- Image Size – Larger images increase cold‑start time. Keep images lean and use multi‑stage builds.
Actionable Checklist
- Set
--concurrencyto match your typical request rate (start with 5). - Choose a scale‑to‑zero timeout that balances cost and acceptable latency (e.g., 10–15 min).
- Deploy a test app and monitor the
Instancesmetric for at least 24 h. - Measure cold start latency before and after adjusting the timeout.
- If latency is unacceptable, increase concurrency or extend the idle timeout.
- For stateful services, move to a managed Kubernetes or VM deployment.
By iteratively tuning these parameters, you can keep idle costs low while maintaining a responsive user experience.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.