Repository navigation
Description
Summary
Implement zero-downtime deployments for the Tale platform using blue-green deployment strategy with Docker Compose on a single VPS/VM.
Current Pain Points
- Service restarts cause brief outages (~90 seconds downtime)
- Database migrations block traffic
- Long container startup times before traffic resumes
Proposed Solution
Use blue-green deployment with Caddy upstream pools. Two versions of stateless services run simultaneously during deployment, with traffic switched only after the new version is healthy.
How It Works
PHASE 1: Blue serving → Internet → Caddy → [Blue ✅]
PHASE 2: Green starting → Internet → Caddy → [Blue ✅] + [Green ⏳]
PHASE 3: Both healthy → Internet → Caddy → [Blue draining] + [Green ✅ NEW]
PHASE 4: Cleanup → Internet → Caddy → [Green ✅]
Colors rotate: blue → green → blue → green...
Implementation Tasks
Files to Create
-
scripts/deploy.sh- Deployment orchestration script- Detect current color (blue/green)
- Build and start new color
- Wait for health checks
- Switch traffic via Caddy reload
- Drain and cleanup old containers
-
compose.blue.yml- Blue deployment overlay- Container names with
-bluesuffix - Network aliases:
platform-blue,rag-blue, etc.
- Container names with
-
compose.green.yml- Green deployment overlay- Container names with
-greensuffix - Network aliases:
platform-green,rag-green, etc.
- Container names with
Files to Modify
-
compose.yml- Remove
container_namefrom stateless services (platform, rag, crawler, search, graph-db) - Keep
container_namefor stateful services (db, proxy) - Optimize health checks (5s interval instead of 30s)
- Remove
-
services/proxy/Caddyfile- Add upstream pool with both blue and green backends
- Configure health-based routing (
lb_policy first) - Add passive health checking (circuit breaker)
-
services/platform/docker-entrypoint.sh- Add graceful shutdown with connection draining
- Create shutdown marker file for health check
Database Consistency
Both Blue and Green share the same database. For schema changes, use the expand-contract pattern:
- EXPAND: Add new columns/tables (backward compatible) - run BEFORE deployment
- DEPLOY: Code works with both old and new schema
- CONTRACT: Remove old columns (only AFTER old code is gone)
Safe Migrations (EXPAND)
ADD COLUMNADD TABLECREATE INDEX CONCURRENTLYADD NULLABLE COLUMN
Unsafe Migrations (CONTRACT - run manually after deploy)
DROP COLUMNRENAME COLUMNADD NOT NULL
Resource Requirements
Running two versions simultaneously requires ~2x memory during deployment:
| Phase | Memory Required |
|---|---|
| Normal operation | ~5-6 GB |
| During deployment | ~10-12 GB |
Recommendation: VPS should have at least 12-16 GB RAM.
Deployment Commands
# Normal deployment
./scripts/deploy.sh deploy
# Quick rollback (if previous containers still running)
./scripts/deploy.sh rollback
# Check status
./scripts/deploy.sh statusTesting Plan
- Deploy blue version initially
- Make code change and deploy (should switch to green)
- Verify zero dropped requests during switch
- Make another change and deploy (should switch back to blue)
- Test rollback scenario
- Test failed deployment scenario (new version doesn't pass health check)
Alternative: Simpler Rolling Update
If blue-green is too resource-intensive, use rolling updates:
docker compose up -d --no-deps --build platformCaddy's lb_try_duration handles brief unavailability (~10-30s potential for errors).
Detailed Plan
See the comment below for the full implementation plan with diagrams.
Activity
larryro commentedon Dec 30, 2025
Full Implementation Plan
How Zero-Downtime Works (Conceptual)
The Problem Today
Currently, when you run
docker compose up -d --build:The Solution: Blue-Green Deployment
Blue-green deployment runs TWO versions simultaneously during the switch:
How Caddy Knows Where to Send Traffic
Caddy's reverse proxy has health checking built in:
Key behavior:
/api/healthon both backendsWhy We Need Overlay Files
Docker Compose needs to know which containers belong to "blue" vs "green":
When we deploy:
Both can run simultaneously because they have different names.
Color Rotation Logic Explained
The Core Principle
The script maintains two variables:
Timeline of Multiple Deployments
How the Script Determines What to Stop
Why This Works When Both Are Healthy
The key insight is: the script decides what to stop BEFORE starting the new version.
Database Consistency During Deployments
The Shared Database Model
Both Blue and Green services connect to the same database. This is intentional and necessary:
The Problem: Schema Changes
When your code update requires database changes, you have a problem:
Solution: Expand-Contract Pattern
Migration Timing Diagram
Example: Renaming a Column (Requires 3 Deployments!)
Convex Database Considerations
Detailed Implementation
Step 1: Update compose.yml
Remove
container_namefrom stateless services and optimize health checks:Step 2: Create compose.blue.yml
Step 3: Create compose.green.yml
Same structure as blue, with
-greensuffix.Step 4: Update Caddyfile
{$DOMAIN:http://localhost} { @needsTLS not host *.test *.local *.localhost localhost 127.0.0.1 tls { issuer acme { dir {$ACME_CA:https://acme-v02.api.letsencrypt.org/directory} disable_tlsalpn_challenge } } log { output stdout format console level INFO } # Blue-Green Upstream Pool reverse_proxy { to platform-blue:3000 platform-green:3000 lb_policy first # Active health checking health_uri /api/health health_interval 3s health_timeout 2s health_status 200 # Passive health checking (circuit breaker) fail_duration 10s max_fails 3 unhealthy_status 500 502 503 504 # Retry on failure lb_try_duration 5s lb_try_interval 250ms header_up Host {host} header_up X-Real-IP {remote_host} } encode gzip zstd header { -Server X-Content-Type-Options "nosniff" X-Frame-Options "SAMEORIGIN" Referrer-Policy "strict-origin-when-cross-origin" } @https { protocol https } header @https Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" }Step 5: Create scripts/deploy.sh
Key functions:
get_current_color()- Detect which color is activedeploy_new_version()- Build and start new colorwait_for_health()- Poll health endpointsswitch_traffic()- Reload Caddy configcleanup_old()- Drain and remove old containersStep 6: Update docker-entrypoint.sh
Add graceful shutdown:
Deployment Workflow
Summary: Database Safety Rules
feat(deployment): implement zero-downtime blue-green deployment
feat(deployment): implement zero-downtime blue-green deployment
fix(platform): close round-5 review findings on voice-mode TTS (4/N) …
fix(platform): close round-5 review findings on voice-mode TTS (4/N) …