Vercel: 504 timeout on serverless functions [2026 fix]
Serverless functions exceed 10s execution limit or memory; increase timeout in vercel.json and optimize cold starts.
TL;DR
Cause: Your serverless function exceeded Vercel's 10-second timeout or hit memory limits during execution.
Fix: Add "maxDuration": 60 to your vercel.json function config and optimize cold starts.
---
Real Console Error Messages
``` 1. Error: FUNCTION_INVOKE_TIMEOUT_EXCEEDED 504 Gateway Timeout at /api/process-data Invocation timed out after 10000ms
2. Error: Task timed out after 10.00 seconds at Timeout._onTimeout [as _callback] at processTimers (internal/timers.js:1621:51)
3. FetchError: request to https://yourapp.vercel.app/api/handler failed reason: socket hang up code: 'ECONNRESET'
4. ERROR [Error]: Serverless Function exceeded maximum size of 250MB Duration: 10023.45 ms Memory Used: 512 MB
5. 504 GATEWAY_TIMEOUT x-vercel-id: iad1::abc123 RequestId: xyz789 ```
---
Code Comparison: Broken vs Fixed
❌ Broken Code
api/process-data.js ```javascript export default async function handler(req, res) { // Long-running operation without timeout config const results = await expensiveDatabaseQuery(); const processed = await complexCalculation(results); // This takes 15+ seconds but Vercel kills it at 10s await saveToS3(processed); res.json({ success: true }); }
// Cold start overhead adds 2-3 seconds const expensiveDatabaseQuery = async () => { const client = new DatabaseClient(); // Initialized on every call return client.query('SELECT * FROM large_table'); }; ```
vercel.json ```json { "functions": { "api/**": { "memory": 512 } } } ```
---
✅ Fixed Code
api/process-data.js ```javascript // Reuse client connection (persistent across warm invocations) let cachedClient;
const getDatabaseClient = async () => { if (cachedClient) return cachedClient; cachedClient = new DatabaseClient(); await cachedClient.connect(); return cachedClient; };
export default async function handler(req, res) { try { const client = await getDatabaseClient(); // Optimized query with limits const results = await client.query( 'SELECT * FROM large_table LIMIT 1000' ); // Process in chunks instead of all at once const processed = await processInChunks(results, 100); // Use async S3 upload without waiting for completion saveToS3(processed).catch(err => console.error(err)); res.json({ success: true, itemsProcessed: results.length }); } catch (error) { console.error('Handler error:', error); res.status(500).json({ error: error.message }); } }
const processInChunks = async (data, chunkSize) => { const results = []; for (let i = 0; i < data.length; i += chunkSize) { const chunk = data.slice(i, i + chunkSize); results.push(await complexCalculation(chunk)); } return results.flat(); }; ```
vercel.json ```json { "functions": { "api/process-data.js": { "maxDuration": 60, "memory": 1024 }, "api/**": { "maxDuration": 30, "memory": 512 } } } ```
---
Why This Works
1. maxDuration: 60 — Extends timeout from default 10s to 60s (Pro plan allows up to 900s)
2. Connection reuse — Eliminating cold-start database initialization saves 2-3 seconds per invocation
3. Chunked processing — Breaks large operations into smaller batches, preventing memory exhaustion
4. Increased memory — 1024MB functions start faster and have more room for operations
5. Fire-and-forget S3 — Doesn't wait for upload confirmation; returns response immediately
Important note about version-specific behavior: As of 2026, Vercel Pro plans support maxDuration up to 900 seconds, but free tier remains at 10 seconds. We cannot confirm if this has changed in newer Vercel versions—check [official Vercel limits](https://vercel.com/docs/functions/serverless-functions/runtimes#maxduration).
---
Still Broken? Check These Too
1. [Environment variables not loading](/?guide=vercel-env-vars) — Missing database credentials cause connection timeouts; verify .env.local and Vercel project settings
2. [Cold starts killing performance](/?guide=vercel-cold-starts) — Dependencies bundle size >50MB? Use dynamic imports and tree-shake unused code
3. Dependency size bloat — Run npm ls --depth=0 and remove unused packages like lodash or moment; each MB adds 50-100ms to cold start
---
Quick Checklist
vercel.json with appropriate maxDuration?vercel analyze?git push or redeploy button)?---
Reference
---
Found a different variation? Drop it in the comments—especially if you hit this on Edge Runtime or encountered 504s from database connection pooling issues.