Vercel: 504 timeout on serverless functions [2026 fix]

Serverless functions exceed 10s default timeout or hit memory limits. Increase function timeout and optimize cold start performance.

TL;DR

Cause: Your Vercel serverless function is exceeding the 10-second default timeout or hitting memory constraints during execution.

Fix: Increase the maxDuration in vercel.json to 60 seconds and optimize cold starts by reducing dependencies.

---

Exact Error Messages

Here are real console outputs you'll see at 2am:

``` Error: FUNCTION_INVOCATION_TIMEOUT 504 Gateway Timeout Your serverless function exceeded the 10 second time limit. Function: /api/process-data Duration: 12340ms ```

``` EXCEPTION_IN_HANDLER: Task timed out after 10.01 seconds at Runtime.invoke (/var/task/index.js:145:67) ERR_CODE: TASK_TIMEOUT ```

``` Vercel Function Logs: [ERROR] Runtime exceeded maximum duration of 10000ms Remaining execution budget: -2340ms Function: db-query ```

``` {"errorCode":"FUNCTION_TIMEOUT","message":"Serverless function timed out after 10000ms","functionName":"api/heavy-computation","region":"iad1"} ```

``` Duration: 15234ms | Billed Duration: 15234ms | Memory Used: 1024 MB 504 Internal Server Error - Gateway Timeout ```

---

Broken Code vs. Exact Fix

Problem 1: Missing Timeout Configuration

BROKEN: ```json // vercel.json - NO timeout set, defaults to 10 seconds { "functions": { "api/**/*.js": { "memory": 1024, "includeFiles": "dist/**" } } } ```

FIXED: ```json // vercel.json - Explicitly set maxDuration { "functions": { "api/**/*.js": { "memory": 3008, "maxDuration": 60, "includeFiles": "dist/**" } } } ```

Problem 2: Synchronous Database Calls in Serverless

BROKEN: ```javascript // api/process-data.js - Blocking operation export default async function handler(req, res) { const users = db.query('SELECT * FROM users'); // Blocks entire function const processed = users.map(heavyComputation); res.json(processed); }

function heavyComputation(user) { // CPU-intensive work, no timeout let result = 0; for (let i = 0; i < 1_000_000_000; i++) { result += Math.sqrt(i); } return result; } ```

FIXED: ```javascript // api/process-data.js - Optimized with timeout + streaming export const config = { maxDuration: 60, memory: 3008, };

export default async function handler(req, res) { try { // Use Promise.race to enforce hard timeout const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error('Function timeout')), 55000) ); const processingPromise = (async () => { // Paginate database results instead of fetching all at once const BATCH_SIZE = 100; const results = []; for (let offset = 0; offset < 10000; offset += BATCH_SIZE) { const batch = await db.query( 'SELECT * FROM users LIMIT ? OFFSET ?', [BATCH_SIZE, offset] ); // Process smaller batches const processed = batch.map(user => ({ id: user.id, value: quickComputation(user.data) })); results.push(...processed); } return results; })(); const data = await Promise.race([processingPromise, timeoutPromise]); res.json(data); } catch (error) { res.status(503).json({ error: error.message }); } }

function quickComputation(data) { // Keep computation lightweight return data.value * 2; } ```

Problem 3: Large Dependency Bundle

BROKEN: ```javascript // api/ml-model.js - Massive library loads on every invocation import tf from '@tensorflow/tfjs'; // 2.5MB import transformers from 'transformers'; // 850KB

export default async function handler(req, res) { // Cold start includes full dependency load: +8 seconds const model = await transformers.pipeline('sentiment-analysis'); // ... } ```

FIXED: ```javascript // api/ml-model.js - Lazy load + external service import { callExternalMLService } from '../lib/ml-client';

export const config = { maxDuration: 60, memory: 1024, // Reduced from 3008 };

export default async function handler(req, res) { // Offload heavy ML to dedicated service (Lambda layer, Modal, Replicate) const result = await callExternalMLService(req.body.text); res.json(result); } ```

---

Configuration Details

Note on version behavior: As of 2026, Vercel's Pro plan supports maxDuration up to 60 seconds. Enterprise plans allow up to 900 seconds. We're uncertain if this changes in mid-2026 pricing updates—verify current limits at [Vercel pricing page](https://vercel.com/pricing).

The maxDuration setting in vercel.json applies globally; use per-function config exports to override per-endpoint.

---

Still broken? Check these too

1. [Cold starts slowing your app?](/?guide=vercel-cold-start) — Implement connection pooling and use Vercel KV for session caching instead of database queries on every invocation.

2. [Memory errors alongside 504s?](/?guide=vercel-memory-optimization) — Check if your function actually needs 3008MB or if library tree-shaking would reduce it to 512MB, improving cold start by 40%.

3. [504s only on specific regions?](/?guide=vercel-regional-routing) — Your function might timeout in iad1 but work in sfo1. Configure region-specific memory allocation or use regions config.

---

Official Documentation

  • [Vercel Serverless Function Configuration](https://vercel.com/docs/concepts/functions/serverless-functions/api-reference)
  • [Function Timeout and Duration Limits](https://vercel.com/docs/concepts/functions/serverless-functions#maxduration)
  • [Optimizing Cold Start Performance](https://vercel.com/docs/concepts/functions/serverless-functions/tips)
  • ---

    Found a different variation? Drop it in the comments.

    🔥 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