RAILWAY_REPLICA_ID empty
How can I read my variable in Railway itself? For example, I have the RAILWAY_REPLICA_ID, but it shows as empty when I use console.log."
{ REPLICA_ID: undefined, SERVICE_ID: undefined }
console.log({
REPLICA_ID: process.env.RAILWAY_REPLICA_ID,
SERVICE_ID: process.env.RAILWAY_SERVICE_ID,
})
Solution:Jump to solution
you dont need to define it, and you shouldn't, you defining it is causing the variable to be empty
26 Replies
Project ID:
92e4a81b-58fc-4487-8c5e-6dee0616fc2f
92e4a81b-58fc-4487-8c5e-6dee0616fc2f
what kind of app
Nodejs server
can you share a minimal reproducible example?
this utilities app can read the variable just fine - https://utilities.up.railway.app/env-vars?prefix=RAILWAY_REPLICA_ID
Can I simply read the environment variables this way?
Or is there another standard for reading them in the application?
the code looks fine, but are you trying to define it as a service variable, because thats not how it works
Hmm, I see. But is there a way to read it or to differentiate the REPLICAS within my application?
🥲
are you trying to define it as a service variable?
yes
thats not how it works, dont do that, and then it will work
To give you context, the same application has 4 replicas, but they are socket applications. I want to read my database with 1000 users and distribute 250 to each replica
Is there a way to differentiate my replicas at the service layer?
the replica id is a unique id given to each replica
Yes, that's why I would like to use this ID as a reference to distribute users among the replicas
now that i have told you why you are having trouble with that, and how to fix it, what is blocking you now?
Sorry, I still don't understand. How should I define it to be able to read the variable from the code?
Solution
you dont need to define it, and you shouldn't, you defining it is causing the variable to be empty
But I am simply trying to read it with process.env, without defining it. It was created by Railway itself, right?
yes
In that case, shouldn't it have a value in console.log?
it will when you remove it from your service variables
Hello @Brody , even though I didn't add the RAILWAY_REPLICA_ID to the service variables, I still receive it as empty in my console.log
please provide a minimal reproducible example
Alright, I created a simple project with NestJS and a health check endpoint. In this endpoint, I included console.log(RAILWAY_REPLICA_ID). I deployed the project on Railway with 4 replicas, but I never added RAILWAY_REPLICA_ID to the service variables. Even so, I am receiving null in console.log.
send the repo please