Vercel: 504 timeout on serverless functions [2026 fix]

Serverless function exceeds 10s timeout or cold start delay. Increase function timeout, optimize code execution, or switch to background jobs.

Vercel: 504 timeout on serverless functions [2026 fix]

TL;DR

Cause: Your serverless function is taking longer than Vercel's 10-second timeout limit (Pro plan) or hitting cold start delays on infrequent requests.

Fix: Increase maxDuration in vercel.json, optimize database queries, or offload long-running tasks to background jobs.

---

Real Console Error Messages

Here are exact error outputs you'll see at 2am:

``` Error: Function ABCD1234 took 12015ms, exceeding timeout of 10000ms stack_trace: at /var/task/node_modules/serverless-runtime/timeout.js:45 ```

``` 504 Gateway Timeout Response from function was empty or timed out. Vercel Function ID: arn:aws:lambda:us-east-1:123456789:function:api-prod ```

``` WARNING: Task timed out after 10.00 seconds Max duration exceeded. Consider using Edge Functions or increasing maxDuration. Request ID: req_abc123xyz789 ```

``` DurationMs: 15234, Timeout: 10000 ERROR_LAMBDA_EXECUTION_TIMEOUT ```

``` status: 504, statusText: 'Gateway Timeout' headers: { 'x-vercel-id': 'sfo1::5x8kq-1234567890-abcdef' } ```

---

The Problem: Side-by-Side Code

❌ Broken Code (Default 10s timeout)

```javascript // api/user/profile.js - NO TIMEOUT CONFIG export default async function handler(req, res) { // Database query takes 12 seconds const user = await db.query(` SELECT * FROM users WHERE id = $1 AND related_posts IN (SELECT...) AND comments JOIN threads... `, [req.query.id]); // Cold start + heavy processing = timeout await processLargeDataset(user); res.json(user); } ```

✅ Fixed Code (Increased timeout + optimization)

```javascript // api/user/profile.js - WITH TIMEOUT CONFIG export const config = { maxDuration: 30, // Increase from default 10s to 30s memory: 3008, // Optional: increase memory for faster execution };

export default async function handler(req, res) { // Optimized query with index hints const user = await db.query(` SELECT id, name, email FROM users WHERE id = $1 `, [req.query.id]); // Return fast, process async in background if (user) { // Non-blocking: increment view count separately processLargeDataset(user).catch(console.error); } res.json(user); } ```

Alternative Fix: Use Background Jobs (Best for long tasks)

```javascript // api/user/profile.js - OFFLOAD TO BACKGROUND export default async function handler(req, res) { const user = await db.query( 'SELECT * FROM users WHERE id = $1', [req.query.id] ); // Queue task, return immediately await queue.enqueue('process-user', { userId: user.id }); res.json(user); }

// Background job (Inngest, Bull, or similar) export async function processUserBackground({ userId }) { await processLargeDataset({ id: userId }); } ```

---

Configuration: vercel.json

Set limits project-wide:

```json { "functions": { "api/**/*.js": { "maxDuration": 30 }, "api/public/**/*.js": { "maxDuration": 10 }, "api/heavy/**/*.js": { "maxDuration": 60, "memory": 3008 } } } ```

Timeout limits by plan:

  • Hobby: 10s max
  • Pro: 60s max
  • Enterprise: 900s (contact support)
  • ---

    Optimization Checklist

    1. Add indexes to slow database queries 2. Cache responses with Cache-Control headers or Redis 3. Parallelize with Promise.all() instead of sequential awaits 4. Remove N+1 queries — use joins instead of loops 5. Stream large responses instead of buffering entire datasets 6. Use Edge Functions for simple logic (no timeout limit)

    ---

    Still broken? Check these too

  • [Cold Start Delays](/?guide=vercel-cold-start) — Serverless functions sleep after inactivity. Test with x-vercel-id headers to diagnose.
  • [Memory Exhaustion](/?guide=vercel-memory-limits) — Insufficient RAM slows garbage collection. Increase memory in config or reduce payload sizes.
  • [External Service Timeouts](/?guide=external-api-timeout) — Your function completes fast but waits on third-party API. Implement circuit breakers and fallbacks.
  • ---

    Official Documentation

  • [Vercel Serverless Functions Docs](https://vercel.com/docs/functions/serverless-functions)
  • [maxDuration Configuration](https://vercel.com/docs/functions/serverless-functions/max-duration)
  • [Edge Functions (no timeout)](https://vercel.com/docs/edge-functions)
  • ---

    Common Pitfalls

    ⚠️ Uncertainty note: Timeout behavior changed between Vercel CLI v28 and v32. If your local build passes but production times out, verify you're testing with vercel dev (not next dev) to simulate actual serverless environment.

  • Forgetting export const config — Config must be exported, not nested in default export
  • Mixing Edge + Serverless — Edge Functions can't access databases; use serverless for DB queries
  • Not accounting for cold starts — First invoke after 30min of inactivity adds 2-5s latency
  • ---

    Debug Command (Your 2am Lifesaver)

    ```bash

    Check function logs with timing

    vercel logs --tail

    Look for "DurationMs" in output — if > maxDuration, increase it

    ```

    ---

    Found a different variation? Drop it in the comments — Vercel's behavior changes between regions and plan tiers, so your edge case helps everyone.

    🔥 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