DEV Community

Faizan Ahmad
Faizan Ahmad

Posted on

Our Vercel functions were 15,000 km from our database

Our slowest request generated a form with AI. It took about 12 seconds, and the AI model accounted for 1.4 of them. A server-rendered public form page took 3.7 seconds at the median. Neither had a slow query in it.

The cause was geography. Vercel runs functions in Washington, D.C. (iad1) by default. Our Supabase database had been created in Sydney (ap-southeast-2). Every query crossed the Pacific, and a single request runs a lot of them one after another: check the session, load the workspace, check the plan and the AI credits, insert the form, insert the first version.

How to check your own setup

Where your functions run. Every Vercel response carries an x-vercel-id header. The second-to-last segment is the function region:

x-vercel-id: bom1::iad1::b6gkl-1794861375804-bfed459e1173
             ^edge  ^function
Enter fullscreen mode Exit fullscreen mode

Where your database is. Supabase's pooler hostname names the region (aws-1-ap-southeast-2.pooler.supabase.com). For other providers, a reverse DNS lookup of the host's IP usually gives it away:

$ nslookup <your-db-host>        # → 52.65.247.42
$ nslookup 52.65.247.42          # → ec2-52-65-247-42.ap-southeast-2.compute.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Everything else in the request path. Check each service a request touches before moving anything, so the move can't make one of them worse:

  • Our Redis (Upstash) resolved to global-latency.upstash.io, which is a global, latency-routed database, so it was fine anywhere.
  • Our AI provider (AWS Bedrock) is in us-east-1, so moving to Sydney would add one long round trip per AI call. One per request, against dozens of database round trips saved.
  • Static pages are served from the CDN, and edge-runtime routes such as OG images don't run in the function region at all.

The change

{
  "regions": ["syd1"]
}
Enter fullscreen mode Exit fullscreen mode

That goes in vercel.json. You can also set the default function region in the project settings.

The numbers

Measured against production, before and after, with the same requests:

Request Before (iad1) After (syd1)
Server-rendered form page, median 3.7 s 0.5 s
AI form generation, end to end ~12 s 4–5 s
Pageview beacon (one insert), median 0.30 s 0.23 s
The AI model call inside generation 1.4 s ~2.3 s

The last row is the trade we expected: the model is now an ocean away instead of the database. Moving the AI calls to a Sydney region too would win back about a second.

A gotcha on the way: a newline in CRON_SECRET

While we were in there, we rotated our cron secret, and the next production build failed instantly:

Error: [INVALID_CRON_SECRET] The `CRON_SECRET` environment variable contains
leading or trailing whitespace, which is not allowed in HTTP header values.
Enter fullscreen mode Exit fullscreen mode

We'd piped the value into vercel env add, and the pipe added a trailing newline. Write the value without one. Use printf %s "$SECRET" | vercel env add CRON_SECRET production on a Unix shell, or redirect from a file written without a newline on Windows. The failed build never went live, so the site stayed on the previous deployment the whole time.

Takeaway

If your app feels slow and no single query is slow, check the map before you check the code. Two headers and a DNS lookup tell you where your functions and your data actually are.


This was DodoForm, an AI form builder. The probe we measured with is a small Node script that signs in as a throwaway account, builds one form, reads the timings and deletes the account.

Top comments (1)

Collapse
 
debashish_ghosal profile image
Debashish Ghosal •

Checking the map before the code is a lesson I learned the slow way, and your two-header trick is a tidy way to do it. The x-vercel-id segment and the pooler hostname are both sitting in plain sight, which is probably why nobody looks. What I liked most is that you moved the AI call from 1.4 seconds to about 2.3 and said so. A region move always trades one round trip for another, and leaving that out would have made the post less useful. The CRON_SECRET newline from the pipe is a good bonus, since vercel env add practically invites it. Did you consider putting the AI calls in their own region so the database and the model both stay close?