Overview
This guide covers deploying the Banca Management Backend to production environments, including environment configuration, database setup, and server management.Prerequisites
- Node.js 20.x or higher
- PostgreSQL 15+ database (Supabase recommended)
- Redis instance (optional, for caching)
- Secure environment for secrets management
Environment Configuration
Required Environment Variables
Create a.env file based on .env.example:
Database Connection Strategy
The backend uses three connection modes optimized for different workloads:1. Transaction Pooler (DATABASE_URL)
- Port: 6543
- Mode: PgBouncer transaction pooling
- Use for: API requests, ticket creation, user operations
- Benefits: Fast connection recycling, low latency
2. Direct Connection (DIRECT_URL)
- Port: 5432
- Mode: Session pooler or direct connection
- Use for: Migrations, cron jobs, long-running operations
- Benefits: Stable for periodic operations
3. Read Replica (DATABASE_URL_REPLICA)
- Port: 5432
- Mode: Session pooler (read-only)
- Use for: Analytics, reports, dashboard queries
- Benefits: Offload read traffic from primary database
If
DATABASE_URL_REPLICA is not configured, the system automatically falls back to DATABASE_URL without errors.Build and Deployment
Run Database Migrations
Docker Deployment
Development with Docker Compose
The project includesdocker-compose.yml for local development databases:
docker-compose.yml
Health Checks and Monitoring
Connection Warmup
The backend implements connection warmup before executing jobs:server.ts
Graceful Shutdown
The server handlesSIGTERM and SIGINT signals for graceful shutdown:
- Mark shutting down - Reject new operations
- Wait for active operations - Up to 30 seconds
- Stop automated jobs - Sorteos, settlements, closures
- Close connections - Redis, Prisma, HTTP server
Production Checklist
Environment Variables
- All required variables set (see
.env.example) - Secrets stored securely (not in code)
-
NODE_ENV=production -
DISABLE_AUTH=false
Database
- Migrations deployed successfully
- Database backups configured
- Connection pooling optimized
- Read replica configured (if using analytics)
Security
- CORS configured for allowed origins
- JWT secrets are strong and unique
- Rate limiting enabled
- Helmet security headers active
Monitoring
- Logging configured (Pino in production mode)
- Error tracking enabled (Sentry configured)
- Database connection monitoring
- Job execution monitoring
Process Management
Using PM2 (Recommended)
Install PM2:ecosystem.config.js
Scaling Considerations
Horizontal Scaling
- Multiple backend instances can run behind a load balancer
- Ensure Redis is used for shared caching
- Database connection pool sizing:
instances × connection_limit
Vertical Scaling
- Monitor memory usage (especially for large batch operations)
- Adjust Node.js heap size if needed:
Job Distribution
- Only one instance should run automated jobs (sorteos, settlements)
- Use leader election if running multiple instances
- Consider dedicated job worker instances
Troubleshooting
Connection Issues
Problem:P1001: Database unreachable
Solutions:
- Check database URL and credentials
- Verify network connectivity
- Check Supabase project status
- Review connection pool settings
Migration Failures
Problem:P2034: Transaction aborted due to deadlock
Solutions:
- Run migrations during low-traffic periods
- Use
DIRECT_URLfor migrations (port 5432) - Check for concurrent migration attempts
Job Execution Errors
Problem: Jobs not running Solutions:- Check server logs for job scheduling messages
- Verify timezone calculations (jobs use UTC)
- Check connection warmup status
- Review job configuration in database
Next Steps
Database Migrations
Learn how to manage Prisma migrations safely
Monitoring
Set up logging and error tracking
Automated Jobs
Configure sorteo automation and settlements