# Load Balancing & Failover Strategies

SecureCart’s **e-commerce platform** must handle fluctuating traffic, prevent downtime, and maintain **seamless customer experiences**. AWS provides **load balancing and failover strategies** to ensure **scalability, fault tolerance, and high availability (HA)**.

✔ **Why does SecureCart use Load Balancing & Failover Strategies?**

* **Distributes traffic efficiently across multiple compute resources.**
* **Prevents a single point of failure by routing requests to healthy instances.**
* **Ensures automatic recovery from failed components.**
* **Improves performance by dynamically scaling resources.**

***

### **🔹 Step 1: Understanding Load Balancing in AWS**

✔ **What is Load Balancing?**

* **Distributes incoming traffic across multiple servers or compute resources.**
* **Ensures applications remain available even if individual instances fail.**
* **Automatically scales based on demand to handle traffic spikes.**

✔ **AWS Load Balancing Types:**

| **Load Balancer Type**              | **Purpose**                                                            | **SecureCart Use Case**                                                                                                 |
| ----------------------------------- | ---------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- |
| **Application Load Balancer (ALB)** | Distributes HTTP/HTTPS traffic and supports advanced routing features. | **Routes API requests based on paths (e.g., `/checkout` to checkout service, `/products` to product catalog service).** |
| **Network Load Balancer (NLB)**     | Handles TCP/UDP traffic with ultra-low latency.                        | **Manages secure payment transactions that require fast, reliable connections.**                                        |
| **Gateway Load Balancer (GWLB)**    | Routes traffic to third-party security appliances.                     | **Filters traffic through a web application firewall (WAF) before reaching SecureCart’s services.**                     |
| **Classic Load Balancer (CLB)**     | Legacy load balancer supporting both HTTP & TCP traffic.               | **Not recommended; SecureCart uses ALB/NLB for modern architectures.**                                                  |

✅ **Best Practices:**\
✔ **Use ALB for web applications that require advanced routing.**\
✔ **Deploy NLB for high-performance TCP-based workloads.**\
✔ **Configure health checks to detect unhealthy instances automatically.**

***

### **🔹 Step 2: Designing SecureCart’s Load Balancing Strategy**

✔ **Why?** – SecureCart **ensures consistent performance** by distributing traffic efficiently.

| **AWS Load Balancing Feature** | **Purpose**                                                 | **SecureCart Implementation**                                                                      |
| ------------------------------ | ----------------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| **Path-Based Routing**         | Routes traffic based on the URL path.                       | **ALB routes `/cart` requests to the cart service and `/orders` to the order processing service.** |
| **Host-Based Routing**         | Directs traffic based on domain name.                       | **`shop.securecart.com` → storefront, `admin.securecart.com` → admin panel.**                      |
| **Sticky Sessions**            | Ensures a user session stays on the same backend instance.  | **Used for maintaining session state in the checkout service.**                                    |
| **Cross-Zone Load Balancing**  | Distributes traffic evenly across Availability Zones (AZs). | **Enabled to balance load across multiple regions.**                                               |

✅ **Best Practices:**\
✔ **Enable cross-zone load balancing to evenly distribute requests.**\
✔ **Use path-based routing to separate API endpoints efficiently.**\
✔ **Leverage sticky sessions for stateful applications (only when necessary).**

***

### **🔹 Step 3: Implementing Failover Strategies for High Availability**

✔ **Why?** – SecureCart **prepares for failures by implementing automatic failover strategies**.

| **Failover Strategy**           | **Purpose**                                          | **SecureCart Use Case**                                                               |
| ------------------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------- |
| **Route 53 Failover Routing**   | Automatically redirects traffic to a healthy region. | **If `us-east-1` fails, users are redirected to `us-west-2`.**                        |
| **Multi-AZ Deployment**         | Runs workloads across multiple AZs for resilience.   | **Database and backend services are distributed across at least 2 AZs.**              |
| **Multi-Region Active-Passive** | Keeps a secondary region as a hot standby.           | **Orders are processed in the main region, but an alternate region is always ready.** |
| **Active-Active Multi-Region**  | Distributes traffic between multiple active regions. | **Users are dynamically routed to the nearest available region.**                     |

