Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold start delay. Increase function timeout, optimize code execution, or switch to background jobs.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is taking longer than Vercel's 10-second timeout limit (Pro plan) or hitting cold start delays on infrequent requests.
Fix: Increase maxDuration in vercel.json, optimize database queries, or offload long-running tasks to background jobs.
---
Real Console Error Messages
Here are exact error outputs you'll see at 2am:
``` Error: Function ABCD1234 took 12015ms, exceeding timeout of 10000ms stack_trace: at /var/task/node_modules/serverless-runtime/timeout.js:45 ```
``` 504 Gateway Timeout Response from function was empty or timed out. Vercel Function ID: arn:aws:lambda:us-east-1:123456789:function:api-prod ```
``` WARNING: Task timed out after 10.00 seconds Max duration exceeded. Consider using Edge Functions or increasing maxDuration. Request ID: req_abc123xyz789 ```
``` DurationMs: 15234, Timeout: 10000 ERROR_LAMBDA_EXECUTION_TIMEOUT ```
``` status: 504, statusText: 'Gateway Timeout' headers: { 'x-vercel-id': 'sfo1::5x8kq-1234567890-abcdef' } ```
---
The Problem: Side-by-Side Code
❌ Broken Code (Default 10s timeout)
```javascript // api/user/profile.js - NO TIMEOUT CONFIG export default async function handler(req, res) { // Database query takes 12 seconds const user = await db.query(` SELECT * FROM users WHERE id = $1 AND related_posts IN (SELECT...) AND comments JOIN threads... `, [req.query.id]); // Cold start + heavy processing = timeout await processLargeDataset(user); res.json(user); } ```
✅ Fixed Code (Increased timeout + optimization)
```javascript // api/user/profile.js - WITH TIMEOUT CONFIG export const config = { maxDuration: 30, // Increase from default 10s to 30s memory: 3008, // Optional: increase memory for faster execution };
export default async function handler(req, res) { // Optimized query with index hints const user = await db.query(` SELECT id, name, email FROM users WHERE id = $1 `, [req.query.id]); // Return fast, process async in background if (user) { // Non-blocking: increment view count separately processLargeDataset(user).catch(console.error); } res.json(user); } ```
Alternative Fix: Use Background Jobs (Best for long tasks)
```javascript // api/user/profile.js - OFFLOAD TO BACKGROUND export default async function handler(req, res) { const user = await db.query( 'SELECT * FROM users WHERE id = $1', [req.query.id] ); // Queue task, return immediately await queue.enqueue('process-user', { userId: user.id }); res.json(user); }
// Background job (Inngest, Bull, or similar) export async function processUserBackground({ userId }) { await processLargeDataset({ id: userId }); } ```
---
Configuration: vercel.json
Set limits project-wide:
```json { "functions": { "api/**/*.js": { "maxDuration": 30 }, "api/public/**/*.js": { "maxDuration": 10 }, "api/heavy/**/*.js": { "maxDuration": 60, "memory": 3008 } } } ```
Timeout limits by plan:
---
Optimization Checklist
1. Add indexes to slow database queries
2. Cache responses with Cache-Control headers or Redis
3. Parallelize with Promise.all() instead of sequential awaits
4. Remove N+1 queries — use joins instead of loops
5. Stream large responses instead of buffering entire datasets
6. Use Edge Functions for simple logic (no timeout limit)
---
Still broken? Check these too
x-vercel-id headers to diagnose.---
Official Documentation
---
Common Pitfalls
⚠️ Uncertainty note: Timeout behavior changed between Vercel CLI v28 and v32. If your local build passes but production times out, verify you're testing with vercel dev (not next dev) to simulate actual serverless environment.
export const config — Config must be exported, not nested in default export---
Debug Command (Your 2am Lifesaver)
```bash
Check function logs with timing
vercel logs --tailLook for "DurationMs" in output — if > maxDuration, increase it
```---
Found a different variation? Drop it in the comments — Vercel's behavior changes between regions and plan tiers, so your edge case helps everyone.