Deployment

    Deploy agents to production with confidence

    Deployment Overview

    Introduction to deploying Fantomu agents to production

    5 min

    Deployment Overview

    Learn how to deploy your Fantomu agents to production environments with confidence and reliability.

    Deployment Options

    Cloud Platforms

    • AWS: EC2, Lambda, ECS, EKS
    • Google Cloud: Compute Engine, Cloud Run, GKE
    • Azure: Virtual Machines, Container Instances, AKS
    • Vercel: Serverless functions
    • Netlify: Edge functions

    Self-Hosted

    • Docker: Containerized deployment
    • Kubernetes: Orchestrated deployment
    • On-premises: Your own infrastructure

    Pre-deployment Checklist

    1. Code Review

    • [ ] All tests passing
    • [ ] Code reviewed and approved
    • [ ] Security scan completed
    • [ ] Performance testing done

    2. Configuration

    • [ ] Environment variables set
    • [ ] API keys configured
    • [ ] Database connections tested
    • [ ] External integrations verified

    3. Monitoring

    • [ ] Logging configured
    • [ ] Metrics collection enabled
    • [ ] Alerts set up
    • [ ] Health checks implemented

    Environment Configuration

    Development Environment

    javascript
    // Development configuration
    const config = {
      environment: 'development',
      logLevel: 'debug',
      apiUrl: 'https://api-dev.fantomu.com',
      database: {
        host: 'localhost',
        port: 5432,
        name: 'fantomu_dev'
      }
    };
    

    Staging Environment

    javascript
    // Staging configuration
    const config = {
      environment: 'staging',
      logLevel: 'info',
      apiUrl: 'https://api-staging.fantomu.com',
      database: {
        host: 'staging-db.fantomu.com',
        port: 5432,
        name: 'fantomu_staging'
      }
    };
    

    Production Environment

    javascript
    // Production configuration
    const config = {
      environment: 'production',
      logLevel: 'warn',
      apiUrl: 'https://api.fantomu.com',
      database: {
        host: 'prod-db.fantomu.com',
        port: 5432,
        name: 'fantomu_prod'
      }
    };
    

    Deployment Strategies

    Blue-Green Deployment

    javascript
    // Blue-green deployment
    const deployment = {
      strategy: 'blue-green',
      current: 'blue',
      target: 'green',
      switchover: 'automatic',
      rollback: 'automatic'
    };
    

    Rolling Deployment

    javascript
    // Rolling deployment
    const deployment = {
      strategy: 'rolling',
      batchSize: 2,
      maxUnavailable: 1,
      timeout: 300
    };
    

    Canary Deployment

    javascript
    // Canary deployment
    const deployment = {
      strategy: 'canary',
      trafficSplit: 10, // 10% to new version
      duration: 3600, // 1 hour
      successCriteria: {
        errorRate: 0.01,
        responseTime: 1000
      }
    };
    

    Container Deployment

    Docker Configuration

    dockerfile
    # Dockerfile
    FROM node:18-alpine
    
    WORKDIR /app
    
    COPY package*.json ./
    RUN npm ci --only=production
    
    COPY . .
    
    EXPOSE 3000
    
    CMD ["npm", "start"]
    

    Docker Compose

    yaml
    # docker-compose.yml
    version: '3.8'
    services:
      agent:
        build: .
        ports:
          - "3000:3000"
        environment:
          - NODE_ENV=production
          - DATABASE_URL=postgresql://user:pass@db:5432/fantomu
        depends_on:
          - db
          - redis
      
      db:
        image: postgres:15
        environment:
          - POSTGRES_DB=fantomu
          - POSTGRES_USER=user
          - POSTGRES_PASSWORD=pass
        volumes:
          - postgres_data:/var/lib/postgresql/data
      
      redis:
        image: redis:7-alpine
        ports:
          - "6379:6379"
    
    volumes:
      postgres_data:
    

    Kubernetes Deployment

    Deployment Manifest

    yaml
    # k8s-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: fantomu-agent
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: fantomu-agent
      template:
        metadata:
          labels:
            app: fantomu-agent
        spec:
          containers:
          - name: agent
            image: fantomu/agent:latest
            ports:
            - containerPort: 3000
            env:
            - name: NODE_ENV
              value: "production"
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: fantomu-secrets
                  key: database-url
            resources:
              requests:
                memory: "256Mi"
                cpu: "250m"
              limits:
                memory: "512Mi"
                cpu: "500m"
    

    Service Manifest

    yaml
    # k8s-service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: fantomu-agent-service
    spec:
      selector:
        app: fantomu-agent
      ports:
      - port: 80
        targetPort: 3000
      type: LoadBalancer
    

    Serverless Deployment

    Vercel Configuration

    json
    // vercel.json
    {
      "version": 2,
      "builds": [
        {
          "src": "api/**/*.js",
          "use": "@vercel/node"
        }
      ],
      "routes": [
        {
          "src": "/api/(.*)",
          "dest": "/api/$1"
        }
      ],
      "env": {
        "FANTOMU_API_KEY": "@fantomu-api-key"
      }
    }
    

    AWS Lambda

    javascript
    // lambda.js
    const { FantomuClient } = require('@fantomu/sdk');
    
    exports.handler = async (event, context) => {
      const client = new FantomuClient({
        apiKey: process.env.FANTOMU_API_KEY
      });
      
      try {
        const result = await client.agents.execute({
          agentId: event.agentId,
          task: event.task,
          context: event.context
        });
        
        return {
          statusCode: 200,
          body: JSON.stringify(result)
        };
      } catch (error) {
        return {
          statusCode: 500,
          body: JSON.stringify({ error: error.message })
        };
      }
    };
    

    CI/CD Pipeline

    GitHub Actions

    yaml
    # .github/workflows/deploy.yml
    name: Deploy to Production
    
    on:
      push:
        branches: [main]
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
        
        - name: Setup Node.js
          uses: actions/setup-node@v3
          with:
            node-version: '18'
            cache: 'npm'
        
        - name: Install dependencies
          run: npm ci
        
        - name: Run tests
          run: npm test
        
        - name: Build
          run: npm run build
        
        - name: Deploy to AWS
          uses: aws-actions/configure-aws-credentials@v2
          with:
            aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
            aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
            aws-region: us-east-1
        
        - name: Deploy
          run: aws s3 sync dist/ s3://my-bucket/
    

    Monitoring and Observability

    Health Checks

    javascript
    // Health check endpoint
    app.get('/health', async (req, res) => {
      try {
        // Check database connection
        await db.query('SELECT 1');
        
        // Check external services
        await checkExternalServices();
        
        res.status(200).json({
          status: 'healthy',
          timestamp: new Date().toISOString(),
          services: {
            database: 'up',
            external: 'up'
          }
        });
      } catch (error) {
        res.status(503).json({
          status: 'unhealthy',
          error: error.message
        });
      }
    });
    

    Metrics Collection

    javascript
    // Metrics collection
    const prometheus = require('prom-client');
    
    const httpRequestDuration = new prometheus.Histogram({
      name: 'http_request_duration_seconds',
      help: 'Duration of HTTP requests in seconds',
      labelNames: ['method', 'route', 'status_code']
    });
    
    const agentExecutionCounter = new prometheus.Counter({
      name: 'agent_executions_total',
      help: 'Total number of agent executions',
      labelNames: ['agent_id', 'status']
    });
    

    Security Considerations

    Environment Variables

    bash
    # .env.production
    NODE_ENV=production
    FANTOMU_API_KEY=sk-prod-...
    DATABASE_URL=postgresql://user:pass@host:5432/db
    REDIS_URL=redis://host:6379
    JWT_SECRET=your-jwt-secret
    

    Secrets Management

    javascript
    // Use AWS Secrets Manager
    const AWS = require('aws-sdk');
    const secretsManager = new AWS.SecretsManager();
    
    const getSecret = async (secretName) => {
      const result = await secretsManager.getSecretValue({
        SecretId: secretName
      }).promise();
      
      return JSON.parse(result.SecretString);
    };
    

    Rollback Strategies

    Automatic Rollback

    javascript
    // Automatic rollback configuration
    const rollback = {
      enabled: true,
      triggers: {
        errorRate: 0.05, // 5% error rate
        responseTime: 5000, // 5 second response time
        healthCheckFailures: 3
      },
      rollbackTo: 'previous_version',
      notification: ['admin@company.com']
    };
    

    Manual Rollback

    bash
    # Rollback to previous version
    kubectl rollout undo deployment/fantomu-agent
    
    # Or using Docker
    docker service update --image fantomu/agent:v1.0.0 fantomu-agent
    

    Best Practices

    1. Gradual Rollout

    • Start with small traffic percentage
    • Monitor metrics closely
    • Increase gradually if stable

    2. Monitoring

    • Set up comprehensive monitoring
    • Create meaningful alerts
    • Track key performance indicators

    3. Testing

    • Test in staging environment
    • Use production-like data
    • Simulate failure scenarios

    4. Documentation

    • Document deployment process
    • Keep runbooks updated
    • Train team on procedures

    Next Steps

    • Learn about [Scaling](/docs/deployment/scaling)
    • Explore [Monitoring](/docs/monitoring)
    • Check out [Security](/docs/security)

    Scaling

    Scale your agents to handle increased load

    6 min

    Scaling

    Learn how to scale your Fantomu agents to handle increased load and ensure optimal performance.

    Scaling Strategies

    Horizontal Scaling

    Add more instances to handle increased load.

    javascript
    // Auto-scaling configuration
    const scaling = {
      type: 'horizontal',
      minInstances: 2,
      maxInstances: 10,
      targetCPU: 70,
      targetMemory: 80,
      scaleUpCooldown: 300,
      scaleDownCooldown: 600
    };
    

    Vertical Scaling

    Increase resources for existing instances.

    javascript
    // Vertical scaling configuration
    const scaling = {
      type: 'vertical',
      cpu: {
        min: '500m',
        max: '2000m'
      },
      memory: {
        min: '512Mi',
        max: '4Gi'
      }
    };
    

    Load Balancing

    Application Load Balancer

    yaml
    # ALB configuration
    apiVersion: v1
    kind: Service
    metadata:
      name: fantomu-agent-alb
    spec:
      type: LoadBalancer
      selector:
        app: fantomu-agent
      ports:
      - port: 80
        targetPort: 3000
      sessionAffinity: ClientIP
    

    Round Robin

    javascript
    // Round robin load balancing
    const loadBalancer = {
      algorithm: 'round_robin',
      healthCheck: {
        path: '/health',
        interval: 30,
        timeout: 5,
        healthyThreshold: 2,
        unhealthyThreshold: 3
      }
    };
    

    Least Connections

    javascript
    // Least connections load balancing
    const loadBalancer = {
      algorithm: 'least_connections',
      stickySessions: true,
      sessionTimeout: 3600
    };
    

    Database Scaling

    Read Replicas

    javascript
    // Read replica configuration
    const database = {
      primary: {
        host: 'db-primary.fantomu.com',
        port: 5432,
        role: 'master'
      },
      replicas: [
        {
          host: 'db-replica-1.fantomu.com',
          port: 5432,
          role: 'slave'
        },
        {
          host: 'db-replica-2.fantomu.com',
          port: 5432,
          role: 'slave'
        }
      ]
    };
    

    Connection Pooling

    javascript
    // Connection pool configuration
    const pool = {
      min: 5,
      max: 50,
      acquireTimeoutMillis: 60000,
      idleTimeoutMillis: 300000,
      createTimeoutMillis: 30000,
      destroyTimeoutMillis: 5000
    };
    

    Database Sharding

    javascript
    // Database sharding configuration
    const sharding = {
      strategy: 'hash',
      shards: [
        {
          id: 'shard-1',
          host: 'db-shard-1.fantomu.com',
          range: '0-33'
        },
        {
          id: 'shard-2',
          host: 'db-shard-2.fantomu.com',
          range: '34-66'
        },
        {
          id: 'shard-3',
          host: 'db-shard-3.fantomu.com',
          range: '67-99'
        }
      ]
    };
    

    Caching Strategies

    Redis Caching

    javascript
    // Redis cache configuration
    const cache = {
      type: 'redis',
      host: 'redis-cluster.fantomu.com',
      port: 6379,
      ttl: 3600,
      maxMemory: '2gb',
      evictionPolicy: 'allkeys-lru'
    };
    

    CDN Caching

    javascript
    // CDN cache configuration
    const cdn = {
      provider: 'cloudflare',
      cacheRules: [
        {
          path: '/api/static/*',
          ttl: 86400 // 24 hours
        },
        {
          path: '/api/dynamic/*',
          ttl: 300 // 5 minutes
        }
      ]
    };
    

    Application Caching

    javascript
    // Application cache configuration
    const appCache = {
      type: 'memory',
      maxSize: 1000,
      ttl: 1800, // 30 minutes
      strategy: 'lru'
    };
    

    Performance Optimization

    Code Optimization

    javascript
    // Optimize database queries
    const optimizedQuery = {
      select: ['id', 'name', 'email'], // Only select needed fields
      where: { status: 'active' },
      limit: 100,
      orderBy: 'created_at DESC',
      index: 'status_created_at'
    };
    

    Memory Optimization

    javascript
    // Memory optimization
    const memoryConfig = {
      maxOldSpaceSize: 4096, // 4GB
      gcInterval: 10000, // 10 seconds
      heapDumpOnCrash: true
    };
    

    CPU Optimization

    javascript
    // CPU optimization
    const cpuConfig = {
      cluster: {
        enabled: true,
        workers: require('os').cpus().length
      },
      compression: {
        enabled: true,
        level: 6
      }
    };
    

    Monitoring and Metrics

    Key Metrics

    javascript
    // Key performance metrics
    const metrics = {
      throughput: {
        requestsPerSecond: 1000,
        transactionsPerSecond: 500
      },
      latency: {
        p50: 100, // 100ms
        p95: 500, // 500ms
        p99: 1000 // 1s
      },
      errors: {
        errorRate: 0.01, // 1%
        timeoutRate: 0.005 // 0.5%
      },
      resources: {
        cpuUsage: 0.7, // 70%
        memoryUsage: 0.8, // 80%
        diskUsage: 0.6 // 60%
      }
    };
    

    Alerting

    javascript
    // Alert configuration
    const alerts = {
      highCPU: {
        threshold: 0.8,
        duration: 300, // 5 minutes
        action: 'scale_up'
      },
      highMemory: {
        threshold: 0.9,
        duration: 180, // 3 minutes
        action: 'scale_up'
      },
      highErrorRate: {
        threshold: 0.05,
        duration: 60, // 1 minute
        action: 'alert_team'
      }
    };
    

    Auto-scaling Configuration

    Kubernetes HPA

    yaml
    # Horizontal Pod Autoscaler
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: fantomu-agent-hpa
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: fantomu-agent
      minReplicas: 2
      maxReplicas: 20
      metrics:
      - type: Resource
        resource:
          name: cpu
          target:
            type: Utilization
            averageUtilization: 70
      - type: Resource
        resource:
          name: memory
          target:
            type: Utilization
            averageUtilization: 80
    

    Custom Metrics

    yaml
    # Custom metrics scaling
    apiVersion: autoscaling/v2
    kind: HorizontalPodAutoscaler
    metadata:
      name: fantomu-agent-custom-hpa
    spec:
      scaleTargetRef:
        apiVersion: apps/v1
        kind: Deployment
        name: fantomu-agent
      minReplicas: 2
      maxReplicas: 50
      metrics:
      - type: Pods
        pods:
          metric:
            name: requests_per_second
          target:
            type: AverageValue
            averageValue: "100"
    

    Best Practices

    1. Start Small

    • Begin with conservative scaling settings
    • Monitor and adjust based on actual usage
    • Test scaling behavior thoroughly

    2. Monitor Everything

    • Track all key performance indicators
    • Set up meaningful alerts
    • Regular performance reviews

    3. Plan for Growth

    • Design for horizontal scaling
    • Use stateless architectures
    • Implement proper caching

    4. Test Scaling

    • Load test your application
    • Simulate failure scenarios
    • Validate recovery procedures

    Next Steps

    • Learn about [Monitoring](/docs/monitoring)
    • Explore [Security](/docs/security)
    • Check out [Troubleshooting](/docs/troubleshooting)