This guide covers setting up the Sparrow backend on Railway with PostgreSQL database and Redis caching.
- Railway Account: Sign up at railway.app
- Railway CLI: Install with
npm install -g @railway/cli - Git Repository: Your code should be in a Git repository
# Login to Railway
railway login
# Create new project
railway init
# Link to existing project (if you have one)
railway link# Add PostgreSQL service
railway service create postgresql
# Add Redis service (optional, for caching)
railway service create redisSet up the following environment variables in Railway dashboard:
# Database
DATABASE_URL=postgresql://postgres:password@localhost:5432/sparrow
# API Keys (set these in Railway dashboard)
OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key
PLAID_CLIENT_ID=your_plaid_client_id
PLAID_CLIENT_SECRET=your_plaid_client_secret
CHASE_API_KEY=your_chase_api_key
# Application Settings
ENVIRONMENT=production
LOG_LEVEL=INFO
CORS_ORIGINS=*
CACHE_TTL_SECONDS=3600# Redis (if using Redis caching)
REDIS_URL=redis://localhost:6379
# Local development fallback
LOCAL_DATABASE_URL=postgresql://localhost/sparrow# Deploy to Railway
railway up
# Or deploy specific service
railway up --service backend# Navigate to backend directory
cd backend/python_engine
# Deploy
railway up# Run database initialization
railway run "python -c \"from core.database import init_database; init_database()\""# Check database health
curl https://your-app.railway.app/db/health
# Initialize database via API
curl -X POST https://your-app.railway.app/db/init# Check application health
curl https://your-app.railway.app/health# Test root endpoint
curl https://your-app.railway.app/
# Test profiles endpoint
curl https://your-app.railway.app/profiles
# Test simulation endpoint
curl -X POST https://your-app.railway.app/simulation/emergency_fund \
-H "Content-Type: application/json" \
-d '{"profile_id": "1", "scenario_type": "emergency_fund"}'Update your frontend to point to the Railway backend:
# frontend/netlify.toml
[build.environment]
BACKEND_URL = "https://your-app.railway.app"# Set in your frontend deployment
NEXT_PUBLIC_API_URL=https://your-app.railway.app# View application logs
railway logs
# View specific service logs
railway logs --service backend# Check Railway dashboard for:
# - CPU usage
# - Memory usage
# - Request count
# - Error rates-
Database Connection Failed
# Check DATABASE_URL environment variable railway variables list # Test database connection railway run "python -c \"from core.database import db_config; print(db_config.test_connection())\""
-
Missing Dependencies
# Check requirements.txt is up to date pip install -r requirements.txt # Verify all imports work python -c "import api.main"
-
Port Issues
# Ensure PORT environment variable is set echo $PORT # Check if port 8000 is available lsof -i :8000
-
Memory Issues
# Check memory usage railway logs --service backend | grep memory # Consider upgrading Railway plan for more memory
-
Database Connection Pooling
- Already configured in
core/database.py - Uses SQLAlchemy connection pooling
- Already configured in
-
Redis Caching
- Configured in
core/cache_manager.py - Falls back to in-memory cache if Redis unavailable
- Configured in
-
Async Operations
- All database operations are async
- FastAPI handles concurrent requests
backend/python_engine/
├── main.py # Entry point for Railway
├── requirements.txt # Python dependencies
├── railway.json # Railway configuration
├── Procfile # Railway process file
├── runtime.txt # Python version
├── core/
│ ├── database.py # Database configuration
│ ├── cache_manager.py # Redis caching
│ └── config.py # Application config
├── api/
│ ├── main.py # FastAPI application
│ └── workflow_endpoints.py
└── workflows/
├── workflow_engine.py
└── workflow_definitions.py
| Variable | Description | Required | Default |
|---|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Yes | - |
REDIS_URL |
Redis connection string | No | - |
OPENAI_API_KEY |
OpenAI API key | No | - |
ANTHROPIC_API_KEY |
Anthropic API key | No | - |
PLAID_CLIENT_ID |
Plaid client ID | No | - |
PLAID_CLIENT_SECRET |
Plaid client secret | No | - |
CHASE_API_KEY |
Chase API key | No | - |
ENVIRONMENT |
Environment name | No | production |
LOG_LEVEL |
Logging level | No | INFO |
CACHE_TTL_SECONDS |
Cache TTL in seconds | No | 3600 |
PORT |
Application port | No | 8000 |
- Environment Variables: Never commit API keys to Git
- CORS: Configure CORS origins properly for production
- Database: Use strong passwords for PostgreSQL
- Redis: Configure Redis with authentication if needed
- HTTPS: Railway provides HTTPS by default
- Horizontal Scaling: Railway supports multiple replicas
- Database Scaling: Upgrade PostgreSQL plan as needed
- Caching: Redis helps with read-heavy workloads
- CDN: Consider using a CDN for static assets
- Railway Documentation: docs.railway.app
- FastAPI Documentation: fastapi.tiangolo.com
- PostgreSQL Documentation: postgresql.org/docs
- Redis Documentation: redis.io/documentation