grpc_graphql_gateway 0.7.0

A Rust implementation of gRPC-GraphQL gateway - generates GraphQL execution code from gRPC services
# Deployment Architecture Summary

## πŸ—οΈ Complete Infrastructure

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Internet / Users                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚  LoadBalancer   β”‚ ← External access
                  β”‚  (AWS NLB/GCP)  β”‚ ← Health checks
                  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ← SSL termination
                           β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚                 β”‚                 β”‚
    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”
    β”‚  Pod 1  β”‚      β”‚  Pod 2  β”‚     β”‚  Pod 3  β”‚ ← HPA manages count
    β”‚ Gateway β”‚      β”‚ Gateway β”‚     β”‚ Gateway β”‚ ← VPA adjusts resources
    β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
         β”‚                β”‚                β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                  β”‚                β”‚
            β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
            β”‚   Redis   β”‚    β”‚  Backend  β”‚
            β”‚   Cache   β”‚    β”‚  Services β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ“¦ Docker Images

### 1. Main Gateway Image
```dockerfile
FROM rust:1.75-slim AS builder
# Build greeter and federation binaries
FROM debian:bookworm-slim
# Runtime with minimal dependencies
```

**Features**:
- Multi-stage build (minimal size)
- Non-root user (security)
- Health checks
- Supports both greeter and federation modes

### 2. Federation Image
```dockerfile
FROM rust:1.75-slim AS builder
# Build federation with all subgraphs
FROM debian:bookworm-slim
# Runs user, product, review subgraphs
```

**Ports**:
- 8891: User subgraph
- 8892: Product subgraph
- 8893: Review subgraph
- 50051-50053: gRPC ports
- 9090: Metrics

## ☸️ Kubernetes Resources

### Core Resources
```
Deployment
β”œβ”€β”€ ReplicaSet (managed by HPA)
β”œβ”€β”€ Pods (3-50 replicas)
β”‚   β”œβ”€β”€ Container: gateway
β”‚   β”œβ”€β”€ Liveness probe: /health
β”‚   └── Readiness probe: /health
└── PodDisruptionBudget (min 2 available)
```

### Services
```
Service (ClusterIP)
└── Session affinity: ClientIP

LoadBalancer (optional)
β”œβ”€β”€ External IP
β”œβ”€β”€ Health checks
└── Traffic policy: Local/Cluster
```

### Autoscaling
```
HorizontalPodAutoscaler
β”œβ”€β”€ Min replicas: 3
β”œβ”€β”€ Max replicas: 10
β”œβ”€β”€ Metrics: CPU 70%, Memory 80%
└── Behavior: gradual scale-up/down

VerticalPodAutoscaler (optional)
β”œβ”€β”€ Update mode: Off/Auto
β”œβ”€β”€ Min resources: 100m CPU, 128Mi RAM
β”œβ”€β”€ Max resources: 2000m CPU, 2Gi RAM
└── Recommendations: continuous
```

### Networking
```
Ingress (NGINX)
β”œβ”€β”€ TLS: cert-manager
β”œβ”€β”€ Load balancing: round_robin
β”œβ”€β”€ Rate limiting: 1000 RPS
└── CORS: enabled

NetworkPolicy (optional)
β”œβ”€β”€ Ingress: from ingress-nginx
└── Egress: DNS + backend services
```

## πŸ”„ Scaling Strategies

### Horizontal Scaling (HPA)
| Metric | Threshold | Action |
|--------|-----------|--------|
| CPU > 70% | Scale up | Add pods (max 50) |
| Memory > 80% | Scale up | Add pods |
| CPU < 40% | Scale down | Remove pods (min 3) |

**Behavior**:
- Scale up: Fast (4 pods/30s)
- Scale down: Gradual (2 pods/60s)
- Stabilization: 5min

