Overview
Railway is an excellent choice for deploying KAIU Natural Living because it can host:- Your Node.js application (backend + frontend)
- PostgreSQL database with
pgvectorextension - Redis instance for BullMQ queues
- All in one platform with unified billing and management
Prerequisites
Before deploying to Railway, ensure you have:- A Railway account (sign up here)
- Your GitHub repository
- WhatsApp Cloud API credentials from Meta
- Anthropic API key for Claude 3 Haiku
Deployment Steps
Create a New Project
- Log in to Railway Dashboard
- Click “New Project”
- Select “Deploy from GitHub repo”
- Choose your KAIU Natural Living repository
Add PostgreSQL Database
- In your Railway project, click “New”
- Select “Database” → “PostgreSQL”
- Railway will create a managed PostgreSQL instance
- Note the connection string for environment variables
Railway’s PostgreSQL includes the
pgvector extension, which is required for the RAG functionality.Add Redis Instance
- In your Railway project, click “New”
- Select “Database” → “Redis”
- Railway will create a managed Redis instance
- Note the connection details for environment variables
Configure Environment Variables
Set up environment variables for your application:
- Click on your application service
- Go to the “Variables” tab
- Add all required variables (see Environment Setup)
DATABASE_URL(from PostgreSQL service)REDIS_URL(from Redis service)
WHATSAPP_PHONE_NUMBER_IDWHATSAPP_ACCESS_TOKENWHATSAPP_VERIFY_TOKENANTHROPIC_API_KEY- And other required variables
Configure Build Settings
Railway usually auto-detects the configuration, but verify:
- Build Command:
npm run build - Start Command:
npm run api - Install Command:
npm install
Set Up Custom Domain (Optional)
- Go to “Settings” → “Networking”
- Click “Generate Domain” for a Railway subdomain
- Or add your custom domain
- Railway handles SSL certificates automatically
Deploy Application
Railway will automatically deploy when you push to your repository.To trigger a manual deployment:
- Go to “Deployments” tab
- Click “Deploy” on the latest commit
Initialize Database
After the first deployment, initialize your database:
- Install Railway CLI:
- Login and link to your project:
- Run database migrations:
- (Optional) Seed initial data:
Enable pgvector Extension
Enable the required
pgvector extension:- Access your database through Railway CLI:
- Run this SQL command:
- Verify installation:
Service Configuration
Application Service
- Start Command:
npm run api(runsnode server.mjs) - Port: Railway automatically assigns
PORTenvironment variable - Health Check: Configure at
/api/healthendpoint
PostgreSQL Service
- Version: PostgreSQL 14+ (with
pgvector) - Backup: Enable automatic backups in Railway settings
- Connection Pooling: Enabled by default
Redis Service
- Version: Redis 7+
- Persistence: Enabled by default
- Max Memory: Configure based on your plan
Continuous Deployment
Railway automatically deploys when you:- Push to your connected branch (usually
mainormaster) - Create a new release
- Manually trigger from the dashboard
Monitoring and Logs
Railway provides built-in monitoring:- Logs: Real-time application logs in the dashboard
- Metrics: CPU, memory, and network usage
- Database Metrics: Connection count, query performance
- Alerts: Configure webhooks for deployment events
Troubleshooting
Build Failures
- Check build logs in Railway dashboard
- Verify
package.jsonscripts are correct - Ensure Node.js version is v20+
- Check for missing dependencies
Database Connection Issues
- Verify
DATABASE_URLis set correctly - Check PostgreSQL service is running
- Ensure
pgvectorextension is installed - Review connection pooling settings
Redis Connection Issues
- Verify Redis service is running
- Check
REDIS_HOSTandREDIS_PORTvariables - Test connection using Railway CLI
- Review Redis logs for errors
Application Crashes
- Review application logs in Railway dashboard
- Check for unhandled promise rejections
- Verify all environment variables are set
- Monitor memory usage (adjust if needed)
Cost Optimization
- Hobby Plan: $5/month for basic usage
- Resource Optimization: Monitor and right-size your services
- Database: Use connection pooling to reduce overhead
- Redis: Configure appropriate max memory settings
- Scheduled Tasks: Use BullMQ’s scheduled jobs instead of always-running workers
Next Steps
Environment Setup
Complete guide to environment variables
Vercel Deployment
Alternative deployment option