How to Reduce Latency in a Next.js App on Vercel with a MongoDB Database Hosted in Asia?
Answered
Blanc de Hotot posted this in #help-forum
Blanc de HototOP
I'm hosting a Next.js app on Vercel, and it's connected to a MongoDB instance that's hosted in Southeast Asia. I'm noticing some latency in the app, especially in API routes and server components that interact with the database.
From what I understand, Vercel deploys serverless functions by default in the US (unless configured otherwise), so my app might be experiencing latency due to cross-region communication between the Vercel server (USA) and the MongoDB server (Asia).
Am I right in thinking this region mismatch is causing the latency?
Would hosting MongoDB in the US (closer to Vercel’s region) improve performance? Or is there a better solution like changing the region of the Vercel deployment?
From what I understand, Vercel deploys serverless functions by default in the US (unless configured otherwise), so my app might be experiencing latency due to cross-region communication between the Vercel server (USA) and the MongoDB server (Asia).
Am I right in thinking this region mismatch is causing the latency?
Would hosting MongoDB in the US (closer to Vercel’s region) improve performance? Or is there a better solution like changing the region of the Vercel deployment?
Answered by Asian black bear
You need your data source close to your compute, yes. Either move the DB into the same region as your app or the other way round, depending on who you're trying to serve the app to.
3 Replies
Asian black bear
You need your data source close to your compute, yes. Either move the DB into the same region as your app or the other way round, depending on who you're trying to serve the app to.
Answer
Blanc de HototOP
I see, just moved it and Latency dropped from 4s to 300ms 💀
Thanks for answering!