Order of deployment
udaian
HOBBYOP

4 months ago

My project has several services that it deploys. I want to specify order of deployment for all the services. How can I do it?

Solved$10 Bounty

1 Replies

udaian
HOBBYOP

4 months ago

Project ID: 577c1800-0af8-468b-b52d-87090c2d44d8


jitmisra
HOBBY

4 months ago

You can't specify a strict deployment order for services in a Railway project. By design, Railway deploys all services in parallel to make the process as fast as possible.

The recommended approach is to make each service resilient enough to wait for its dependencies to be ready. Instead of controlling the deployment order, you build retry logic into your applications' startup process.

## How to Make Your Services Wait for a Database

The most common reason for wanting an ordered deployment is to ensure a database is running before a backend application tries to connect to it. You can solve this by using a simple startup script.

This script will act as a wrapper. It will try to connect to your database in a loop, and only when the connection is successful will it start your main application.

  1. Create a startup.sh file in your service's root directory:

    Bash

    #!/bin/sh
    
    # This script waits for the database to be ready before starting the app.
    # Replace DB_HOST and DB_PORT with your actual database env variables.
    
    echo "Waiting for database..."
    
    # We use netcat (nc) to check if the port is open.
    # The '-z' flag makes nc scan for listening daemons without sending data.
    while ! nc -z $DB_HOST $DB_PORT; do
      sleep 1 # wait for 1 second before trying again
    done
    
    echo "Database started!"
    
    # Run the main command to start your application
    # (e.g., the command from your original Procfile or Dockerfile CMD)
    exec "$@"
    

    Note: If your base image doesn't have netcat, you may need to install it. For Postgres, you can use pg_isready for a more robust check.

  2. Make the script executable:chmod +x startup.sh

  3. Update your start command to use this script. For example, if you're using a Procfile:

    Before:web: npm start

    After:web: ./startup.sh npm start

Now, when your service deploys, it will pause and wait for the database service to become available before starting the main Node.js process, effectively solving the dependency issue without needing a strict deployment order.


medim
MODERATOR

4 months ago

As far as I know, the only way you can do that is if a service depends on other services through referenced env vars.


medim
MODERATOR

4 months ago

For example: you deploy service X and service Y, service X has a referenced var that belongs to service Y so service X will wait for the service Y to deploy first


medim
MODERATOR

4 months ago

1404570814489165800


medim
MODERATOR

4 months ago

langfuse-web is queued because it depends on clickhouse (through referenced env vars)


udaian
HOBBYOP

4 months ago

oh cool, @Medim can you share some documentation on how to reference env vars from one service to another service?



medim
MODERATOR

4 months ago

!s


Status changed to Solved medim 4 months ago


Loading...