Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or memory limit. Increase function timeout, optimize code, or split into faster endpoints.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your Vercel serverless function is either taking longer than the 10-second default timeout or hitting memory constraints.
Fix: Increase the function timeout in vercel.json, optimize database queries with connection pooling, or offload heavy processing to background jobs.
---
Real Console Error Messages
Here are exact error outputs you'll see:
``` ERROR: FUNCTION_TIMEOUT The serverless function exceeded the maximum execution time of 10s. Request ID: xxx-xxx-xxx Duration: 10000ms ```
``` 504 Gateway Timeout vercel.com The request took too long to process and was terminated. ```
``` Error: connect ETIMEDOUT 192.168.x.x:5432 ERR_HTTP_REQUEST_TIMEOUT at TCPConnectWrap.afterConnect ```
``` FATAL ERROR: CALL_USAGE_EXCEEDED Function invoked too many times. Memory exhausted. Heap out of memory at 512MB (Pro plan limit) ```
``` WARNING: Cold start taking 8234ms Function initialization + execution = 10234ms TOTAL ```
---
The Problem: Code Side-by-Side
❌ BROKEN CODE
```javascript
// api/process-data.js - DEFAULT 10s TIMEOUT
export default async function handler(req, res) {
// No timeout configuration
const data = await fetch('https://slow-api.example.com/data');
const json = await data.json();
// Looping through 50k records synchronously
let result = [];
for (let i = 0; i < 50000; i++) {
const processed = await database.query(
SELECT * FROM users WHERE id = ${i}
);
result.push(processed);
}
res.json(result);
}
```
Problems:
✅ FIXED CODE
Step 1: Set timeout in vercel.json
```json { "functions": { "api/process-data.js": { "maxDuration": 30, "memory": 1024 } } } ```
Step 2: Optimize the function itself
```javascript // api/process-data.js - OPTIMIZED import { pool } from '../lib/db';
export default async function handler(req, res) { try { // Use connection pooling (reusable connections) const client = await pool.connect(); // Single query instead of 50k queries const { rows } = await client.query( 'SELECT * FROM users LIMIT 50000' ); client.release(); // Process in parallel batches, not sequentially const batchSize = 100; const results = []; for (let i = 0; i < rows.length; i += batchSize) { const batch = rows.slice(i, i + batchSize); const processed = await Promise.all( batch.map(row => processUserData(row)) ); results.push(...processed); } res.status(200).json({ count: results.length, data: results.slice(0, 100) // Paginate response }); } catch (error) { console.error('Function error:', error); res.status(500).json({ error: 'Processing failed' }); } }
async function processUserData(user) { // Lightweight processing return { ...user, processed_at: new Date() }; } ```
Step 3: For long operations, use queues
If processing *must* take >30s, offload to [background jobs](/?guide=vercel-cron-jobs):
```javascript // api/queue-processing.js - RETURNS IMMEDIATELY export default async function handler(req, res) { const jobId = crypto.randomUUID(); // Queue the work asynchronously fetch(process.env.QUEUE_URL, { method: 'POST', body: JSON.stringify({ jobId, data: req.body }) }).catch(err => console.error(err)); // Return immediately (no timeout) res.json({ jobId, status: 'queued' }); } ```
---
Configuration Details
maxDuration limits by plan:
I'm explicitly uncertain about: Vercel may have adjusted Pro tier limits between 2025-2026; verify in your account dashboard under "Function settings."
---
Still broken? Check these too
1. Cold starts + timeout combined: Initialization takes 3-5s, leaving only 5-7s for actual work. Solution: Use [Edge Functions](/?guide=vercel-edge-functions) for latency-sensitive code, or keep functions warm with scheduled pings.
2. Database connection pooling missing: Every function spawn creates a new connection. Fix: Use connection pools (e.g., pg-pool) or serverless-compatible databases (Supabase, PlanetScale).
3. Large response payloads: Returning 50MB of data causes timeout. Solution: Stream responses, paginate results, or compress with gzip middleware.
---
Official Resources
---
Found a different variation? Drop it in the comments—504 errors manifest differently across databases and runtimes.