Vercel: 504 timeout on serverless functions [2026 fix]

Serverless functions exceeding 10s timeout or hitting memory limits. Increase function timeout, optimize code, or split into multiple functions.

Vercel: 504 timeout on serverless functions [2026 fix]

TL;DR

Cause: Your serverless function is taking longer than the 10-second default timeout limit or consuming too much memory. Fix: Increase the timeout in vercel.json to 60 seconds (max for hobby plans) and optimize database queries.

---

Exact Error Messages from Console

Here are real console outputs you'll see:

``` Error: ETIMEDOUT - Connection timeout at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:47) at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:144:72) ```

``` 504 Gateway Timeout The request took too long to complete. Vercel Serverless Function Timeout ```

``` FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 1: 0x10c4e60 node::Abort() [node] 2: 0x10c4e60 node::OnFatalError(char const*, char const*) [node] ```

``` Error: Task timed out after 10.05 seconds at Timeout._onTimeout (internal/timers.js:1120:35) ```

``` ERR_HTTP_REQUEST_TIMEOUT: The operation was aborted at new NodeError (internal/errors.js:288:64) at abortOnTimeout (internal/http.js:1234:58) ```

---

Broken Code vs. Fixed Code

Problem 1: No Timeout Configuration

BROKEN: ```json { "buildCommand": "npm run build", "outputDirectory": "dist" } ```

FIXED: ```json { "buildCommand": "npm run build", "outputDirectory": "dist", "functions": { "api/**": { "maxDuration": 60 } } } ```

Problem 2: Inefficient Database Query

BROKEN: ```javascript export default async (req, res) => { const users = await db.query('SELECT * FROM users'); const posts = await db.query('SELECT * FROM posts'); const comments = await db.query('SELECT * FROM comments'); const likes = await db.query('SELECT * FROM likes'); res.json({ users, posts, comments, likes }); }; ```

FIXED: ```javascript export default async (req, res) => { // Add pagination and indexes const users = await db.query( 'SELECT id, name, email FROM users LIMIT 100 OFFSET 0' ); const posts = await db.query( 'SELECT id, title FROM posts WHERE user_id IN (?) LIMIT 50', [users.map(u => u.id)] ); // Use Promise.all for parallel queries instead of sequential const [comments, likes] = await Promise.all([ db.query('SELECT * FROM comments LIMIT 50'), db.query('SELECT * FROM likes LIMIT 50') ]); res.json({ users, posts, comments, likes }); }; ```

Problem 3: No Connection Pooling

BROKEN: ```javascript import mysql from 'mysql';

export default async (req, res) => { const conn = mysql.createConnection({ host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD }); conn.connect(); const result = await conn.query('SELECT * FROM data'); conn.end(); res.json(result); }; ```

FIXED: ```javascript import mysql from 'mysql';

const pool = mysql.createPool({ connectionLimit: 5, host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD });

export default async (req, res) => { pool.query('SELECT * FROM data LIMIT 100', (error, results) => { if (error) return res.status(500).json({ error: error.message }); res.json(results); }); }; ```

---

Important Notes on Version Behavior

Uncertainty acknowledged: Vercel's maximum timeout varies by plan tier. I'm stating defaults as of 2026, but Vercel may have updated these limits. Always verify your current plan's limits in the [Vercel dashboard](https://vercel.com/docs/functions/serverless-functions) before relying on specific timeout values.

  • Hobby Plan: 10-second default, 60-second maximum
  • Pro Plan: 15-second default, 300-second maximum
  • Enterprise: Custom limits available
  • ---

    Still broken? Check these too

    1. Cold Starts: First invocation after 15 minutes of inactivity adds 5-10 seconds. Use [Vercel Cron](https://vercel.com/docs/cron-jobs) to warm up functions. See our guide on [optimizing cold start performance](/?guide=cold-start-optimization).

    2. Environment Variables Missing: Database credentials not loading causes silent failures and retries. Verify DB_HOST, DB_USER, DB_PASSWORD are set in Vercel project settings under "Environment Variables."

    3. External API Delays: Calling slow third-party APIs without timeout wrapping causes cascade failures. Implement fetch timeout: fetch(url, { signal: AbortSignal.timeout(5000) }).

    4. Memory Exhaustion: Uploading/processing large files exceeds 3GB memory limit. Split processing into multiple functions or use [Vercel Postgres](/?guide=serverless-database) instead.

    5. Regional Latency: Database located far from function region. Deploy to same region or use read replicas.

    ---

    Official Documentation

    [Vercel Serverless Functions - Official Docs](https://vercel.com/docs/functions/serverless-functions)

    ---

    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