diff --git a/docs/configuration/distributed.mdx b/docs/configuration/distributed.mdx index 6d952485..8cedd1db 100644 --- a/docs/configuration/distributed.mdx +++ b/docs/configuration/distributed.mdx @@ -151,7 +151,7 @@ graph TD ### Key Architectural Highlights -- **Decoupled Stateless Workers**: The main web and API server runs on lightweight general compute (`SHUMAI_WORKERS=agent`), delegating media transcoding to background workers orchestrated via DBOS task queues in PostgreSQL. +- **Decoupled Stateless Workers**: The main web and API server, database, and storage run on reliable On-Demand compute (`c9g.large`, `SHUMAI_WORKERS=agent`), delegating media transcoding to background workers orchestrated via DBOS task queues in PostgreSQL. - **Dedicated Graviton5 Spot Compute (`c9g.xlarge`)**: Media encoding runs on 4 vCPU / 8 GiB RAM AWS Graviton5 Spot instances (~$0.08/hr), utilizing ARM64 vector optimizations and high-speed DDR5 memory to achieve exceptional encoding performance at minimal cost. - **1:1 Pod-to-Node Scaling**: Worker pods request `3000m` CPU, guaranteeing that each active transcoding job gets an entire dedicated 4 vCPU `c9g.xlarge` instance without CPU throttling. - **End-to-End Autoscaling**: Kubernetes HPA detects encoding load and scales worker pods (1 to 5 replicas), while Kubernetes Cluster Autoscaler provisions and terminates underlying EC2 Spot instances automatically. diff --git a/eks/README.md b/eks/README.md index 6f6e3289..214c9594 100644 --- a/eks/README.md +++ b/eks/README.md @@ -18,7 +18,7 @@ This directory contains a complete, production-ready Kubernetes deployment examp │ │ Direct Browser Uploads │ ▼ (Presigned URLs) ┌───────────────────┼─────────────────────────────────────────────────────────────────┐ -│ General Workload Node (c9g.large Spot, ~$0.03/hr) │ +│ General Workload Node (c9g.large On-Demand, ~$0.087/hr) │ │ │ │ │ ▼ │ │ ┌───────────────────────┐ DBOS Tasks ┌───────────────────────────┐ │ @@ -56,12 +56,16 @@ This directory contains a complete, production-ready Kubernetes deployment examp ## Core Design Principles -### 1. Compute Tiering & Why Graviton5 (`c9g.xlarge`) +### 1. Compute Tiering: On-Demand Core & Graviton Spot Workers -Media transcoding with FFmpeg is one of the most CPU-intensive workloads in modern web applications. To achieve maximum throughput at minimum cost, this deployment separates system services from media encoding: +Media transcoding with FFmpeg is one of the most CPU-intensive workloads in modern web applications. To ensure rock-solid stability for persistent data while minimizing transcoding costs, this deployment strictly separates system services from media encoding: -- **General System Node (`shumai-general-spot`)**: A single 2 vCPU / 4 GiB RAM (`c9g.large` Spot) node (~$0.03/hr) runs the Web UI, API, PostgreSQL, and Garage S3. -- **Dedicated Transcode Pool (`shumai-transcode-spot`)**: High-performance 4 vCPU / 8 GiB RAM (`c9g.xlarge` Spot) instances dedicated exclusively to media encoding. +- **General System Node (`shumai-general`)**: A single 2 vCPU / 4 GiB RAM (`c9g.large` **On-Demand**) node (~$0.087/hr) hosts the Web UI, API server, PostgreSQL database, and Garage S3. + > [!IMPORTANT] + > **Why On-Demand for Core Services?** + > Spot instances are subject to involuntary AWS reclamation (interruption). Running stateful services (PostgreSQL, local S3 storage) and the primary user-facing API on an On-Demand instance guarantees that the database and storage layers are never killed, evicted, or subject to abrupt downtime. + +- **Dedicated Transcode Pool (`shumai-transcode-spot`)**: High-performance 4 vCPU / 8 GiB RAM Graviton Spot instances (`c9g.xlarge` primary, with `c8g.xlarge` fallback pools) dedicated exclusively to media encoding. **Why choose AWS Graviton5 (`c9g.xlarge`)?** AWS Graviton5 is AWS's fifth-generation custom ARM64 processor, built on the 6th-generation Nitro system with sustained 3.3 GHz clock speeds, high-speed DDR5 memory (8800 MT/s), and 5x more L3 cache. As demonstrated in AWS's video encoding benchmarks (see [Optimized Video Encoding with FFmpeg on AWS Graviton Processors](https://aws.amazon.com/cn/blogs/opensource/optimized-video-encoding-with-ffmpeg-on-aws-graviton-processors/)), Graviton processors deliver exceptional performance and frames-per-second per dollar for media workloads. Graviton5 further pushes compute performance by up to 25% per vCPU. Coupled with AWS EC2 Spot instances, `c9g.xlarge` costs only **~$0.08/hour** (a massive discount over On-Demand). @@ -84,6 +88,24 @@ When running Garage in-cluster, Shumai utilizes **Split-Horizon S3 Resolution**: All video and document transcoding jobs are enqueued to `transcode_queue` inside PostgreSQL via **DBOS Transact**. Transcode workers are completely stateless and consume jobs from the queue. Each worker pod uses the Kubernetes Downward API (`POD_NAME`) to obtain a unique, collision-free executor ID. +### 4. What Happens If Transcode Spot Nodes Are Interrupted or Killed? + +Because the transcode workers are completely stateless and coordinated via **DBOS Transact** against PostgreSQL on the On-Demand node: + +1. **State Isolation**: + PostgreSQL, EBS persistent volume claims, the Web UI, and S3 storage live on the On-Demand node and are **never terminated** during Spot interruptions. +2. **AWS 2-Minute Interruption Warning & Graceful Drain**: + When AWS reclaims a Spot instance, it issues a 2-minute notice. Kubernetes drains the node and sends `SIGTERM` to the worker pod. With `terminationGracePeriodSeconds: 300`, the worker finishes any encoding step currently near completion, uploads the result to S3, and exits cleanly. +3. **Automatic Step Recovery via DBOS**: + If an instance is terminated abruptly mid-encode, **zero data or tasks are lost**. The video source file remains safe in S3. DBOS workflow step states are durably tracked in PostgreSQL; uncompleted tasks are detected by surviving or newly launched workers and automatically re-executed from their last durable checkpoint. +4. **Spot Capacity Pool Diversification**: + [`eks/eks-cluster.yaml`](./eks-cluster.yaml) configures `instanceTypes: [c9g.xlarge, c8g.xlarge]`. If AWS temporarily exhausts Graviton5 (`c9g.xlarge`) Spot capacity in an Availability Zone, the Auto Scaling Group immediately falls back to Graviton4 (`c8g.xlarge`) Spot capacity. All three families share the same 4 vCPU / 8 GiB RAM profile and ARM64 architecture. +5. **What If All Spot Nodes Are Killed Simultaneously?** + - Transcode worker pods enter `Pending` state (`Insufficient cpu`). + - All newly submitted transcode jobs queue safely inside PostgreSQL on the On-Demand node. No user uploads or jobs are dropped. + - The **Cluster Autoscaler** detects the `Pending` pods and continuously requests new EC2 Spot instances from AWS. + - As soon as Spot capacity becomes available (typically within 1–2 minutes), new nodes launch, pods schedule to `Running`, and workers resume draining the queue. + --- ## Autoscaling Mechanics @@ -131,8 +153,8 @@ eksctl create cluster -f eks/eks-cluster.yaml This provisions: - An EKS 1.31 control plane in a dedicated VPC across multiple AZs. -- `shumai-general-spot`: 1x `c9g.large` Spot instance (labeled `workload: general`). -- `shumai-transcode-spot`: Autoscaling `c9g.xlarge` Spot instances (min: 1, max: 5, labeled `workload: transcode`). +- `shumai-general`: 1x `c9g.large` On-Demand instance (labeled `workload: general`) for reliable API, database, and storage. +- `shumai-transcode-spot`: Autoscaling `c9g.xlarge` Spot instances with `c8g.xlarge` fallback pools (min: 1, max: 5, labeled `workload: transcode`). - AWS EBS CSI Driver with IAM OIDC roles. ### Step 2: Deploy the Shumai Stack diff --git a/eks/eks-cluster.yaml b/eks/eks-cluster.yaml index d03e0584..57008d0f 100644 --- a/eks/eks-cluster.yaml +++ b/eks/eks-cluster.yaml @@ -16,9 +16,10 @@ addons: managedNodeGroups: # General system workload: Web UI, REST API, DBOS PostgreSQL, and Garage S3 - - name: shumai-general-spot - instanceType: c9g.large # 2 vCPU, 4 GiB RAM (Graviton5 Spot, ~$0.03/hr) - spot: true + # Runs on On-Demand instances to guarantee database & storage durability without spot interruptions + - name: shumai-general + instanceType: c9g.large # 2 vCPU, 4 GiB RAM (Graviton5 On-Demand, ~$0.087/hr) + spot: false minSize: 1 maxSize: 2 desiredCapacity: 1 @@ -33,8 +34,11 @@ managedNodeGroups: ebs: true # Transcoding compute pool: High-CPU media processing + # Uses Spot instances with capacity diversification across Graviton generations - name: shumai-transcode-spot - instanceType: c9g.xlarge # 4 vCPU, 8 GiB RAM (Graviton5 Spot, ~$0.08/hr) + instanceTypes: + - c9g.xlarge # 4 vCPU, 8 GiB RAM (Graviton5 Spot, ~$0.08/hr, primary) + - c8g.xlarge # 4 vCPU, 8 GiB RAM (Graviton4 Spot, ~$0.07/hr, fallback pool) spot: true minSize: 1 maxSize: 5 diff --git a/eks/garage.yaml b/eks/garage.yaml index 398829ee..23f42185 100644 --- a/eks/garage.yaml +++ b/eks/garage.yaml @@ -80,6 +80,7 @@ spec: - key: eks.amazonaws.com/nodegroup operator: In values: + - shumai-general - shumai-general-spot - matchExpressions: - key: workload diff --git a/eks/postgres.yaml b/eks/postgres.yaml index 59016f4e..b535e65d 100644 --- a/eks/postgres.yaml +++ b/eks/postgres.yaml @@ -36,6 +36,7 @@ spec: - key: eks.amazonaws.com/nodegroup operator: In values: + - shumai-general - shumai-general-spot - matchExpressions: - key: workload diff --git a/eks/shumai-server.yaml b/eks/shumai-server.yaml index 57b22300..8548f6fd 100644 --- a/eks/shumai-server.yaml +++ b/eks/shumai-server.yaml @@ -42,6 +42,7 @@ spec: - key: eks.amazonaws.com/nodegroup operator: In values: + - shumai-general - shumai-general-spot - matchExpressions: - key: workload