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: Increase maxDuration 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:
  • Free/Hobby: 10 seconds max
  • Pro: 60 seconds max
  • Enterprise: 900 seconds max
  • 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:

  • Sequential database queries → Use parallel Promise.all() or SQL JOINs
  • Unoptimized API calls → Add request timeouts, implement caching
  • Large data processing → Stream responses or paginate results
  • Missing database indexes → Add indexes on frequently queried columns
  • Step 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

  • [Vercel environment variables not loading](/?guide=vercel-env-fix)
  • [Node.js memory leaks in serverless](/?guide=node-memory-leaks)
  • ---

    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.

    🔥 0d
    LIVE
    ↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising •↑ Resend loved by devs •↓ PlanetScale rage spiking •⚠ Vercel pricing complaints •↑ Railway gaining fast •↑ Supabase happiness rising
    DEVELOPER PAIN RADAR // Loading...

    Developers complain.
    Opportunities appear.

    We track what developers are struggling with today — and what opportunities that creates.

    —
    guides today
    —
    avg happiness
    🔥 Pain
    📖 Guides
    🔭 Explore
    👤 Mine
    🔥 Pain Radar — rage scores today
    ↗ share
    💡 Opportunity Feed — pain = market gap
    📈 Tool Momentum
    all scores →
    📖 Latest Guide
    all guides →
    📖 All Guides
    📊 Tool Scores
    + Submit
    📰 Hacker News
    ➕ Submit a Tool
    ← back