# Auto Scaling

Master ClawCloud Run's **Auto Scaling** capabilities to automatically adjust application instances for optimal resource allocation.

***

### **How It Works**

Think of auto-scaling as a 24/7 "smart regulator" for your applications. It operates through three key phases:

1. **Continuous Monitoring** Tracks core metrics like **CPU utilization** and **memory usage** across all active instances.
2. **Intelligent Analysis** Calculates required instance count based on preset thresholds (e.g., "maintain CPU usage below 50%").
3. **Automatic Adjustment** Dynamically scales instances up/down via ClawCloud Run's control plane, ensuring:
   * 📈 **Stability** during traffic spikes
   * 💰 **Cost efficiency** during low demand

#### **Metric Calculation Logic**

When multiple instances are running:

* **Averages** determine scaling actions:

  ```
  Example 1:  
  2 instances → CPU at 60% and 40% → 50% average  
  Example 2:  
  3 instances → Memory at 70%, 50%, 30% → 50% average  
  ```

***

### **Real-World Example: E-Commerce Application**

#### Scaling Rules

```
instance_range: 1-5  
target_cpu_utilization: 50%  
```

**Scaling Formula**:

```
Desired Replicas = ceil[Current Replicas × (Current Metric / Target)]  
```

#### Scenario 1: 🚀 Peak Traffic (Flash Sale)

* **Current State**: 3 instances, CPU at 75%/80%/70% → **75% average**
* **Calculation**:

  ```
  3 × (75 / 50) = 4.5 → Rounded up to 5 instances  
  ```
* **Action**: Scales **out to 5 instances**

#### Scenario 2: 🌙 Off-Peak Hours

* **Current State**: 5 instances, CPU at 30% average for 5+ minutes
* **Calculation**:

  ```
  5 × (30 / 50) = 3 → 3 instances  
  ```
* **Action**: Scales **in to 3 instances**

<figure><img src="https://2509840198-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FrViHKCGxzpwUEsRBfwlN%2Fuploads%2FqzoBtmZy1caKNIuKYvb3%2Fimage.png?alt=media&#x26;token=04d93874-b362-4e23-b435-30d61eb135eb" alt=""><figcaption></figcaption></figure>
