Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold start hits memory limit. Reduce execution time, increase function memory allocation, or split logic into smaller functions.
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/Enterprise: 60s) or hitting memory constraints during cold starts. Fix: Optimize code execution time, increase memory allocation invercel.json, or offload heavy operations to background jobs.---
Real Console Error Messages
Here are exact errors you'll see in Vercel logs, Sentry, or browser DevTools:
``` 504 Gateway Timeout Invocation failed with status 504. Output: ERROR: Task timed out after 10.00 seconds ```
``` VercelFunctionError: Your serverless function timed out. Please check your function's logs for more details. [504 Gateway Timeout] ```
``` ERRORRuntime.DeadlineExceeded The task did not complete within the allotted time. function execution time: 12453ms ```
``` Duration: 15234.56 ms Billed Duration: 15000 ms Memory Size: 512 MB Max Memory Used: 487 MB FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory ```
``` HTTP request took 11234ms to complete (function timeout: 10000ms) vercel-function-engine: Execution halted due to timeout threshold ```
---
Code Examples: Broken → Fixed
Problem 1: Long-Running Database Query
BROKEN CODE: ```javascript // api/users.js export default async function handler(req, res) { const users = await db.query( `SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id LEFT JOIN products ON orders.product_id = products.id WHERE users.created_at > NOW() - INTERVAL 30 DAY` ); // Takes 12+ seconds with 1M records res.json(users); } ```
FIXED CODE: ```javascript // api/users.js export default async function handler(req, res) { const users = await db.query( `SELECT id, name, email FROM users WHERE users.created_at > NOW() - INTERVAL 30 DAY LIMIT 100` ); // Add pagination, indexes, select only needed columns res.json(users); } ```
Problem 2: Missing Memory Configuration
BROKEN CODE (vercel.json): ```json { "functions": { "api/process.js": { "runtime": "nodejs18.x" } } } ```
FIXED CODE (vercel.json): ```json { "functions": { "api/process.js": { "runtime": "nodejs18.x", "memory": 3008, "maxDuration": 60 } } } ```
Note: maxDuration requires Pro/Enterprise plan. Free tier caps at 10s, 512MB.
Problem 3: Heavy Processing Without Offloading
BROKEN CODE: ```javascript // api/generate-report.js export default async function handler(req, res) { const data = await fetchLargeDataset(); // 5s const processed = heavyComputation(data); // 8s const pdf = generatePDF(processed); // 6s - TIMEOUT! res.json({ url: pdf }); } ```
FIXED CODE: ```javascript // api/generate-report.js export default async function handler(req, res) { const jobId = generateUUID(); // Queue job immediately await queue.push({ jobId, action: 'generate-report', data: req.body }); res.status(202).json({ jobId }); // Return immediately }
// background/process-report.js (background job, 15min timeout)
export default async function processReport(job) {
const data = await fetchLargeDataset();
const processed = heavyComputation(data);
const pdf = generatePDF(processed);
await storage.save(reports/${job.jobId}.pdf, pdf);
}
```
---
Configuration Best Practices
Optimal vercel.json for most use cases: ```json { "functions": { "api/**/*.js": { "memory": 1024, "maxDuration": 30 } } } ```
Memory allocation reference:
---
Still broken? Check these too
1. [Cold Start Delays](/?guide=vercel-cold-start) — First invocation takes 5-7 seconds. Use serverless-http or upgrade to Pro for auto-warming.
2. [External API Timeouts](/?guide=external-api-timeout) — Your function waits for slow third-party APIs. Implement request timeouts and retry logic with exponential backoff.
3. [Memory Leak in Node.js Process](/?guide=nodejs-memory-leak) — Function uses 500MB+ on each call. Profile with clinic.js or check for unclosed database connections.
---
Official Documentation
[Vercel Serverless Function Limits Documentation](https://vercel.com/docs/functions/serverless-functions#limits)
[AWS Lambda Timeout Configuration](https://docs.aws.amazon.com/lambda/latest/dg/limits.html) (Vercel uses Lambda under the hood)
---
Debugging Checklist
vercel logs <function-name>console.time() / console.timeEnd()vercel dev with same execution constraintsFound a different variation? Drop it in the comments.