Best Practices
Railway is a highly versatile platform, offering various ways to use it, though some may be less optimal than others for most use cases. These topics aim to help you maximize both your potential and the platform's capabilities.
Use Private networking when possible
Private networking allows services within a project to communicate internally without the need to expose them publicly, while also providing faster communication and increased throughput.
When configuring environment variables in your service to reference domains or URLs of other services, ensure you use the private versions of these variables, such as RAILWAY_PRIVATE_DOMAIN
or DATABASE_URL
.
Using the private network enables communication between services within the same project without incurring service-to-service egress costs, which is particularly beneficial when interacting with databases or other internal services.
Screenshot showing the use of theRAILWAY_PRIVATE_DOMAIN
variable being used via referencing.
Deploying related Services into the same Project
In Railway, a project serves as a container for organizing infrastructure. It can encompass an application stack, a group of services, or even multiple service groups.
If you're about to head back to the dashboard to deploy another service like a database, there's a quicker way - look for the Create
button at the top right of the Project canvas. This shortcut allows you to add new services directly to your current project.
There are a few key advantages of keeping related services within the same project -
-
Private networking - The private network is scoped to a single environment within a project, having all related services within a single project will allow you to use private networking for faster networking along with no egress fees for service-to-service communication.
-
Project clutter - Deploying a new service or database as an entire project will quickly become overwhelming and clutter your dashboard.
Use Reference variables where applicable
Reference variables allow you to dynamically reference another variable, either from a variable set on the current service or from another service in the same project.
Rather than manually copying, pasting, and hardcoding variables like a public domain or those from another service, you can use reference variables to build them dynamically. Example VITE_BACKEND_HOST=${{Backend.RAILWAY_PUBLIC_DOMAIN}}
This approach is better than hardcoding variables, as it keeps your variable values in sync. Change your public domain? The variable updates. Change your TCP proxy? The variable updates.
Screenshot showing a reference variable used to reference the Backend's domain.Edit this file on GitHub