Skip to main content

Overview

This guide covers deploying Opinix Trade to production. The platform consists of multiple services that can be deployed independently or together.
Ensure all environment variables are properly configured before deploying to production.

Architecture Components

Opiinix Trade consists of the following deployable components:

Client (Next.js)

Frontend application serving the user interface

Server (Express)

Backend API handling orders and authentication

Engine

Order matching engine processing trades

WebSocket

Real-time communication server

Queue Worker

Background job processing with BullMQ

Database

PostgreSQL with TimescaleDB extension

Redis

Cache and message broker

Deployment Strategies

Deploy all services in a single container or server.Pros:
  • Simpler deployment
  • Lower cost for low traffic
  • Easy development-production parity
Cons:
  • Limited scalability
  • Single point of failure
  • Resource conflicts
Best for: Development, staging, low-traffic applications

Pre-deployment Checklist

1

Environment Configuration

Ensure all required environment variables are set:Database:
DATABASE_URL="postgresql://user:pass@host:5432/opinix?schema=public&sslmode=require"
Client:
NEXTAUTH_URL="https://yourdomain.com"
NEXTAUTH_SECRET="[secure-random-secret]"
TWILIO_ACCOUNT_SID="[your-twilio-sid]"
TWILIO_AUTH_TOKEN="[your-twilio-token]"
TWILIO_NUMBER="[your-twilio-number]"
Redis:
REDIS_URI="redis://username:password@host:6379"
2

Build Verification

Test production builds locally:
# Build all packages
npm run build

# Test client build
cd apps/client && npm run build && npm run start

# Test server build
cd apps/server && npm run build && npm run start
3

Database Migrations

Ensure migrations are ready:
cd packages/db
npx prisma migrate deploy
4

Security Review

  • All secrets are stored securely (not in code)
  • Database uses SSL/TLS (sslmode=require)
  • Redis uses authentication and TLS
  • CORS is properly configured
  • Rate limiting is enabled
  • API endpoints have authentication
  • Secrets are rotated regularly
  • Error messages don’t leak sensitive info

Deployment Platforms

1

Install Vercel CLI

npm install -g vercel
2

Configure Project

Create vercel.json in apps/client:
{
  "buildCommand": "cd ../.. && npm run build -- --filter=client",
  "devCommand": "npm run dev",
  "installCommand": "npm install",
  "framework": "nextjs",
  "outputDirectory": ".next"
}
3

Set Environment Variables

Add environment variables in Vercel dashboard or CLI:
vercel env add NEXTAUTH_SECRET
vercel env add DATABASE_URL
vercel env add TWILIO_ACCOUNT_SID
vercel env add TWILIO_AUTH_TOKEN
vercel env add TWILIO_NUMBER
4

Deploy

cd apps/client
vercel --prod
Vercel automatically handles Next.js optimizations, edge functions, and CDN distribution.
Railway provides easy deployment for backend services with integrated database hosting.
1

Install Railway CLI

npm install -g @railway/cli
railway login
2

Create Services

# Initialize project
railway init

# Add PostgreSQL
railway add --plugin postgresql

# Add Redis
railway add --plugin redis
3

Configure Build

Create railway.json in project root:
{
  "build": {
    "builder": "NIXPACKS",
    "buildCommand": "npm install && npm run build"
  },
  "deploy": {
    "startCommand": "npm run start:server",
    "restartPolicyType": "ON_FAILURE",
    "restartPolicyMaxRetries": 10
  }
}
4

Deploy

railway up

Docker / Container Platforms

Deploy using Docker to any container platform (AWS ECS, Google Cloud Run, Azure Container Instances, DigitalOcean Apps).
Create optimized production Dockerfile:
# Build stage
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files
COPY package*.json turbo.json ./
COPY apps/server ./apps/server
COPY packages ./packages

# Install dependencies
RUN npm ci --only=production

# Generate Prisma client
ARG DATABASE_URL
RUN cd packages/db && DATABASE_URL=$DATABASE_URL npx prisma generate

# Build application
RUN npm run build

# Production stage
FROM node:20-alpine AS production

WORKDIR /app

# Copy built application
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/apps/server/dist ./apps/server/dist
COPY --from=builder /app/packages ./packages

# Run as non-root user
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
USER nodejs

EXPOSE 3001

HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
  CMD node -e "require('http').get('http://localhost:3001/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"

CMD ["node", "apps/server/dist/index.js"]

Database Deployment

Use managed database services:Options:
  • Railway: Built-in PostgreSQL
  • Supabase: PostgreSQL with additional features
  • AWS RDS: Enterprise-grade reliability
  • DigitalOcean Managed Databases: Simple and affordable
  • Neon: Serverless PostgreSQL
Benefits:
  • Automatic backups
  • High availability
  • Scaling capabilities
  • SSL/TLS by default
  • Monitoring included

Redis Deployment

Options:
  • Upstash: Serverless Redis with generous free tier
  • Redis Labs: Official Redis cloud
  • AWS ElastiCache: Enterprise Redis
  • Railway: Built-in Redis
Configuration:
REDIS_URI="rediss://username:password@host:6380"

Post-Deployment

1

Run Database Migrations

# Connect to production database
DATABASE_URL="postgresql://user:pass@host:5432/opinix" \
npx prisma migrate deploy
2

Verify Deployment

Test all endpoints:
# Client
curl https://yourdomain.com

# API
curl https://api.yourdomain.com/health

# WebSocket
wscat -c wss://ws.yourdomain.com
3

Monitor Services

Set up monitoring and alerts. See Monitoring Guide.
4

Configure CDN

Use a CDN for static assets:
  • Vercel includes CDN
  • CloudFlare for custom domains
  • AWS CloudFront

Scaling Considerations

Horizontal Scaling

Scale services independently:
  • Multiple server instances behind load balancer
  • Engine workers based on queue depth
  • WebSocket servers with sticky sessions

Database Scaling

  • Read replicas for queries
  • Connection pooling (PgBouncer)
  • Optimize slow queries
  • Partition large tables

Caching Strategy

  • Redis for hot data
  • CDN for static assets
  • API response caching
  • Browser caching headers

Load Balancing

  • NGINX or HAProxy
  • Cloud load balancers (ALB, GCP LB)
  • Health check endpoints
  • Session affinity for WebSockets

Rollback Strategy

Always have a rollback plan before deploying.
1

Tag Releases

git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0
2

Keep Previous Version

Maintain the previous stable version in production for quick rollback.
3

Database Migrations

Make migrations backward-compatible or maintain separate rollback scripts.
4

Rollback Command

# Vercel
vercel rollback [deployment-url]

# Railway
railway rollback

# Docker
docker-compose up -d --no-deps service-name:previous-tag

Next Steps