### Vertical Scaling (VPA)
| Mode | Purpose | Use Case |
|------|---------|----------|
| Off | Recommendations only | Safe with HPA |
| Initial | Set on creation | Initial sizing |
| Auto | Continuous updates | Full automation |

**Controls**:
- CPU: 100m - 2000m
- Memory: 128Mi - 2Gi

### Load Balancing
| Strategy | Method | Benefit |
|----------|--------|---------|
| Round Robin | Ingress annotation | Even distribution |
| Least Connections | Ingress annotation | Optimal utilization |
| IP Hash | Service affinity | Sticky sessions |

## πŸš€ Federation Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚          Apollo Router (Port 4000)       β”‚
β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”‚
β”‚          β”‚ Query Planner    β”‚             β”‚
β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚            β”‚            β”‚
β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
β”‚   User    β”‚ β”‚ Product  β”‚ β”‚  Review  β”‚
β”‚ Subgraph  β”‚ β”‚ Subgraph β”‚ β”‚ Subgraph β”‚
β”‚ (3 pods)  β”‚ β”‚ (3 pods) β”‚ β”‚ (3 pods) β”‚
β”‚ Port 8891 β”‚ β”‚Port 8892 β”‚ β”‚Port 8893 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚             β”‚             β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                   β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
            β”‚   Backend   β”‚
            β”‚   Services  β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

**Each Subgraph**:
- Independent scaling (HPA)
- Separate resource limits
- Entity resolution with DataLoader
- Metrics on port 9090

## πŸ“Š Monitoring Stack

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Prometheus  β”‚ ← Scrapes metrics (port 9090)
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
β”‚   Grafana    β”‚ ← Visualizes metrics
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”œβ”€ Request rate
       β”œβ”€ Error rate
       β”œβ”€ Latency (p50, p95, p99)
       β”œβ”€ Pod count (HPA)
       └─ Resource usage (VPA)
```

## πŸ”’ Security Layers

```
1. Network
   └─ NetworkPolicy: restrict traffic

2. Container
   β”œβ”€ Non-root user (UID 1000)
   β”œβ”€ Read-only filesystem
   └─ Dropped capabilities

3. Pod
   └─ Security context enforced

4. Service
   β”œβ”€ TLS termination
   └─ Source IP restrictions

5. Application
   β”œβ”€ Rate limiting
   β”œβ”€ CORS policies
   └─ Query whitelisting
```

## πŸ“ˆ Resource Planning

### Development
```yaml
replicas: 1
cpu: 250m
memory: 256Mi
HPA: disabled
VPA: Off (recommendations)
```

### Staging
```yaml
replicas: 2
cpu: 500m
memory: 512Mi
HPA: 2-5 replicas
VPA: Initial
```

### Production
```yaml
replicas: 5
cpu: 1000m
memory: 1Gi
HPA: 5-50 replicas
VPA: Off (with HPA)
LoadBalancer: enabled
PDB: min 3 available
```

## 🎯 Deployment Commands

```bash
# Development
docker-compose -f docker-compose.federation.yml up

# Staging
helm install gateway ./helm/grpc-graphql-gateway \
  --namespace staging \
  -f helm/values-staging.yaml

# Production
helm install gateway ./helm/grpc-graphql-gateway \
  --namespace production \
  -f helm/values-autoscaling-complete.yaml
```

## πŸ“ Testing

```bash
# Load test
k6 run --vus 100 --duration 5m loadtest.js

# Watch scaling
watch 'kubectl get pods,hpa,vpa -n production'

# Check load distribution
kubectl get pods -o wide -l app=gateway

# View metrics
curl http://<lb-ip>/metrics
```

## πŸ”— References

- Dockerfiles: `/Dockerfile`, `/Dockerfile.federation`
- Helm Chart: `/helm/grpc-graphql-gateway/`
- Docker Compose: `/docker-compose.federation.yml`
- Docs: `/docs/src/production/`
- Quick Start: `/DEPLOYMENT.md`