Web Development

Next.js serverless API on Vercel returns intermittent 504s for long upstream requests when HTTP keep-alive sockets are exhausted

Environment

Environment

Next.js 13 API routes deployed as serverless functions on Vercel (Platform timeout ~60s), backend microservice hosted on internal cluster behind an HTTP reverse proxy (keep-alive enabled), upstream service uses Node.js with HTTP/1.1 keep-alive; client code uses node-fetch in Next.js functions.

Symptoms

Symptoms

Occasional long-running API calls (30–55s) return HTTP 504 from Vercel while quick requests succeed. Logs show Vercel returned timeout without upstream response. Reproducible when many concurrent long requests run; fewer concurrent clients succeed.

Observations

Observations

Upstream reports many sockets in CLOSE_WAIT and TIME_WAIT; proxy's connection pool reaches max open connections. The Next.js function uses a shared HTTP agent with keepAlive: true but without limit on maxSockets, or in some deployments uses the default global agent which reuses sockets. Increasing the Vercel function timeout did not eliminate 504s. When instrumenting upstream with a 'Connection: close' response, the 504 rate drops.

Already attempted

Already attempted

Tried increasing function timeout and concurrency on upstream; configured fetch to set 'Connection: close' header (reduced 504s). Replaced global agent with a new agent per request (fixed but increased CPU). Implemented upstream keep-alive timeout reductions — helped but didn't fully eliminate failures under peak concurrency.

Question

Question

How can I prevent intermittent 504 errors for long-running requests from Next.js serverless functions without disabling keep-alive entirely or dramatically increasing resource usage?

COMMUNITY RESPONSES

Verified community troubleshooting

0 replies

No answers yet..

Showing 0 of 0 community responses
AI DIAGNOSTICS

Incident analysis

Updated 4w ago• Trouble ID

Most likely causes

Unbounded HTTP keep-alive agents letting serverless frontends exhaust upstream connection pools under concurrent long requests 85%
Upstream reverse proxy misconfiguration (low max connections, improper reuse) causing sockets to accumulate in CLOSE_WAIT 60%
Vercel platform execution/connect timeout triggers before upstream completes under bursts, unrelated to socket pools 45%

Summary

Root cause is connection pool exhaustion between the serverless function pool and upstream. Serverless functions can be short-lived and high-concurrency; using persistent keep-alive without bounding sockets causes too many idle/half-closed connections, and upstream may reach its max connections or hold sockets in CLOSE_WAIT. Short-term fixes (Connection: close) work but kill benefits of keep-alive. The correct approach is to use a connection agent configured with sensible maxSockets, idle socket timeout, and proper socket reuse, or to use a streaming response/edge function model so connections are not blocked for long durations.

Suggested Fixes

Diagnostic step 1
Instrument and capture socket stats on upstream and proxy (ss -s, netstat -an | grep -E 'CLOSE_WAIT|TIME_WAIT') during a burst to confirm pool exhaustion.
Diagnostic step 2
Configure an HTTP agent for node-fetch/undici with limited maxSockets (e.g., 50) and a keepAliveMsecs reasonable (e.g., 10000), share that agent across invocations where possible; test under load.
Diagnostic step 3
Use Connection: close only for pathological long requests as a temporary mitigation. Prefer bounding keep-alive instead.

Continue troubleshooting

Matched from the title, description and tags