Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold start delay. Increase function timeout, optimize dependencies, or split into smaller functions.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is taking longer than the 10-second default timeout (or 60-second max limit) to respond. Fix: IncreasemaxDuration in your function config, optimize heavy dependencies, or refactor long-running operations to background jobs.---
Exact Error Messages from Console Output
``` 504: GATEWAY_TIMEOUT A request to a function on your deployments took too long to respond. ```
``` Error: Function execution took longer than 60000 milliseconds at Runtime.invokeFunction (internal/handler.js:245:12) ```
``` FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 8: 0x7f1234567890 v8::internal::Factory::NewFixedArray ```
``` Vercel Function Logs: Duration: 45000ms | Status: Timeout ```
``` WARNING: cold start detected Initialization Duration: 8500ms (function execution: 2000ms) Total: 10500ms > 10000ms limit ```
---
Broken Code vs. Fixed Code
Problem 1: Default timeout too short
BROKEN: ```javascript // api/process-data.js - NO timeout config export default async function handler(req, res) { const data = await complexDatabaseQuery(); const processed = await expensiveComputation(data); const result = await thirdPartyAPI(processed); res.status(200).json(result); } ```
FIXED: ```javascript // api/process-data.js export const config = { maxDuration: 30 // seconds (max 60 for Pro plans, 10 for Hobby) };
export default async function handler(req, res) { const data = await complexDatabaseQuery(); const processed = await expensiveComputation(data); const result = await thirdPartyAPI(processed); res.status(200).json(result); } ```
Problem 2: Heavy dependency causing cold starts
BROKEN: ```javascript // api/generate-pdf.js const PDFDocument = require('pdfkit'); // 3MB+ library const sharp = require('sharp'); // 15MB+ native bindings const aws = require('aws-sdk');
export default async function handler(req, res) { // 8+ seconds just loading libraries on cold start const doc = new PDFDocument(); // ... } ```
FIXED: ```javascript // api/generate-pdf.js export const config = { maxDuration: 25, memory: 1024 // Allocate more memory to speed up execution };
// Load only when needed (lazy loading) let pdfLib; async function getPDFLib() { if (!pdfLib) { pdfLib = require('pdfkit'); } return pdfLib; }
export default async function handler(req, res) { const PDFDocument = await getPDFLib(); const doc = new PDFDocument(); // ... } ```
Problem 3: Synchronous/blocking operations
BROKEN: ```javascript // api/sync-users.js export default async function handler(req, res) { const users = await db.query('SELECT * FROM users'); // Blocking operation - waits for ALL to complete for (const user of users) { await sendEmail(user.email); await logActivity(user.id); await updateCache(user.id); } res.json({ sent: users.length }); } ```
FIXED: ```javascript // api/sync-users.js export const config = { maxDuration: 30 };
export default async function handler(req, res) { const users = await db.query('SELECT * FROM users'); // Return immediately, process in background Promise.all(users.map(user => processUserAsync(user).catch(err => console.error(err)) )).then(() => { // Background work continues after response }).catch(console.error); // Return early res.status(202).json({ message: 'Processing started', count: users.length }); }
async function processUserAsync(user) { await sendEmail(user.email); await logActivity(user.id); await updateCache(user.id); } ```
---
How to Check Your Current Limits
Hobby Plan (Default):
Pro Plan:
Check your plan: [Vercel Dashboard → Settings → Plan](https://vercel.com/account/plan)
---
Still Broken? Check These Too
1. Cold Start Penalties: New deployments or after idle periods add 5-8 seconds. Use [Vercel's "Prerender on Demand"](/?guide=vercel-prerender-setup) to warm up functions. Monitor cold start duration in Vercel Logs → Metrics.
2. Database Connection Issues: Slow database queries are the #1 cause. Enable connection pooling (e.g., PgBouncer for PostgreSQL), use indexes, and test query speed locally with EXPLAIN ANALYZE.
3. External API Timeouts: Calling slow third-party APIs? Implement request timeouts and retry logic. See [related guide on API timeout handling](/?guide=api-timeout-patterns).
---
Verification Steps
1. Deploy with maxDuration set
2. Check Vercel Logs: vercel logs <deployment-url>
3. Look for Duration: field—should be under your limit
4. Test from different regions (use --cron or manual triggers)
5. Monitor function memory in Vercel Analytics
---
Official Resources
---
Found a different variation? Drop it in the comments—include your error message, Vercel plan, and fix so we can add it here.