R
Railway•8mo ago
drdrigg

Postgres is very slow

Hello, I have created a project with a very simple express js app that connects to a postgres server (located in the same project in Railway). I connect to it and GET four simple posts (only ones in the database) and it takes a wopping 3 seconds to complete! I therefore try to create the exact same database on Digitalocean with the same data and with the same express js app (hosted on Railway) and the same small query takes 200 ms (still very slow but much much better). Why is Postgres so slow at Railway? This is unsuable unfortunately. Project ID: 312b74d3-7061-448f-a297-539dd2bfbb7e
14 Replies
Percy
Percy•8mo ago
Project ID: 312b74d3-7061-448f-a297-539dd2bfbb7e
Brody
Brody•8mo ago
are both postgres and your app in the same region?
drdrigg
drdrigg•8mo ago
No, Express is in EU and Postgres in the US so that could absolutely be something. Moving the DB now!
Brody
Brody•8mo ago
and connect to the database through the private network
drdrigg
drdrigg•8mo ago
mm ok, will try that after Redeploying now Moving the DB to correct region made it go from 3 seconds to 96 ms, so my bad. I am sorry about that. Will moving to private network shave off more ms?
Brody
Brody•8mo ago
definitely could, here is a ping test to the same database through the private network and through the public network
| POSTGRES_PRIVATE_URL | postgres.railway.internal | postgres | Success | N/A | 26.018442ms |
| POSTGRES_PUBLIC_URL | monorail.proxy.rlwy.net | postgres | Success | N/A | 56.743634ms |
| POSTGRES_PRIVATE_URL | postgres.railway.internal | postgres | Success | N/A | 26.018442ms |
| POSTGRES_PUBLIC_URL | monorail.proxy.rlwy.net | postgres | Success | N/A | 56.743634ms |
drdrigg
drdrigg•8mo ago
So I just change the database URL?
Brody
Brody•8mo ago
yeah just change the reference
DATABASE_URL=${{Postgres.DATABASE_PRIVATE_URL}}
DATABASE_URL=${{Postgres.DATABASE_PRIVATE_URL}}
drdrigg
drdrigg•8mo ago
On it, changed. Redeploying! That made it worse 🙂 I went from 96ms to 356ms Changed back and is now 96ms again
Brody
Brody•8mo ago
what are you measuring exactly? connection time + query time? just query time? ??
drdrigg
drdrigg•8mo ago
Time from when I call the endpoint to when it responds (in Postman)
Brody
Brody•8mo ago
well using the internal network would be on average faster as I've shown, so i suspect there is something not quite right with your setup/code
drdrigg
drdrigg•8mo ago
Well not sure about that since its just a change of url to the database. Same code (that works). Do I need to change anything else in the code to make it work with internal communication or is it just a variable change?
Brody
Brody•8mo ago
it should just be a variable change, but that makes things worse for you and that's why I've come to the current conclusion of this being a code issue, unfortunately I have no idea how your code is setup or how the queries are ran, you could be making a connection to the database on every api request, no clue