Jiffoo Docs
Deployment

Railway Deployment

Deploy Jiffoo Mall on Railway with zero configuration

Railway Deployment

Prerequisites

  • Railway account (sign up free)
  • Railway CLI (optional)
  • GitHub account for automatic deployments

Architecture Overview

Railway provides a simple deployment platform with:

  • Automatic builds from GitHub repositories
  • Managed PostgreSQL database
  • Managed Redis cache
  • Automatic SSL certificates
  • Environment variables management
  • Zero configuration deployments

Quick Start (Web Dashboard)

1. Create New Project

  1. Go to Railway Dashboard
  2. Click "New Project"
  3. Select "Deploy from GitHub repo"
  4. Authorize Railway to access your repositories
  5. Select your Jiffoo Mall repository

2. Add Database Services

Add PostgreSQL

  1. Click "+ New" in your project
  2. Select "Database""Add PostgreSQL"
  3. Railway automatically provisions a PostgreSQL instance
  4. Connection details are available in the service variables

Add Redis

  1. Click "+ New" in your project
  2. Select "Database""Add Redis"
  3. Railway automatically provisions a Redis instance
  4. Connection details are available in the service variables

3. Deploy Services

Railway will automatically detect your monorepo structure if configured correctly. For Jiffoo Mall:

Deploy API Service

  1. Click "+ New""GitHub Repo"
  2. Select your repository
  3. Configure the service:
    • Name: jiffoo-api
    • Root Directory: apps/api (or leave empty if using workspace)
    • Build Command: cd apps/api && npm install && npm run build
    • Start Command: cd apps/api && npm start
    • Port: 3001

Deploy Shop Service

  1. Click "+ New""GitHub Repo"
  2. Select your repository
  3. Configure the service:
    • Name: jiffoo-shop
    • Root Directory: apps/shop
    • Build Command: cd apps/shop && npm install && npm run build
    • Start Command: cd apps/shop && npm start
    • Port: 3000

Deploy Admin Service

  1. Click "+ New""GitHub Repo"
  2. Select your repository
  3. Configure the service:
    • Name: jiffoo-admin
    • Root Directory: apps/admin
    • Build Command: cd apps/admin && npm install && npm run build
    • Start Command: cd apps/admin && npm start
    • Port: 3003

Quick Start (Railway CLI)

1. Install Railway CLI

# macOS/Linux
brew install railway

# Windows
scoop install railway

# npm
npm install -g @railway/cli

2. Login to Railway

railway login

3. Initialize Project

# Create new project
railway init

# Link to existing project
railway link

4. Add Services

# Add PostgreSQL
railway add --database postgresql

# Add Redis
railway add --database redis

5. Deploy Services

# Deploy API
cd apps/api
railway up

# Deploy Shop
cd ../shop
railway up

# Deploy Admin
cd ../admin
railway up

Configuration

Environment Variables

Railway automatically injects database connection strings. Configure additional variables:

API Service Variables

# Using CLI
railway variables set NODE_ENV=production
railway variables set PORT=3001
railway variables set JWT_SECRET=your-secret-key

# Or set in Railway Dashboard > Service > Variables

Railway automatically provides:

  • DATABASE_URL - PostgreSQL connection string
  • REDIS_URL - Redis connection string

Shop Service Variables

railway variables set NEXT_PUBLIC_API_URL=https://jiffoo-api.up.railway.app
railway variables set PORT=3000

Admin Service Variables

railway variables set NEXT_PUBLIC_API_URL=https://jiffoo-api.up.railway.app
railway variables set PORT=3003

Using railway.json

Create railway.json in each service directory for configuration:

apps/api/railway.json

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS",
    "buildCommand": "npm install && npm run build"
  },
  "deploy": {
    "startCommand": "npm start",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  }
}

apps/shop/railway.json

{
  "$schema": "https://railway.app/railway.schema.json",
  "build": {
    "builder": "NIXPACKS",
    "buildCommand": "npm install && npm run build"
  },
  "deploy": {
    "startCommand": "npm start",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  }
}

Monorepo Configuration

For turborepo monorepos, Railway needs special configuration:

Option 1: Root-Level Deployment

Create railway.toml in the project root:

[build]
builder = "nixpacks"
buildCommand = "npm install && npx turbo run build --filter=api"

[deploy]
startCommand = "cd apps/api && npm start"

Option 2: Service-Specific Configuration

Use Railway's service settings to specify:

  • Root Directory: Leave empty or set to /
  • Build Command: npx turbo run build --filter=api
  • Start Command: cd apps/api && npm start

Database Management

Run Migrations

# Using Railway CLI
railway run npm run migrate

# Or connect to the database directly
railway connect postgres

# Then run migrations manually
npm run migrate

Seed Database

railway run npm run seed

Database Backups

Railway automatically backs up PostgreSQL databases. To create manual backups:

  1. Go to Database Service in Railway Dashboard
  2. Click "Backups" tab
  3. Click "Create Backup"

Restore from Backup

# List backups
railway backups list

# Restore from backup
railway backups restore <backup-id>

Custom Domains

