# Cost Optimization #1: On-Demand vs Spot Instances Pricing in AWS with an example instance

---

Managing compute costs in the cloud can be tricky — and that’s where understanding **On-Demand vs Spot Instances** becomes crucial, especially if you want to **save money without risking reliability**.

---

### 🧾 What Are On-Demand Instances?

On-Demand instances are **regular EC2 virtual machines** that you can launch anytime. You:

* **Pay per hour or per second**, depending on instance type.
    
* **Get reliable access** — your instance won't be interrupted.
    
* Great for **critical apps**, **databases**, or **unknown workloads** where flexibility is key.
    

💵 **Example (Frankfurt – eu-central-1):**  
`m7i-flex.xlarge` On-Demand = **$0.22942/hour**

---

### 🌀 What Are Spot Instances?

Spot Instances let you use **unused EC2 capacity** at a **much lower cost** — **up to 90% cheaper** than On-Demand.

But there’s a **catch**: AWS **can take it back (terminate it)** at any time if they need that capacity back.

💡 Spot is best for:

* Batch jobs
    
* Data processing
    
* CI/CD runners
    
* Containers that can restart
    
* Fault-tolerant apps
    

💵 **Example (Frankfurt – eu-central-1):**  
`m7i-flex.xlarge` Spot = **$0.085/hour**

---

### 📈 How Does Spot Pricing Work?

* **Dynamic Pricing:** The Spot price changes based on **demand and supply** of spare capacity.
    
* **Cap Limit:** The price will **never exceed the On-Demand rate**.
    
* **Fluctuations:** Prices can go up if there’s high demand or AWS has less unused capacity.
    
* **Discounts Vary:** Some instances offer 30–70% off, while others go up to 90%.
    

---

### ⚠️ What Happens When AWS Needs the Spot Back?

If AWS reclaims the instance:

* You get a **2-minute warning**.
    
* The instance is **terminated or stopped**, depending on your settings.
    

🛡️ That’s why it’s good to:

* Use **Auto Scaling or Karpenter** to re-launch it elsewhere.
    
* Handle **interruptions gracefully** (e.g., save data to S3 or a queue).
    

---

### 📊 How to Calculate the Cost Difference?

Let’s compare On-Demand vs Spot pricing:

* **On-Demand**: $0.22942/hour
    
* **Spot**: $0.085/hour
    

🧮 **Savings**:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1746702869170/acd6ab65-3259-4483-8209-026e9588a316.jpeg align="center")

So, using Spot saves you **about 63%** in this case.

---

### 🧠 Tips for Using Spot Wisely

1. **Use Spot for stateless apps**: Things that can restart without data loss.
    
2. **Fallback to On-Demand**: Use Auto Scaling groups with mixed instance types.
    
3. **Store app data in EBS/S3**: So when the instance is gone, your data isn’t.
    
4. **Use Spot interruption notices**: Automate graceful shutdowns or draining.
    

---

### ✅ Summary Table

| Feature | On-Demand | Spot |
| --- | --- | --- |
| Reliability | High | Medium |
| Cost | High | Low (up to 90% off) |
| Use Cases | Critical apps, databases | Batch jobs, dev/test, CI, data processing |
| Can be Interrupted? | No | Yes (2-min notice) |
| Max Price? | Fixed | Dynamic (≤ On-Demand) |

---

### 🔚 Final Thoughts

Spot Instances are an excellent way to reduce costs if your app can handle interruptions. Think of them as “happy hours” for compute — cheap when available, but not always guaranteed. With tools like **Karpenter**, **Auto Scaling**, and **interrupt handlers**, Spot can save you thousands of dollars per month — if used right.

---
