Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold starts fail. Increase function timeout, optimize code, or switch to longer-running service.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is exceeding Vercel's execution timeout (10 seconds default, max 60 seconds on Pro) or taking too long to respond. Fix: IncreasemaxDuration in your vercel.json, optimize slow database queries, or offload work to background jobs.---
Exact Error Messages
Here are real console outputs you'll see at 2am:
``` 504 Gateway Timeout The request took too long to process and timed out. ```
``` Error: Function crashed with signal 'SIGKILL'. {"errorMessage":"Task timed out after 10.00 seconds"} ```
``` 504 Service Unavailable The serverless function did not respond within the timeout period. Request duration: 10234ms ```
``` Vercel Deployment Error: api/user.js exceeded maximum duration of 10s ```
``` Gateway Timeout Did not receive a timely response from upstream server. X-Vercel-Error-Code: FUNCTION_INVOCATION_TIMEOUT ```
---
Broken Code vs. Fix
Problem 1: Default 10s timeout on slow database query
BROKEN:
```javascript
// api/reports.js - takes 15 seconds to generate report
export default async function handler(req, res) {
const data = await db.query(
SELECT * FROM orders WHERE date > ? AND status = ?,
[req.query.startDate, 'completed']
); // This query takes 12 seconds
const processed = data.map(item => calculateMetrics(item));
res.json(processed);
}
```
FIXED:
```javascript
// api/reports.js
export default async function handler(req, res) {
// Add explicit timeout configuration
const data = await db.query(
SELECT * FROM orders WHERE date > ? AND status = ?,
[req.query.startDate, 'completed']
);
const processed = data.map(item => calculateMetrics(item));
res.json(processed);
}
// vercel.json - increase max duration { "functions": { "api/reports.js": { "maxDuration": 30 } } } ```
Problem 2: Missing optimization + timeout config
BROKEN: ```javascript // api/process.js - loops through 10,000 records export default async function handler(req, res) { const users = await db.getAllUsers(); for (let user of users) { await sendEmail(user.email); await logActivity(user.id); } res.json({ processed: users.length }); } ```
FIXED: ```javascript // api/process.js - offload to background job export default async function handler(req, res) { const users = await db.getAllUsers(); // Immediately queue job and return await queueBackgroundJob('send-emails', { userIds: users.map(u => u.id) }); res.json({ processed: users.length, status: 'queued' }); }
// background-job/send-emails.js - runs separately, can use longer timeout export default async function handler(req, res) { const { userIds } = req.body; for (let userId of userIds) { await sendEmail(userId); } }
// vercel.json { "functions": { "api/process.js": { "maxDuration": 10 }, "background-job/send-emails.js": { "maxDuration": 60 } } } ```
---
Configuration Details
vercel.json structure (exact format required): ```json { "functions": { "api/slow-endpoint.js": { "maxDuration": 30, "memory": 1024 }, "api/reports/**": { "maxDuration": 60 } } } ```
Timeout limits by plan:
I'm uncertain whether Vercel's timeout calculation includes cold-start time in all regions—this behavior may vary by deployment region and runtime version.
---
Still Broken? Check These Too
1. Cold start cascades: If your function imports heavy libraries (NumPy, ML models), initialization alone exceeds 5+ seconds. Consider pre-warming with [scheduled jobs](/?guide=vercel-cron) or moving to container images.
2. Memory exhaustion: Functions with 512MB default memory swap to disk (extreme slowdown). Try "memory": 1024 or 3008 in vercel.json. Monitor CloudWatch/Vercel logs for OOM kills.
3. External API timeouts: Your function waits 15s for Stripe/payment API while Vercel cuts you off at 10s. Implement timeouts on external calls: axios.create({ timeout: 5000 }).
4. Database connection pooling: Creating fresh DB connection per request adds 2-3s latency. Use [pooling libraries](/?guide=database-pooling) or managed serverless database.
5. Synchronous file operations: Reading large files from /tmp blocks the event loop. Use streams and async operations instead.
---
Deployment Check
After fixing vercel.json:
```bash
vercel deploy --prod
Check function configuration
vercel ls ```Test with: ```bash time curl https://your-app.vercel.app/api/endpoint ```
---
Official Resources
[Vercel Serverless Functions Documentation](https://vercel.com/docs/concepts/functions/serverless-functions)
---
Found a different variation? Drop it in the comments—include your error message and vercel.json config so we can add it to this guide.