Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s timeout or cold starts spike memory. Increase function timeout, optimize bundle size, or enable concurrency.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function exceeded Vercel's 10-second execution timeout (or 60s for Pro/Enterprise) or cold starts are consuming your memory allocation.
Fix: Increase maxDuration in vercel.json, optimize dependencies, or split logic into background jobs.
---
Real Console Error Messages
Here are exact error patterns you'll see:
``` Error: Function execution took 12050ms, exceeding the 10000ms timeout. 504 Gateway Timeout ```
``` {"errorMessage":"Task timed out after 10.00 seconds","errorType":"TimeoutError"} ```
``` Duration: 10001.23 ms Billed Duration: 10001 ms Memory Size: 1024 MB Max Memory Used: 1087 MB ```
``` ERR_FUNCTION_TIMEOUT: Your serverless function exceeded the maximum execution time. Increasing maxDuration may help. Contact support for limits on your plan. ```
``` Fetch failed: upstream timeout at https://your-domain.vercel.app/api/endpoint (10s elapsed) ```
---
Broken Code → Fixed Code
Problem 1: Missing maxDuration Config
Broken: ```json { "buildCommand": "npm run build", "outputDirectory": "out" } ```
Fixed:
```json
{
"buildCommand": "npm run build",
"outputDirectory": "out",
"functions": {
"api/**/*.js": {
"maxDuration": 30
}
}
```
Note: maxDuration is in seconds. Hobby plan: max 10s. Pro: max 60s. Enterprise: up to 900s. We're uncertain if Vercel's limits changed in late 2025; check the [official Vercel docs](https://vercel.com/docs/functions/serverless-functions/runtimes#timeout-limits) for your current plan tier.
Problem 2: Synchronous Database Calls in Hot Loop
Broken:
```javascript
export default async function handler(req, res) {
const users = [];
for (let i = 0; i < 1000; i++) {
const user = await db.query(SELECT * FROM users WHERE id = ${i});
users.push(user);
}
res.status(200).json(users);
}
```
Fixed:
```javascript
export default async function handler(req, res) {
const users = await db.query(
SELECT * FROM users WHERE id IN (?, ?, ?, ...),
Array.from({length: 1000}, (_, i) => i)
);
res.status(200).json(users);
}
```
Problem 3: Large Dependencies Not Tree-Shaken
Broken: ```javascript import moment from 'moment'; // 67KB minified
export default function handler(req, res) { const date = moment().format('YYYY-MM-DD'); res.json({date}); } ```
Fixed: ```javascript // Use date-fns (12KB) or native Date API instead const date = new Date().toISOString().split('T')[0];
export default function handler(req, res) { res.json({date}); } ```
For Node.js 18+, the native Intl API is optimized.
---
Still broken? Check these too
1. Cold Start Overhead: First invocation after deploy takes 2-5s. If your baseline is already 8s, cold starts push you over. Use [Concurrency](/?guide=vercel-concurrency) settings or Reserved Instances on Pro/Enterprise to pre-warm functions.
2. External API Cascades: Your function waits for 3+ sequential HTTP calls (e.g., auth → database → cache). Add aggressive timeouts to upstream calls (2-3s each) and implement circuit breakers. Don't wait forever for slow services.
3. Memory Pressure & Garbage Collection: If your function uses 90%+ of allocated memory, GC pauses spike during execution. Increase memory allocation in vercel.json:
```json
"functions": {
"api/**/*.js": {
"memory": 3008
}
}
```
Higher memory = faster CPU + faster execution. See [related memory tuning guide](/?guide=vercel-memory-optimization).
---
Verification Steps
1. Check Vercel Logs:
```bash
vercel logs --follow
```
Filter by Duration and Max Memory Used to spot patterns.
2. Test Locally with vercel dev:
```bash
vercel dev
curl http://localhost:3000/api/endpoint
```
If it completes in <10s locally, the issue is production-specific (cold starts, regional latency, or load).
3. Add Timing Telemetry:
```javascript
const start = Date.now();
// ... your logic ...
console.log(Execution: ${Date.now() - start}ms);
```
---
Long-Term Prevention
vercel build should complete in <5 minutes. If not, you have unused dependencies.---
Official Resources
---
Found a different variation? Drop it in the comments. If you're hitting 504s with a different root cause (e.g., Postgres connection pooling, middleware overhead), let us know the exact error and your setup.