How to identify replica number?
vimtor
PROOP

a year ago

https://docs.railway.app/reference/variables#railway-provided-variables

Railway already provides a system replica ID environment variable, but I would like to have a replica sequential number so I can listen to ports 3000, 3001, 3002, etc. in each replica.

How are other solving this issue?

Solved

12 Replies

brody
EMPLOYEE

a year ago

Hello,

Sorry but we don't provide a way to identify replicas sequencely, that is something you would need to orchestrate yourself in your applications code.

But I have to ask, why would you want each replica to listen on a different port?


Status changed to Awaiting User Response Railway over 1 year ago


vimtor
PROOP

a year ago

We want to scale our game servers:

https://docs.colyseus.io/scalability/

In our game, users connect directly to the room via websocket. We are using this framework and need a separate domain for each replica so each one can handle a game room. In their example, they use pm2, but that would create a mess inside Railway with mixed logs. Therefore, we would prefer to use different replicas, each having its own domain and game room.


Status changed to Awaiting Railway Response Railway over 1 year ago


vimtor
PROOP

a year ago

Our idea was to have each replica listen to different ports (3000, 3001, 3002, etc) and associate a different domain to each one (eu-server-1, eu-server-2). When a game room needs to be created we check the region and the server number. Then the client users that information to compose the domain and connect to the room.

I'm guessing that we would need to implement some sort of mechanism using Redis to distribute the port numbers across the replicas.


brody
EMPLOYEE

a year ago

It sounds like this is a use case for sticky sessions?


Status changed to Awaiting User Response Railway over 1 year ago


vimtor
PROOP

a year ago

Not quite. I don't have much experience with sticky sessions, but here the setup is different. Users connect to an already created room by ID; there is no matchmaking. One single node process can only handle 1-2 of our game rooms. We just need a way to have multiple processes, each publicly identifiable (via URL, port, or whatever method). I guess we could create a new Railway service for each new server we want to deploy, but using replicas seemed like a simpler option to manage.


Status changed to Awaiting Railway Response Railway over 1 year ago


brody
EMPLOYEE

a year ago

I don't personally see why you couldn't use replicas for this, without having them listen on different ports.


Status changed to Awaiting User Response Railway over 1 year ago


vimtor
PROOP

a year ago

If each replica has a different room, how can the client connect to a specific one without providing different domains or ports?

For example:

  • Replica 1 has room A

  • Replica 2 has room B

Both are listening to port 3000 and we have a single domain as a load balancer server.game.com

If the player wants to connect to room A, I don't see how to do it with this setup. Your only option is to create some sort of proxy that redirects the traffic to the specific replica.

My intended solution would be:

  • Replica 1 has room A and listens to port 3000

  • Replica 2 has room B and listens to port 3001

Each replica is assigned a custom domain which points to each port (e.g. server-1.game.com, server-2.game.com)

That way the client knows that if he wants to connect to room A he has to use the server-1.game.com domain.

I hope I explained myself better this time sweat_smile emoji

Thanks for sharing your thoughts!


Status changed to Awaiting Railway Response Railway over 1 year ago


brody
EMPLOYEE

a year ago

I'm sorry, I was under the impression that you had some kind of gateway in place to facilitate what users connect to what backend, and that it would be relatively trivial for this gateway to route users to a specific replica.


Status changed to Awaiting User Response Railway over 1 year ago


vimtor
PROOP

a year ago

In case we had that gateway, how would it route to a specific replica?


Status changed to Awaiting Railway Response Railway over 1 year ago


brody
EMPLOYEE

a year ago

The gateway would do a DNS lookup and pick a replica to route traffic to.

Here is an example DNS lookup for a service with 5 replicas -

https://utilities.up.railway.app/dns-lookup?value=hello-world.railway.internal&type=ip


Status changed to Awaiting User Response Railway over 1 year ago


vimtor
PROOP

a year ago

Oh, that's really cool. Thanks for sharing. I think I have all the information now to make a decision.


Status changed to Awaiting Railway Response Railway over 1 year ago


brody
EMPLOYEE

a year ago

Awsome, if you have any more questions about how DNS works on the private network feel free to ask!


Status changed to Awaiting User Response Railway over 1 year ago


Railway
BOT

6 months ago

This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!

Status changed to Solved Railway 6 months ago


Loading...