Slowed by Region
Tags: javascript, react, remix, vercel
Published: 2024-06-03 08:25:46

## TLDR;

I just publish my product [Etalase](https://etalase.store) that built with Remix and Hono. The problem comes from the Remix side which hosted on Vercel. For Hono, I use Deno as the runtime engine and hosted it on Deno Deploy combined with Supabase Database.

My Remix page is rendered ~500ms approximately when I back to previous page. For example, from home to products then back again to home. As my inspection, the problem comes from my Vercel region set to Washington while my Supabase and Deno are set to Singapore.

So, setting region for each Platform you use to same region should resolve this issue 😁.

## Crab, why so slow?

I'm asking my self why my page get slower after adding some API calls into loader function. It approximately ~500ms. It's very slow for zero users (for now 😅). Is that because my API slow? nope, I tested it and it fast!

![IDK what meme is this](https://miro.medium.com/v2/resize:fit:752/1*UXNH96dwNg_hj-txjStXVQ.png)

Does my loader not optimized yet? Yeah, it does, but this platform still zero users 😭. Such a beautiful realize...

## Streaming tears from "I'm OK" promises

"OK FINE! Let's tune this" I said it to my self.

![Yeah shit](https://storage.googleapis.com/blog-images-backup/1*GCYCBoi74-alLtIrskvnwg.png)

First, I use [Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) API to aggregate all promises together. I thought the loader slow because the promises running sequentially. After doing that, the result? it does not change anything 🙂.

"How about to streaming this?" I'm literally said it to my self.

Btw, Remix has [streaming](https://remix.run/docs/en/main/guides/streaming) ability to stream your data into UI. As soon as your first chunk of data received by loader function it stream into the UI.

And after doing this 🥁🥁🥁...

It also does not change anything 🙂.

## Let's ~~drown~~ draw

Enough for trying, let's analyzing.

![Senku hahaha](https://i.imgflip.com/4uao5t.jpg)

First, I define the problem. it's SLOW. My expectation is FAST. I've tried two different method: Promise.all and Streaming and it fails.

Now let's make some scenario.

![yyy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crsm75wt9134wqfvl0wj.png)

And focus to the problem.

![zzz](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yj4yqcyfyh07lvdi36m.png)

I use [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) to connect with my API that returns promise and make it parallel using `Promise.all()`. Since fetch is using HTTP based communication this can have two main problems: slow request times or slow response times.

![http](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zrahzcxbj9rtetpsval.png)

But, based on my scenario above I have no problem for Browser and Postman. So what happen? OK Let's figure out what actually make HTTP based communication get slowed specially on the network side because I know my connection is good and data being transferred is small.

![adf](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5smvyihg643k6pppzwub.png)

Oh, wait. Latency? I though I've got some light of God 😭

You know? The actual response times for your server is `time (ms) = latency + server request or response times`. As I remember the major problem for latency is a distance between client and the server itself.

## Triggered

![adfasdf](https://i.pinimg.com/originals/61/cb/be/61cbbee618d02f7bb8a631e849534d8b.jpg)

Wait 🤨 (100x)...

"What the default region of my Vercel serverless function?", I said.

I immediately open my Vercel Functions settings to check the region. And you know, it's "Washington, D.C., USA ( iad1 )" by default.

![kliuyu](https://www.meme-arsenal.com/memes/ca214af538f757e099c220059f16e191.jpg)

>**📖 Here's the reference for you**
> 1. [Configuring Regions for Vercel Functions](https://vercel.com/docs/functions/configuring-functions/region)
> 2. [Remix on Vercel](https://vercel.com/docs/frameworks/remix)

It took my three hours to debug. Thanks, btw 🙂.

## Streaming tears (again, but really)

After realized that problem I change my serverless function to Singapore and apply it.

![sihauiserh](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z1ig3wxxrpsf3i882xwc.png)

Also, I re-check all services are located to Singapore. And the final test is... "fast". I'm very happy about it and take some break to make a coffee.

To summarize, I give you some checklist if your Remix function slow even thought it isn't hosted on Vercel.

1. Use `Promise.all()`. If it can't help, use...
2. Remix `Streaming`. Or even worst...
3. Check your client-server latency issue.

See ya!