504: GATEWAY_TIMEOUT Code: FUNCTION_INVOCATION_TIMEOUT

By Jake Marsh · Updated

The error

The response from a Vercel Function that ran longer than its maximum duration, often a cron route. Vercel prints an ID under the code, such as pdx1::7mhrk-1794176271212-85ed09989af7: that is the request, not part of the error.

Quick fix

  1. Raise maxDuration for that route (export const maxDuration = 60 in a Next.js route, or functions settings in vercel.json) up to your plan limit.
  2. Make the job smaller per call: process a batch (for example 100 rows) and return, and let the next scheduled call pick up the rest.
  3. Return 200 quickly and do the slow part in a background task or queue the platform supports.

Why this happens

Every Vercel Function has a maximum duration. Cron routes often do a whole day of work in one call, so they hit it first. Vercel does not retry a failed cron invocation.

Source: Vercel: Managing Cron Jobs (duration, error handling)

Other solutions

  • Schedule the route more often with less work per call (every 5 minutes instead of once a night), which also needs a non-Hobby plan or an outside scheduler.
  • Use a scheduler that retries: RunLater retries a timed-out or 5xx call with exponential backoff and logs each attempt, with a timeout you set from 1 to 300 seconds.

Check a schedule before you deploy it: the cron expression explainer shows the next 10 run times.

Frequently asked questions

What does 504 FUNCTION_INVOCATION_TIMEOUT mean?

The function ran longer than its maximum duration, so Vercel stopped it and returned a 504 Gateway Timeout. Per Vercel, the usual causes are a slow API or database call, a function that never returns a response, an infinite loop, or an unhandled exception.

What is the ID after the error, like pdx1::7mhrk-...?

It identifies the request, the same value Vercel sends in the x-vercel-id header. The prefix is the region that handled it: pdx1 is Portland, cle1 is Cleveland, iad1 (the default) is Washington, D.C. Search your Runtime Logs for it to find that invocation.

How long can a Vercel Function run?

With fluid compute, Vercel lists 300 seconds as the default and the maximum on Hobby, and a 300-second default with an 800-second maximum on Pro and Enterprise. Set maxDuration per route to use more of it.

Does Vercel retry a cron job that timed out?

No. A failed cron invocation is not retried; the next run happens at the next scheduled time.

Have the URL called on schedule, with retries

RunLater calls your URL on a cron schedule or at an exact time, in your timezone, retries failed calls with backoff and logs each response, so a timeout or a 5xx shows up in a log instead of disappearing.

Start a 14-day trialStarter is $15/month for 250 scheduled requests and 25,000 runs. Cancel before day 14 and you pay nothing.

Related errors