Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold starts delay response. Increase function timeout, optimize code execution, or switch to Vercel Functions with longer limits.
TL;DR
Cause: Your serverless function exceeded Vercel's 10-second timeout (Hobby) or 60-second timeout (Pro), or cold starts are blocking responses.
Fix: Increase function timeout in vercel.json, optimize database queries, or migrate long-running tasks to background jobs.
---
Real Console Error Messages
``` 504 Gateway Timeout Did not complete before the request timeout limit. ```
``` Error: Task timed out after 10.00 seconds at ChildProcess.<anonymous> (/var/task/handler.js:45:12) ```
``` FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory Serverless function timed out before memory error surfaced ```
``` [ERROR] RequestId: abc123xyz Failed to load response data for POST /api/process status code: 504 from Vercel edge network ```
``` vercel[api]: FunctionComputeError: The request failed to return a response before the timeout. Duration: 10000ms ```
---
Problem: Broken Code vs. Fixed Code
❌ BROKEN: Default timeout with slow query
```javascript // api/process.js - NO timeout config export default async function handler(req, res) { // This database query takes 15 seconds const results = await db.query( `SELECT * FROM users WHERE status = 'active' AND created_at > NOW() - INTERVAL '1 year' AND verified = true` ); // Never reaches here on Hobby plan (10s limit) res.status(200).json({ data: results }); } ```
✅ FIXED: Increase timeout + optimize query
```javascript // vercel.json { "functions": { "api/process.js": { "maxDuration": 30 } } } ```
```javascript // api/process.js - Optimized with timeout config export const config = { maxDuration: 30 };
export default async function handler(req, res) { // Add database indexes and use pagination const results = await db.query( `SELECT id, name, email FROM users WHERE status = 'active' AND created_at > NOW() - INTERVAL '1 year' LIMIT 100`, { timeout: 8000 } // Query-level timeout ); res.status(200).json({ data: results }); } ```
---
Root Causes & Solutions
1. Function Timeout Limit Exceeded
Vercel Hobby plan: 10-second limit Vercel Pro plan: 60-second limit Vercel Enterprise: Up to 3600 seconds
Solution: Add maxDuration to vercel.json or use the config export.
```javascript export const config = { maxDuration: 45 // Pro plan supports this }; ```
2. Cold Start Delays
First invocation loads dependencies (Node modules, connections), consuming 2-5 seconds before your code runs.
Solution:
```javascript // ✅ Reuse connection let cachedClient;
export default async function handler(req, res) { if (!cachedClient) { cachedClient = new Pool({ connectionString: process.env.DB_URL }); } const result = await cachedClient.query('SELECT NOW()'); res.json({ time: result.rows[0] }); } ```
3. Synchronous Database Calls
Blocking operations don't use Promise concurrency.
Solution: Use Promise.all() for parallel queries.
```javascript // ✅ Parallel execution const [users, posts, comments] = await Promise.all([ db.query('SELECT * FROM users LIMIT 10'), db.query('SELECT * FROM posts LIMIT 10'), db.query('SELECT * FROM comments LIMIT 10') ]); ```
4. Memory Exhaustion
Large data processing hits memory limits (1024MB default).
Solution: Stream data instead of loading entirely.
```javascript // ✅ Stream large datasets import { createReadStream } from 'fs';
export default async function handler(req, res) { const stream = createReadStream('/tmp/large-file.json'); stream.pipe(res); } ```
---
Still Broken? Check These Too
1. [Vercel Environment Variables Not Loading](/?guide=vercel-env-vars) — Missing DB_URL causes connection retries (adds 5+ seconds)
2. [Node.js Cold Start Optimization](/?guide=nodejs-cold-start) — Unused imports and heavy middleware increase startup time
3. [Background Jobs with Vercel Cron](/?guide=vercel-background-jobs) — Move long operations off the request path entirely
---
Verification Checklist
maxDuration valuevercel dev locally first---
Official Resources
---
Found a different variation? Drop it in the comments
If you hit a 504 with a different root cause—rate limiting, firewall blocks, CDN issues—share your error trace and solution below. Tag @vercel-help for faster responses.