Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold start delay. Increase function timeout, optimize code, or split into background jobs.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is taking longer than Vercel's default 10-60 second timeout (depending on plan). Fix: Increase timeout invercel.json, optimize function execution time, or offload heavy work to background jobs.---
Real Error Messages from Console Output
``` Error: Task timed out after 10.00 seconds ```
``` 504 Gateway Timeout The request took too long to process and timed out. ```
``` {"errorCode":"FUNCTION_TIMEOUT","statusCode":504,"message":"Your function execution exceeded the timeout of 10 seconds"} ```
``` WARN Serverless Function timed out after 10000ms This typically happens when:
``` RuntimeError: Command timed out: Running process took longer than the 10-second limit ```
---
Broken Code vs. Fix
Problem 1: No Timeout Configuration
❌ BROKEN — api/process-data.js
```javascript
export default async function handler(req, res) {
// This runs until it hits the default 10s timeout
const data = await fetchLargeDataset();
const processed = await heavyComputation(data);
const result = await saveToDatabase(processed);
res.status(200).json({ success: true, result });
}
async function heavyComputation(data) { // Simulate 15 seconds of work return new Promise(r => setTimeout(() => r(data), 15000)); } ```
✅ FIXED — Add vercel.json timeout config
```json
{
"functions": {
"api/process-data.js": {
"memory": 3008,
"maxDuration": 60
}
}
}
```
Note on version specificity: As of January 2026, maxDuration supports up to 900 seconds on Pro/Enterprise plans, 60 seconds on Hobby plans. Verify your plan at [Vercel Pricing](https://vercel.com/pricing).
Problem 2: Unoptimized Database Queries
❌ BROKEN — api/users.js
```javascript
export default async function handler(req, res) {
// Fetches ALL users, then filters in-app (N+1 query pattern)
const allUsers = await db.query('SELECT * FROM users');
const filtered = allUsers.filter(u => u.status === 'active');
res.status(200).json(filtered);
}
```
✅ FIXED — Push filtering to database layer ```javascript export default async function handler(req, res) { // Database handles filtering (one optimized query) const activeUsers = await db.query( 'SELECT * FROM users WHERE status = ?', ['active'] ); res.status(200).json(activeUsers); } ```
Problem 3: Blocking Long Operations
❌ BROKEN — api/send-emails.js
```javascript
export default async function handler(req, res) {
const recipients = await getRecipients();
// Waits for ALL emails to send before responding
for (const recipient of recipients) {
await sendEmail(recipient);
}
res.status(200).json({ sent: recipients.length });
}
```
✅ FIXED — Offload to background job queue ```javascript import { Queue } from '@vercel/quickstart';
const emailQueue = new Queue('send-emails');
export default async function handler(req, res) { const recipients = await getRecipients(); // Queue job and return immediately await emailQueue.enqueue({ recipients }); res.status(200).json({ queued: true, jobId: 'process-in-background' }); } ```
---
Still broken? Check these too
1. Cold Start Delays: First invocation of a function takes 1-5 seconds. If your function logic is already 8+ seconds, cold start pushes it over the edge. Use [Vercel Pro with Concurrency](https://vercel.com/docs/functions/serverless-functions#max-duration) or pre-warm functions with cron jobs.
2. Memory Allocation Too Low: Functions with insufficient memory swap aggressively, slowing execution. Increase memory in vercel.json from default 1024MB to 3008MB. See [related Memory troubleshooting guide](/?guide=vercel-memory-limits).
3. External API Timeouts: Your function might be timing out *waiting* for third-party APIs (Stripe, OpenAI, databases). Add explicit client timeouts: ```javascript const response = await fetch(url, { signal: AbortSignal.timeout(5000) // 5 second max }); ```
4. Unzipped Bundle Size: Large dependencies slow cold starts. Check bundle size with vercel build --debug. Remove unused packages ([see Dependency optimization guide](/?guide=vercel-bundle-size)).
5. Synchronous File I/O: Reading/writing files blocks the event loop. Use streaming or async methods exclusively.
---
Configuration Reference
vercel.json for different needs:
```json { "functions": { "api/**/*.js": { "memory": 3008, "maxDuration": 60 }, "api/heavy-compute.js": { "memory": 3008, "maxDuration": 900, "regions": ["iad1"] } } } ```
Timeout limits by plan (2026):
---
Resources
---
Found a different variation?
Drop it in the comments—2am production fires are no joke, and community intel saves lives. Post your error message, code snippet, and what fixed it.