R
Railway5mo ago
zeebs

Request Help / Template demonstrating private networking connection to Postgres

I wish to connect my backend server to its postgres and redis via internal private network. While I've read the guide here https://docs.railway.app/guides/private-networking, i require more assistance. I'm surprised to discover that the Deploy Templates like expressjs-postgres and nextjs-prisma use public networking by default when it would make sense to connect internally surely? Why i want to do this for a small perceived performance advantage. I could be wrong on this... networking is not my strong suit. Maybe I'm missing something?
Solution:
having your app connect to the databases via the private network should be as simple as referencing the private url instead. note, both databases need to be in the same project and environment as the app. for postgres if you use the DATABASE_URL variable in code. to connect via the public tcp proxy, set that service variable to ${{Postgres.DATABASE_URL}}...
Jump to solution
6 Replies
Percy
Percy5mo ago
Project ID: adc1afb3-6652-4f08-9ac3-0dda485170c8
zeebs
zeebs5mo ago
adc1afb3-6652-4f08-9ac3-0dda485170c8 and a9776b4e-bbcb-4a5d-9d9c-2f15387d4118
Solution
Brody
Brody5mo ago
having your app connect to the databases via the private network should be as simple as referencing the private url instead. note, both databases need to be in the same project and environment as the app. for postgres if you use the DATABASE_URL variable in code. to connect via the public tcp proxy, set that service variable to ${{Postgres.DATABASE_URL}} to connect via the private network, set that service variable to ${{Postgres.DATABASE_PRIVATE_URL}} (assuming your postgres database service is named Postgres) for redis if you use the REDIS_URL variable in code. to connect via the public tcp proxy, set that service variable to ${{Redis.REDIS_URL}} to connect via the private network, set that service variable to ${{Redis.REDIS_PRIVATE_URL}} (assuming your redis database service is named Redis) yes you would get a faster rtt!
Lelebees
Lelebees5mo ago
An added bonus for the internal network is also: no networking charges!
Brody
Brody5mo ago
ah yes good mention, thanks! no egress fees for database <--> service communication when using the private network
zeebs
zeebs4mo ago
Thanks @Brody & @Lelebees. I finally got it working with the additions of the following tweaks for my instance.... Append /?family=6 to the end of the Redis URL => REDIS_URL=${{Redis.REDIS_PRIVATE_URL}}/?family=6 This ensures that the DNS resolution looks for IPV6 as per the following thread https://github.com/redis/ioredis/issues/1576 ** Add ENABLE_ALPINE_PRIVATE_NETWORKING=true as mentioned in your docs https://docs.railway.app/guides/private-networking