Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold starts fail; increase timeout + optimize code paths.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is taking longer than Vercel's default 10-second timeout (Pro plan) or hitting memory limits during execution.
Fix: Increase function timeout in vercel.json and optimize cold start performance by reducing bundle size.
---
Real Console Error Messages
Here are exact error outputs you'll see:
``` Error 504: GATEWAY_TIMEOUT The request took too long to process and timed out. Duration: 10000 ms ```
``` Error: Task timed out after 10.01 seconds at Runtime.invokeAsync [as handler] (file:///var/task/api/route.js:1:1) ```
``` 504 Gateway Timeout Vercel: The lambda took too long to respond. X-Vercel-Id: sfo1::xyz123 ```
``` WARNING: Cold start duration exceeded 5000ms (5.2s) Function initialization: 3400ms User code execution: 1800ms ```
``` Errror: ECONNREFUSED when calling external API Timeout waiting for database connection after 10000ms ```
---
Broken Code vs. Fixed Code
Problem 1: Default Timeout Too Short
BROKEN: ```javascript // api/process-data.js - No timeout configuration export default async function handler(req, res) { const data = await fetchLargeDataset(); // Takes 12+ seconds const processed = await complexCalculation(data); // 5+ seconds res.status(200).json(processed); } ```
FIXED: ```json // vercel.json { "functions": { "api/process-data.js": { "maxDuration": 30 } } } ```
```javascript // api/process-data.js - Same handler, now with sufficient timeout via config export default async function handler(req, res) { const data = await fetchLargeDataset(); const processed = await complexCalculation(data); res.status(200).json(processed); } ```
Problem 2: Synchronous Operations Blocking Execution
BROKEN: ```javascript // api/webhook.js export default async function handler(req, res) { // Returns response immediately but continues processing res.status(200).json({ received: true }); // This runs AFTER response sent, eats timeout budget await processWebhookAsync(req.body); // 15 seconds await sendNotifications(); // 8 seconds } ```
FIXED: ```javascript // api/webhook.js import { scheduleTask } from '@vercel/functions';
export default async function handler(req, res) { // Queue background work immediately scheduleTask(async () => { await processWebhookAsync(req.body); await sendNotifications(); }); // Return response without waiting res.status(200).json({ received: true, queued: true }); } ```
Problem 3: Massive Bundle Size Causing Cold Starts
BROKEN: ```javascript // api/analyze.js import TensorFlow from 'tensorflow'; // 50MB import LargeLibrary from 'huge-lib'; // 40MB import * as AllUtils from './utils/everything.js'; // 15MB
export default async function handler(req, res) { const result = TensorFlow.analyze(req.body); res.json(result); } ```
FIXED: ```javascript // api/analyze.js // Dynamic imports - only load when needed let TensorFlow;
export default async function handler(req, res) { if (!TensorFlow) { TensorFlow = await import('tensorflow'); } const result = TensorFlow.analyze(req.body); res.json(result); } ```
```json // vercel.json - Also configure for Node.js 20+ faster runtime { "buildCommand": "npm run build", "functions": { "api/**/*.js": { "memory": 3008, "maxDuration": 25 } } } ```
---
Configuration Notes
Timeout Limits by Plan:
*We are uncertain whether maxDuration honors fractional seconds (e.g., 10.5) in all Vercel runtimes as of 2026—test before production if sub-second precision matters.*
---
Still broken? Check these too
1. Database Connection Pooling — Each new function invocation creates fresh connections. Implement connection pooling with serverless-compatible libraries like node-postgres with max: 1 per function.
2. External API Rate Limiting — If calling third-party APIs, add exponential backoff + retry logic. A 504 often masks downstream timeouts. See [related: API rate limit strategies](/?guide=api-retry-patterns).
3. Memory Exhaustion — Increase function memory in vercel.json from default 1024MB to 3008MB. Monitor actual usage in Vercel dashboard; memory contention causes CPU throttling. See [related: memory optimization](/?guide=serverless-memory).
---
Official Documentation
[Vercel Functions Timeout Limits](https://vercel.com/docs/functions/serverless-functions/runtimes#timeout)
[Vercel maxDuration Configuration](https://vercel.com/docs/projects/project-configuration#functions)
---
Found a different variation? Drop it in the comments below—especially if you encountered 504s from specific database drivers, image processing libraries, or regional latency issues.