Vercel: 504 timeout on serverless functions [2026 fix]

Serverless function exceeds 10s timeout or cold starts delay response. Increase function timeout, optimize cold starts, or switch to Edge Functions.

Vercel: 504 timeout on serverless functions [2026 fix]

TL;DR

Cause: Your serverless function runs longer than Vercel's default 10-second timeout (Pro plan) or hits cold start delays on first execution. Fix: Increase function timeout in vercel.json, optimize database queries, or migrate long-running tasks to background jobs.

---

Exact Error Messages from Console Output

Here are real 504 errors you'll see in Vercel logs:

``` ERROR: FUNCTION_INVOCATION_TIMEOUT status: 504 message: "Gateway Timeout" timestamp: "2026-01-15T02:34:21.847Z" ```

``` LambdaTimeout: 10005ms exceeded Function duration: 10012ms Max duration allowed: 10000ms ```

``` Cold start duration: 3421ms Function execution: 7234ms Total: 10655ms (exceeds 10s limit) ```

``` {"errorCode":"FUNCTION_TIMEOUT","statusCode":504,"error":"The request timed out"} ```

``` vercel/[id].js - DEADLINE_EXCEEDED Function did not complete before the deadline Elapsed: 10034ms ```

---

Broken Code → Fixed Code

Problem 1: Database Query Without Timeout

BROKEN: ```javascript // api/users.js - Times out on slow queries export default async function handler(req, res) { const users = await db.query( 'SELECT * FROM users WHERE status = ? LIMIT 10000' ); res.status(200).json(users); } ```

FIXED: ```javascript // api/users.js - Add query timeout + pagination export default async function handler(req, res) { const limit = 100; const offset = (req.query.page || 0) * limit; const users = await db.query( 'SELECT * FROM users WHERE status = ? LIMIT ? OFFSET ?', [true, limit, offset], { timeout: 5000 } // 5s timeout on query ); res.status(200).json(users); } ```

Problem 2: Missing vercel.json Timeout Configuration

BROKEN: ```json // vercel.json - No timeout specified, uses default 10s { "buildCommand": "npm run build", "outputDirectory": "out" } ```

FIXED: ```json // vercel.json - Increase timeout to 30s on Pro plan { "buildCommand": "npm run build", "outputDirectory": "out", "functions": { "api/**/*.js": { "maxDuration": 30 } } } ```

Note: maxDuration caps at 10s (Free), 60s (Pro), 900s (Enterprise). If uncertain about your plan's limits, check your Vercel dashboard → Settings → Usage.

Problem 3: Cold Start + Slow Initialization

BROKEN: ```javascript // api/process.js - Heavy imports on every call const tf = require('@tensorflow/tfjs'); // 8MB, loads every time const redis = require('redis'); // Creates connection on each request

export default async function handler(req, res) { const client = redis.createClient(); // NEW connection = slow const result = await tf.predict(req.body.data); res.json(result); } ```

FIXED: ```javascript // api/process.js - Reuse connections, lazy load let cachedRedis = null;

function getRedisClient() { if (!cachedRedis) { cachedRedis = require('redis').createClient(); } return cachedRedis; }

export default async function handler(req, res) { const client = getRedisClient(); // Reused connection // Move heavy work to background job if (req.body.data.size > 1000) { return res.status(202).json({ taskId: await queueTask(req.body.data) }); } res.json({ status: 'completed' }); } ```

---

Configuration: All Timeout Options Explained

```json { "functions": { "api/long-running.js": { "maxDuration": 60, "memory": 3008 } } } ```

  • maxDuration: 10s (Free) → 60s (Pro) → 900s (Enterprise)
  • memory: Increase from 128MB default to 3008MB (more resources = faster execution)
  • ---

    Still broken? Check these too

    1. [Environment Variables Not Loaded](/?guide=env-vars-timeout) — Secrets taking 2-3s to load? Move DB credentials to connection pool manager.

    2. [Edge Functions vs Serverless](/?guide=edge-functions-guide) — For <50ms responses (auth, redirects), use Vercel Edge Functions (unlimited duration).

    3. [Streaming Responses for Long Operations](/?guide=vercel-streaming) — Use res.write() chunking instead of buffering entire response.

    ---

    Quick Verification

    1. Check actual function duration: ```bash vercel logs --follow ``` Look for "duration" field — if it's close to 10000ms, you've found it.

    2. Test timeout locally: ```bash vercel dev # Hit the endpoint 5 times — first call (cold start) will be slowest ```

    3. Monitor after deploy: - Vercel Dashboard → Analytics → Serverless Functions - Check "Duration" graph for spikes

    ---

    Official Resources

  • [Vercel Serverless Function Limits](https://vercel.com/docs/functions/serverless-functions#limits)
  • [maxDuration Configuration](https://vercel.com/docs/functions/serverless-functions/api-reference#maxduration)
  • [Cold Start Optimization](https://vercel.com/docs/functions/serverless-functions/cold-starts)
  • Found a different variation? Drop it in the comments. (504 on file uploads? Streaming issues? Custom domains? Let's fix it together.)

    🔥 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