✅ **Best Practices:**\
✔ **Use Route 53 failover routing to redirect traffic when a region goes down.**\
✔ **Deploy critical workloads in Multi-AZ for fault tolerance.**\
✔ **Choose Active-Passive or Active-Active architecture based on business needs.**

***

### **🔹 Step 4: Auto Scaling for Load Balancer Resilience**

✔ **Why?** – SecureCart **uses AWS Auto Scaling to dynamically adjust resources**.

| **Auto Scaling Component**   | **Purpose**                                               | **SecureCart Implementation**                                         |
| ---------------------------- | --------------------------------------------------------- | --------------------------------------------------------------------- |
| **EC2 Auto Scaling**         | Automatically adds/removes EC2 instances based on demand. | **Scales up checkout service when there’s a surge in traffic.**       |
| **Application Auto Scaling** | Adjusts ECS, Lambda, and DynamoDB throughput dynamically. | **Ensures database read/write capacity increases during peak hours.** |
| **Target Tracking Scaling**  | Maintains optimal CPU/memory utilization.                 | **Keeps API servers at 60% CPU load for cost-efficiency.**            |
| **Scheduled Scaling**        | Increases resources at expected traffic peaks.            | **Scales EC2 instances every day at 6 PM before rush hours.**         |

✅ **Best Practices:**\
✔ **Use EC2 Auto Scaling with ALB to handle traffic spikes.**\
✔ **Leverage target tracking to maintain performance without overprovisioning.**\
✔ **Enable scheduled scaling for predictable workloads.**

***

### **🔹 Step 5: Monitoring & Security for Load Balancing & Failover**

✔ **Why?** – SecureCart **monitors system health and secures load balancer traffic**.

| **AWS Monitoring Tool** | **Purpose**                                          | **SecureCart Use Case**                                              |
| ----------------------- | ---------------------------------------------------- | -------------------------------------------------------------------- |
| **Amazon CloudWatch**   | Tracks load balancer health and performance.         | **Monitors request latency to detect bottlenecks.**                  |
| **AWS WAF**             | Protects ALB from DDoS and malicious attacks.        | **Blocks SQL injection and bot traffic at the load balancer level.** |
| **AWS Shield**          | Mitigates large-scale DDoS attacks.                  | **Prevents volumetric attacks from taking down SecureCart APIs.**    |
| **AWS X-Ray**           | Traces requests to identify performance bottlenecks. | **Detects slow database queries affecting checkout performance.**    |

✅ **Best Practices:**\
✔ **Monitor load balancer metrics in CloudWatch to detect failures early.**\
✔ **Use AWS WAF to block unauthorized traffic.**\
✔ **Enable AWS Shield Advanced for DDoS protection on critical services.**

***

## **🚀 Summary**

✔ **Use ALB for web applications, NLB for high-performance networking.**\
✔ **Distribute application workloads across multiple AZs for high availability.**\
✔ **Implement Route 53 failover to reroute traffic in case of regional failures.**\
✔ **Use EC2 Auto Scaling and Application Auto Scaling to manage demand.**\
✔ **Monitor, secure, and optimize performance using CloudWatch, WAF, and AWS Shield.**

####

#### **Scenario:**

SecureCart needs to **handle traffic spikes and implement automated failover** to ensure that **e-commerce transactions are never disrupted**.

#### **Key Learning Objectives:**

✅ Use **Application Load Balancer (ALB) vs. Network Load Balancer (NLB)**\
✅ Implement **Route 53 Failover Routing**\
✅ Understand **Amazon RDS Proxy for database connection pooling**

#### **Hands-on Labs:**

1️⃣ **Set Up an ALB for SecureCart’s Frontend Services**\
2️⃣ **Use Route 53 Failover Routing to Redirect Traffic During Outages**\
3️⃣ **Implement Amazon RDS Proxy to Improve Database Availability**

🔹 **Outcome:** SecureCart **eliminates single points of failure** using **load balancing and failover techniques**.
