Vercel: 504 timeout on serverless functions [2026 fix]
Serverless function exceeds 10s default timeout or memory limit. Increase timeout/memory in vercel.json or optimize function execution.
Vercel: 504 timeout on serverless functions [2026 fix]
TL;DR
Cause: Your serverless function is taking longer than the 10-second default timeout (or hitting memory limits). Fix: IncreasemaxDuration in vercel.json and optimize slow database queries or external API calls.---
Exact Error Messages from Console
Here are real error signatures you'll see in Vercel logs:
``` Error: Task timed out after 10.00 seconds ```
``` 504 Gateway Timeout The request took too long to process and timed out. ```
``` FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory ```
``` vercel[api]: ERROR Function timed out after 10s (duration: 10004ms) ```
``` ERROR: Function execution took 12456ms, exceeding timeout of 10000ms for plan: pro ```
---
Code: Broken vs Fixed
❌ BROKEN: Default timeout, no optimization
```javascript // api/getData.js - TIMES OUT export default async function handler(req, res) { // No timeout configured - defaults to 10 seconds const data = await fetch('https://slow-api.example.com/data'); const json = await data.json(); // Multiple sequential database calls const user = await db.query('SELECT * FROM users WHERE id = ?', [req.query.id]); const posts = await db.query('SELECT * FROM posts WHERE user_id = ?', [user.id]); const comments = await db.query('SELECT * FROM comments WHERE post_id IN (?)', [posts.map(p => p.id)]); res.json({ user, posts, comments }); } ```
✅ FIXED: Timeout increased + optimized queries
vercel.json: ```json { "functions": { "api/getData.js": { "maxDuration": 30 } } } ```
api/getData.js: ```javascript export default async function handler(req, res) { // Parallel requests instead of sequential const [data, user, posts] = await Promise.all([ fetch('https://slow-api.example.com/data').then(r => r.json()), db.query('SELECT * FROM users WHERE id = ?', [req.query.id]), db.query('SELECT * FROM posts WHERE user_id = ? LIMIT 100', [req.query.id]) ]); // Single query with JOIN instead of multiple queries const comments = await db.query( 'SELECT c.* FROM comments c JOIN posts p ON c.post_id = p.id WHERE p.user_id = ? LIMIT 500', [req.query.id] ); res.json({ user, posts, comments }); } ```
---
Step-by-Step Fix
Step 1: Check Your Plan Limits
Vercel timeout limits depend on your plan:If you're on Free tier and need >10s, upgrade to Pro.
Step 2: Set maxDuration in vercel.json
```json
{
"functions": {
"api/**": {
"maxDuration": 30,
"memory": 1024
}
}
}
```Note on version specificity: This vercel.json syntax works as of 2024-2026. Vercel occasionally adjusts configuration schema, so verify against [official Vercel config docs](https://vercel.com/docs/projects/project-configuration).
Step 3: Optimize Your Function Code
Problem patterns:
Promise.all() or SQL JOINsStep 4: Add Request Timeout to External Calls
```javascript const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 5000);const response = await fetch('https://api.example.com', { signal: controller.signal });
clearTimeout(timeoutId); ```
Step 5: Deploy and Monitor
```bash vercel deploy ``` Check Vercel dashboard → Function logs → Duration column to verify improvement.---
Still broken? Check these too
1. Database connection pooling: If using Postgres/MySQL, your connection might timeout before the function does. Use connection pooling (PgBouncer, Neon) or implement connection reuse with pooling: true in your connection string.
2. Cold start compounding: Function initialization takes 2-5 seconds on first invocation. Pre-warm functions via [cron jobs](/?guide=vercel-cron-jobs) or use Vercel's native Postgres to avoid external connection overhead.
3. Memory exhaustion: 1024MB default memory might be insufficient. Increase via vercel.json memory field, but note higher memory = faster CPU and higher cost. Monitor actual usage in logs before over-allocating.
---
Related Guides
---
Official Resources
🔗 [Vercel Serverless Function Configuration](https://vercel.com/docs/functions/serverless-functions/runtimes#function-payload-api-version)
---
Found a different variation? Drop it in the comments—504s have many causes and your specific scenario helps others troubleshooting at 2am.