콘텐츠로 이동

Environment Setup

🏗️ Production-Grade Environment Architecture

섹션 제목: “🏗️ Production-Grade Environment Architecture”

This project implements Silicon Valley-standard environment isolation with legal compliance and data governance in mind.

Environment is in the configuration, not in variables

Each environment has completely separate Cloudflare resources (R2, D1, KV, Queues) to prevent contamination and ensure legal compliance.


Development:
├── Workers: newsfork-seeds-dev
├── R2: newsfork-datasets-dev, newsfork-metadata-dev
├── D1: newsfork-metadata-dev
├── KV: DOMAIN_KV (dev namespace)
└── Queues: newsfork-research-dev, newsfork-contract-dev, newsfork-liveness-dev
Staging:
├── Workers: newsfork-seeds-staging
├── R2: newsfork-datasets-staging, newsfork-metadata-staging
├── D1: newsfork-metadata-staging
├── KV: DOMAIN_KV (staging namespace)
└── Queues: newsfork-research-staging, newsfork-contract-staging, newsfork-liveness-staging
Production:
├── Workers: newsfork-seeds-prod
├── R2: newsfork-datasets-prod, newsfork-metadata-prod
├── D1: newsfork-metadata-prod
├── KV: DOMAIN_KV (prod namespace)
└── Queues: newsfork-research-prod, newsfork-contract-prod, newsfork-liveness-prod
R2 (Raw Data):
├── research/datasets/country=*/category=*/*.json
├── research/liveness/country=*/*.json
├── research/blocked/country=*/*.json
└── research/dead/country=*/*.json
D1 (Metadata):
├── dataset_metadata
├── task_batches
├── task_items
├── domain_cache
└── liveness_results
GitHub (Audit Trail):
├── metadata/snapshot.json
└── seeds/**/*.json

Purpose: Safe experimentation and feature development

  • Safety: ✅ Completely safe
  • Data: Can be deleted/recreated freely
  • Impact: Zero impact on staging/production
  • Retention: 7 days

Purpose: Pre-production testing and integration validation

  • Safety: ⚠️ Mirror of production
  • Data: Should match production patterns
  • Impact: No production impact
  • Retention: 30 days

Purpose: Legal evidence and compliance records

  • Safety: 🚨 CRITICAL - Legal implications
  • Data: Immutable timestamped files
  • Impact: Legal/compliance consequences
  • Retention: 7 years (legal compliance)

Terminal window
# Development (local)
pnpm run dev
pnpm run dev:local
# Development (remote)
pnpm run dev:remote
# Staging deployment
pnpm run deploy:staging
# Production deployment
pnpm run deploy:production
  • Staging: Automatic on push to main branch (when src/**, package.json, or wrangler.jsonc changes)
  • Production: Manual trigger via workflow_dispatch with environment selection
Terminal window
# Apply migrations to dev
pnpm db:migrate
# Apply migrations to staging
pnpm db:migrate:staging
# Apply migrations to production
pnpm db:migrate:production

VariableDevStagingProduction
CF_ENVdevstagingproduction
DATA_PATH_PREFIXdevstagingprod
ENVIRONMENTdevelopmentstagingproduction
ResourceDevStagingProduction
R2 Datasetsnewsfork-datasets-devnewsfork-datasets-stagingnewsfork-datasets-prod
R2 Metadatanewsfork-metadata-devnewsfork-metadata-stagingnewsfork-metadata-prod
D1 Databasenewsfork-metadata-devnewsfork-metadata-stagingnewsfork-metadata-prod
Research Queuenewsfork-research-devnewsfork-research-stagingnewsfork-research-prod
Contract Queuenewsfork-contract-devnewsfork-contract-stagingnewsfork-contract-prod
Liveness Queuenewsfork-liveness-devnewsfork-liveness-stagingnewsfork-liveness-prod
Seed Queuenewsfork-seed-devnewsfork-seed-stagingnewsfork-seed-prod
Domain Queuenewsfork-domain-devnewsfork-domain-stagingnewsfork-domain-prod
DLQnewsfork-dlq-devnewsfork-dlq-stagingnewsfork-dlq-prod
Seed DLQnewsfork-seed-dlq-devnewsfork-seed-dlq-stagingnewsfork-seed-dlq-prod
Domain DLQnewsfork-domain-dlq-devnewsfork-domain-dlq-stagingnewsfork-domain-dlq-prod

현재 wrangler.jsonc 기준, 모든 Queue는 max_batch_size=1 (파일 단위 처리):

QueueMax Batch SizeMax Batch TimeoutMax RetriesDLQ
Research130s3Yes
Contract130s3Yes
Liveness110s3Yes
Seed130s3Yes
Domain130s3Yes

Queue Processing Flow:

  1. API creates batch → Messages enqueued
  2. Queue consumer processes messages in batches
  3. Failed messages retry up to 3 times
  4. Permanently failed messages → Dead Letter Queue (DLQ)
Terminal window
# Development
wrangler secret put GH_TOKEN
wrangler secret put GH_OWNER
wrangler secret put GH_REPO
# Staging
wrangler secret put GH_TOKEN --env staging
wrangler secret put GH_OWNER --env staging
wrangler secret put GH_REPO --env staging
# Production
wrangler secret put GH_TOKEN --env production
wrangler secret put GH_OWNER --env production
wrangler secret put GH_REPO --env production

  1. Resource Isolation: Completely separate R2, D1, KV, Queues
  2. Manual Deployment: Production requires manual GitHub Actions trigger
  3. Immutability: Production data is write-once, read-many
  4. Audit Trail: All changes synced to GitHub for compliance
  • Production data has 7-year retention
  • All production operations are auditable via GitHub sync
  • Metadata files committed to GitHub for transparency
  • Queue DLQ preserves failed tasks for investigation

Terminal window
# Run local Workers
pnpm run dev:local
# Test API
curl http://localhost:8787/health
curl http://localhost:8787/api/v1/datasets
Terminal window
# Deploy to staging
pnpm run deploy:staging
# Test API
curl https://newsfork-seeds-staging.workers.dev/health
Terminal window
# Full test suite first
pnpm test
# Deploy to production (requires confirmation)
pnpm run deploy:production
# Test API
curl https://newsfork-seeds-prod.workers.dev/health

  1. Stop: Pause all Queue consumers if needed
  2. Assess: Check Cloudflare Dashboard for errors
  3. Document: Review DLQ for failed tasks
  4. Notify: Alert compliance team if data affected
  5. Preserve: Maintain audit trail in GitHub
  1. Identify: Locate affected R2/D1 data
  2. Quarantine: Disable affected environment
  3. Restore: Recover from R2 versioning or backups
  4. Validate: Verify data integrity via metadata snapshot
  5. Update: Strengthen access controls

  • ✅ Experiment freely
  • ✅ Use local mode (pnpm run dev:local)
  • ✅ Delete/recreate resources as needed
  • ✅ Test new features with Queue batches
  • ✅ Mirror production patterns
  • ✅ Run integration tests
  • ✅ Validate Queue processing
  • ✅ Test metadata sync to GitHub
  • ⚠️ Manual deployment confirmation
  • ⚠️ Full test suite must pass
  • ⚠️ Legal compliance validation
  • ⚠️ Monitor Queue DLQ

Production은 법적·컴플라이언스 경계이므로 수동 배포와 검증 후에만 사용하세요.