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:Jump to 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}}
...6 Replies
Project ID:
adc1afb3-6652-4f08-9ac3-0dda485170c8
adc1afb3-6652-4f08-9ac3-0dda485170c8
and a9776b4e-bbcb-4a5d-9d9c-2f15387d4118
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}}
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!An added bonus for the internal network is also: no networking charges!
ah yes good mention, thanks!
no egress fees for database <--> service communication when using the private network
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