Cloud Function Timing Out on Cold Start? Fix the Warmup Request
Cold start timeout usually means your function takes too long to initialize. The fix is adding a warmup request, not upgrading your hardware.
You hit that 504 or "deadline exceeded" error on the first call to your cloud function. That's a cold start timeout. You're not alone — it's one of the most common serverless pains. Here's a fix that really works.
The Quick Fix: Add a Warmup Request
The idea is simple. You make a harmless request to the function right after it starts. This forces the runtime to load all your dependencies and establish database connections before the real request comes in.
- Create a warmup endpoint in your function code. In Node.js (Google Cloud Functions example):
exports.warmup = (req, res) => { res.status(200).send('Warm'); };For Python (AWS Lambda):
def warmup_handler(event, context): return { 'statusCode': 200, 'body': 'Warm' } - Deploy the function with the new endpoint. After you deploy, test it by calling the warmup endpoint once manually. You should see a quick 200 response.
- Set up a Cloud Scheduler (or cron job) to hit the warmup endpoint every 3-5 minutes. Here's a basic Cloud Scheduler config:
- Target: HTTP
- URL:
https://your-region-your-project.cloudfunctions.net/your-function/warmup - Method: GET
- Frequency:
*/3 * * * *(every 3 minutes)
After saving the scheduler, wait a minute, then call the main function. You should see the timeout disappear. The function stays warm now.
Why This Fix Works
Cloud functions shut down after being idle for a while — usually 5-15 minutes depending on the provider. When a new request comes, the runtime has to start from scratch. That's the cold start. It has to load your code, import modules, and run any global initialization.
If your initialization takes longer than the function's timeout (often 60 seconds but you set it lower), you get a timeout. The warmup request keeps the function alive. It resets the idle timer. So when the real request comes, the function is already running. It's like keeping the engine idling instead of turning it off and restarting it each time.
One thing I see a lot: people try to fix this by increasing the function timeout. That just masks the problem. The real fix is to make the function start faster or keep it warm. And keeping it warm is cheaper than reserving a VM.
Less Common Variations of the Same Issue
Not all cold start timeouts happen the same way. Here are a few I've seen in the field:
| Scenario | Cause | Fix |
|---|---|---|
| Database connection timeout on first call | You're opening a DB connection inside the handler, not during initialization. | Move the connection to global scope so it's created once at cold start, not per request. |
| Large module or framework (like express, tensorflow) loading slowly | Heavy imports delay the first request. | Lazy-load non-critical modules. Or use a lighter framework. Or keep the function warm. |
| Throttled cold starts during traffic spikes | Provider queues requests when lots of cold starts happen at once. | Set minimum instances (Google Cloud Run) or provisioned concurrency (AWS Lambda). That's costly but works. |
If you get timeouts even after the warmup request hits, check your initialization code. Sometimes a global variable setup throws an error silently. Add logging at the top of your function to confirm the warmup request is being processed.
How to Prevent This Going Forward
Three things you need to do:
- Set a realistic timeout for your function. 30 seconds minimum for cold starts. But don't go above 60 unless you have to. Long timeouts cost more and hide slow code.
- Use provisioned concurrency if your provider supports it. It keeps a few instances always warm. For AWS Lambda, that's Provisioned Concurrency. For Google Cloud Functions, it's min instances. This costs more but eliminates cold starts entirely.
- Review your initialization code every time you add a new library. A simple
require('heavy-library')can add 2-3 seconds to cold start. Use the warmup request as a canary — if it starts taking longer, you know something changed.
One last thing: don't try to fix this by tweaking memory or CPU alone. More memory helps a little, but it's not the main cause. Focus on warmup and initialization.
That's it. You should be good now. If you still see timeouts after this, check your function logs for actual error messages. Sometimes the timeout is a symptom of something else — like a runaway loop or external API that hangs.
Was this solution helpful?