Add Custom Domain

  1. Go to your service in Railway Dashboard
  2. Click "Settings""Domains"
  3. Click "Add Domain"
  4. Enter your custom domain (e.g., shop.yourdomain.com)
  5. Add the provided CNAME record to your DNS provider:
    CNAME shop.yourdomain.com -> <railway-domain>

Railway automatically provisions SSL certificates via Let's Encrypt.

Multiple Domains

You can add multiple domains per service:

shop.yourdomain.com -> Shop Service
admin.yourdomain.com -> Admin Service
api.yourdomain.com -> API Service

Monitoring and Logs

View Logs

# Using CLI
railway logs

# Follow logs in real-time
railway logs --follow

# Filter by service
railway logs --service jiffoo-api

Metrics Dashboard

Railway provides built-in metrics:

  1. Go to your service in Railway Dashboard
  2. Click "Metrics" tab
  3. View CPU, Memory, Network usage

Webhooks

Set up webhooks for deployment notifications:

  1. Go to Project SettingsWebhooks
  2. Add webhook URL
  3. Select events: deployment.success, deployment.failed

Scaling

Vertical Scaling

Railway automatically scales vertically based on resource usage:

  • Memory: Up to 8GB per service
  • CPU: Shared CPU resources

Horizontal Scaling

Railway currently supports single instance per service. For high availability:

  1. Use Railway's Teams Plan for better resources
  2. Implement application-level caching
  3. Use CDN for static assets

CI/CD Integration

Railway automatically deploys on git push when connected to GitHub.

Manual Deployments

# Deploy current branch
railway up

# Deploy specific environment
railway up --environment production

Deployment Triggers

Configure deployment triggers in Railway Dashboard:

  • Auto-deploy: Deploy on every push (default)
  • Manual deploy: Require manual trigger
  • Branch-specific: Only deploy specific branches

GitHub Actions Integration

Create .github/workflows/railway-preview.yml:

name: Railway Preview

on:
  pull_request:
    branches: [main]

jobs:
  preview:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Install Railway CLI
        run: npm install -g @railway/cli

      - name: Deploy to Railway PR environment
        run: railway up --environment pr-${{ github.event.pull_request.number }}
        env:
          RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}

Environment Management

Railway supports multiple environments per project:

Create Environment

# Using CLI
railway environment create staging

# Switch environment
railway environment use staging

Environment Variables per Environment

Each environment can have different variables:

# Production
railway environment use production
railway variables set DATABASE_URL=prod-db-url

# Staging
railway environment use staging
railway variables set DATABASE_URL=staging-db-url

Cost Optimization

Railway Pricing Tiers

  • Hobby Plan: $5/month, 512MB RAM, $0.20/GB egress
  • Pro Plan: $20/month, 8GB RAM, $0.10/GB egress
  • Team Plan: Custom pricing

Optimization Tips

  1. Remove unused services to reduce costs
  2. Use caching to reduce database queries
  3. Optimize images to reduce bandwidth
  4. Use Railway's CDN for static assets
  5. Scale down non-production environments

Troubleshooting

Build Failures

# Check build logs
railway logs --build

# Common issues:
# - Missing dependencies: Add to package.json
# - Build command fails: Check railway.json
# - Out of memory: Upgrade to Pro plan

Service Not Starting

# Check service logs
railway logs --service jiffoo-api

# Verify environment variables
railway variables

# Check health endpoint
curl https://jiffoo-api.up.railway.app/health

Database Connection Issues

# Verify database is running
railway status

# Check DATABASE_URL variable
railway variables get DATABASE_URL

# Test connection
railway connect postgres

Performance Issues

  1. Check metrics in Railway Dashboard
  2. Upgrade to Pro plan for more resources
  3. Optimize database queries using indexes
  4. Enable Redis caching for frequently accessed data
  5. Use CDN for static assets

Migration from Other Platforms

From Heroku

Railway is Heroku-compatible:

  1. Export Heroku config vars:

    heroku config -s > .env
  2. Import to Railway:

    railway variables set $(cat .env)
  3. Update database connection strings to Railway's format

From Vercel

  1. Deploy backend services to Railway
  2. Keep frontend on Vercel or migrate to Railway
  3. Update API URLs in Vercel environment variables

Security Best Practices

  1. Use environment variables for all secrets
  2. Enable private networking between services (Pro plan)
  3. Restrict database access to Railway internal network
  4. Regular updates - Railway auto-updates base images
  5. Use Railway's secret scanning to detect leaked credentials
  6. Enable 2FA on your Railway account

Advanced Features

Private Networking

Railway Pro plan includes private networking:

# Services can communicate via internal DNS
# Instead of: https://jiffoo-api.up.railway.app
# Use: http://jiffoo-api.railway.internal:3001

TCP Proxying

For services that need TCP connections:

  1. Enable TCP proxy in service settings
  2. Railway provides a TCP endpoint
  3. Connect using the provided host:port

Cron Jobs

Deploy background jobs using Railway's CLI:

# Schedule a cron job
railway run --cron "0 0 * * *" npm run cleanup

Additional Resources

On this page