File size: 5,115 Bytes
8f9c4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# CodeSync β€” Deployment Guide

## Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Vercel        β”‚     β”‚  Railway/Render  β”‚     β”‚   Upstash    β”‚
β”‚  (Frontend)      │────>β”‚  (Backend)       │────>β”‚   Redis      β”‚
β”‚  Next.js SSR     β”‚     β”‚  Express+Socket  β”‚     β”‚   (Pub/Sub)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                         β”‚  Supabase/Neon   β”‚
                         β”‚  (PostgreSQL)    β”‚
                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Step 1: Database Setup (Supabase)

1. Create a Supabase project at https://supabase.com
2. Copy the connection string from Settings β†’ Database
3. Run migrations:
   ```bash
   cd apps/server
   npx prisma migrate deploy
   ```

## Step 2: Redis Setup (Upstash)

1. Create a Redis database at https://console.upstash.com
2. Select the region closest to your backend server
3. Copy the REST URL and token
4. Enable "Eviction" for memory management

## Step 3: Backend Deployment (Railway)

### Using Railway CLI:
```bash
# Install Railway CLI
npm i -g @railway/cli

# Login and create project
railway login
railway init

# Set environment variables
railway variables set NODE_ENV=production
railway variables set PORT=4000
railway variables set DATABASE_URL="postgresql://..."
railway variables set UPSTASH_REDIS_URL="https://..."
railway variables set UPSTASH_REDIS_TOKEN="..."
railway variables set JWT_ACCESS_SECRET="$(openssl rand -base64 64)"
railway variables set JWT_REFRESH_SECRET="$(openssl rand -base64 64)"
railway variables set CLIENT_URL="https://your-app.vercel.app"
railway variables set OPENROUTER_API_KEY="..."

# Deploy
railway up
```

### Using Render:
1. Connect GitHub repository
2. Select `apps/server` as the root directory
3. Build command: `npm run build`
4. Start command: `npm start`
5. Add environment variables in the dashboard

## Step 4: Frontend Deployment (Vercel)

```bash
# Install Vercel CLI
npm i -g vercel

# Deploy
cd apps/web
vercel

# Set environment variable
vercel env add NEXT_PUBLIC_API_URL
# Enter your Railway/Render backend URL
```

### Vercel Configuration:
- Framework: Next.js
- Root Directory: `apps/web`
- Build Command: `next build`
- Output Directory: `.next`

## Step 5: Docker Sandbox Setup

For code execution to work, the backend server needs Docker access:

### Option A: Railway with Docker (recommended)
Railway supports Docker-in-Docker. Mount the Docker socket.

### Option B: Separate execution service
Deploy a dedicated execution microservice on a VPS with Docker:
```bash
# On a VPS (DigitalOcean, Hetzner, etc.)
docker compose -f docker/docker-compose.execution.yml up -d
```

### Option C: Use a managed service
Use Judge0 API or Piston for code execution without managing Docker.

## Step 6: Google OAuth Setup

1. Go to https://console.cloud.google.com
2. Create OAuth 2.0 credentials
3. Add authorized redirect: `https://your-backend.railway.app/api/auth/google/callback`
4. Add authorized origin: `https://your-app.vercel.app`

## Step 7: Build Sandbox Images

```bash
cd docker/sandboxes
docker build -t codesync-sandbox-js ./javascript
docker build -t codesync-sandbox-python ./python
docker build -t codesync-sandbox-cpp ./cpp
docker build -t codesync-sandbox-java ./java
```

## Environment Variables Checklist

| Variable | Required | Service |
|----------|----------|---------|
| DATABASE_URL | βœ… | Supabase |
| UPSTASH_REDIS_URL | βœ… | Upstash |
| UPSTASH_REDIS_TOKEN | βœ… | Upstash |
| JWT_ACCESS_SECRET | βœ… | Generate |
| JWT_REFRESH_SECRET | βœ… | Generate |
| CLIENT_URL | βœ… | Vercel URL |
| OPENROUTER_API_KEY | Optional | OpenRouter |
| GOOGLE_CLIENT_ID | Optional | Google Cloud |
| GOOGLE_CLIENT_SECRET | Optional | Google Cloud |

## Performance Optimization

1. **WebSocket Sticky Sessions**: Not needed β€” Redis pub/sub handles cross-instance communication
2. **Connection Pooling**: Use PgBouncer or Supabase's built-in pooler
3. **CDN**: Vercel Edge Network handles static assets automatically
4. **Rate Limiting**: Redis-backed distributed rate limiting works across instances

## Scaling Strategy

- **Horizontal Backend Scaling**: Add more Railway/Render instances. Redis pub/sub ensures all instances share state.
- **Database Scaling**: Use read replicas for queries, primary for writes
- **WebSocket Scaling**: Each instance handles up to ~10K concurrent connections
- **Code Execution**: Queue-based with separate worker pool

## Monitoring

- Use Railway/Render built-in metrics
- Set up Upstash Redis monitoring dashboard
- Add application logging with structured JSON output
- Consider adding Sentry for error tracking