Vercel: 504 timeout on serverless functions [2026 fix]
Serverless functions exceed 10s timeout or memory limit. Increase function timeout, optimize code, or split into smaller functions.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function exceeds Vercel's 10-second default timeout or hits memory/CPU limits during execution. Fix: Increase function timeout invercel.json, optimize database queries, or split heavy operations into background jobs.---
Real Console Error Messages
Here are exact error outputs you'll see at 2am:
``` Error: FUNCTION_INVOCATION_TIMEOUT The function execution exceeded the timeout of 10s Request ID: arn:aws:lambda:us-east-1:1234567890:function:my-api-prod ```
``` HTTP/1.1 504 Gateway Timeout Content-Type: application/json {"errorMessage":"Task timed out after 10.00 seconds"} ```
``` Vercel Runtime Error: RequestTimeoutError at /var/task/api/handler.js:45 Final Status: RequestTimedOut ```
``` Internal Error: Lambda Invoke Response StatusCode: 504 FunctionError: Unhandled ```
``` Timeout waiting for response from runtime Max duration: 10s | Actual: 10.2s ```
---
Broken Code vs. Fix
Problem 1: Default 10-Second Timeout
BROKEN: ```javascript // api/heavy-processing.js export default async (req, res) => { // Complex operation: PDF generation, image processing, data aggregation const result = await processLargeFile(req.body.fileUrl); const analysis = await runAIAnalysis(result); const report = await generatePDFReport(analysis); res.json(report); // Takes 15+ seconds }; ```
FIXED: ```javascript // vercel.json { "functions": { "api/heavy-processing.js": { "maxDuration": 60, "memory": 3008 } } }
// api/heavy-processing.js export default async (req, res) => { // Return immediately, process in background res.status(202).json({ taskId: "task-123", status: "processing" }); // Non-blocking background job processLargeFile(req.body.fileUrl) .then(result => runAIAnalysis(result)) .then(analysis => generatePDFReport(analysis)) .then(report => storeInDatabase(report)) .catch(err => logError(err)); }; ```
Problem 2: Unoptimized Database Queries
BROKEN: ```javascript // api/user-dashboard.js export default async (req, res) => { const userId = req.query.id; // N+1 query problem: 1 query + 50 queries in loop const user = await db.query('SELECT * FROM users WHERE id = $1', [userId]); const orders = await db.query('SELECT * FROM orders WHERE user_id = $1', [userId]); for (let order of orders) { order.items = await db.query('SELECT * FROM items WHERE order_id = $1', [order.id]); // 50 more queries... } res.json({ user, orders }); // Timeout after 10s }; ```
FIXED: ```javascript // api/user-dashboard.js export default async (req, res) => { const userId = req.query.id; // Single JOIN query instead of 51 queries const result = await db.query(` SELECT u.*, o.id as order_id, o.total, i.id as item_id, i.name FROM users u LEFT JOIN orders o ON o.user_id = u.id LEFT JOIN items i ON i.order_id = o.id WHERE u.id = $1 `, [userId]); // Transform flat result into nested structure const user = normalizeUserData(result); res.json(user); // Completes in <2s }; ```
Problem 3: Missing Timeout Configuration
BROKEN: ```json { "buildCommand": "npm run build", "outputDirectory": ".next" } ```
FIXED: ```json { "buildCommand": "npm run build", "outputDirectory": ".next", "functions": { "api/**/*.js": { "maxDuration": 30, "memory": 1024 }, "api/reports/*.js": { "maxDuration": 120, "memory": 3008 }, "api/webhooks/*.js": { "maxDuration": 60 } } } ```
---
Version-Specific Notes
Vercel Functions (2024+): The examples above use current Vercel configuration. If you're on older Node.js runtimes (pre-14), timeout behavior may differ—check your Vercel dashboard runtime version.
Pro Plan limits: Free tier max 10s, Pro tier max 60s, Enterprise 15m. We're uncertain if this changes in 2026, so verify your current plan limits in Vercel dashboard.
---
Still Broken? Check These Too
1. [Cold starts causing timeouts](/?guide=vercel-cold-starts) — Functions under light traffic may timeout on first invoke. Enable pre-warming or Vercel Autoscaling.
2. [External API calls hanging](/?guide=api-timeout-patterns) — Third-party APIs (Stripe, SendGrid) blocking your function. Add explicit timeouts: fetch(url, { timeout: 5000 }).
3. Memory limit exceeded — If your function allocates >1024MB (or your limit), increase in vercel.json or reduce payload size.
---
Official Resources
---
Found a different variation? Drop it in the comments—whether it's a 504 from streaming responses, WebSocket timeouts, or background job delays, we want to hear it.