# CDN (Content Delivery Network)

A **CDN (Content Delivery Network)** is a network of servers distributed across various geographic locations. These servers store cached copies of content (like images, videos, CSS files, JavaScript files, etc.) from a primary server. When a user requests content, the CDN delivers it from the server closest to the user, reducing latency and improving load times. This is especially useful for websites with global audiences or high traffic.

### **Real time example : Streaming a Cricket Match on Hotstar**

1. **Scenario**: You’re in Mumbai, and India is playing a high-stakes cricket match. Millions of fans across the country are tuning in to watch the match live on Hotstar (now Disney+ Hotstar).
    
2. **Without a CDN**:
    
    * Hotstar’s primary servers might be located in a single data center, say, in Bengaluru.
        
    * When you and millions of other users try to stream the match, all requests go to the same server in Bengaluru.
        
    * This causes high latency, buffering, and even server crashes due to the overwhelming traffic.
        
3. **With a CDN**:
    
    * Hotstar uses a CDN with servers distributed across India, including in Mumbai, Delhi, Chennai, Kolkata, and other cities.
        
    * When you click "Play," the CDN identifies your location (Mumbai) and delivers the live stream from the nearest CDN server in Mumbai.
        
    * This ensures smooth, buffer-free streaming, even during peak traffic when millions of users are watching simultaneously.
        

# **Load Balancing**

**Load balancing** is the process of distributing incoming network traffic (like web requests, API calls, or data streams) across multiple servers or resources. The goal is to ensure no single server is overwhelmed, improving performance, reliability, and availability of applications or websites.\\

### **Real-Time Example: E-Commerce Website During a Sale**

1. **Scenario**: An e-commerce website like Amazon or Flipkart is hosting a massive sale (e.g., Prime Day or Big Billion Days). Millions of users are trying to access the website simultaneously.
    
2. **Without Load Balancing**:
    
    * All requests go to a single server.
        
    * The server gets overwhelmed, leading to slow response times, failed transactions, or even a complete crash.
        
3. **With Load Balancing**:
    
    * A load balancer distributes incoming requests across multiple servers.
        
    * For example:
        
        * User 1’s request goes to Server A.
            
        * User 2’s request goes to Server B.
            
        * User 3’s request goes to Server C.
            
    * This ensures no single server is overloaded, and the website remains fast and responsive even during peak traffic.
        

---

### **Key Benefits of Load Balancing**

1. **Improved Performance**: Distributes traffic evenly, reducing server load and improving response times.
    
2. **High Availability**: If one server fails, the load balancer redirects traffic to other healthy servers, ensuring minimal downtime.
    
3. **Scalability**: Easily add more servers to handle increased traffic without disrupting service.
    
4. **Fault Tolerance**: Detects and avoids sending traffic to unhealthy or overloaded servers.
    
5. **Efficient Resource Utilization**: Ensures all servers are used optimally, reducing waste.
    

---

### **Types of Load Balancing Algorithms**

1. **Round Robin**: Distributes requests equally across all servers in rotation.
    
    * Example: Server A → Server B → Server C → Server A → Server B → Server C.
        
2. **Least Connections**: Sends requests to the server with the fewest active connections.
    
    * Example: If Server A has 10 connections and Server B has 5, the next request goes to Server B.
        
3. **IP Hash**: Uses the client’s IP address to determine which server should handle the request.
    
    * Example: Requests from a specific IP always go to the same server.
        
4. **Weighted Round Robin**: Assigns a weight to each server based on its capacity. Servers with higher weights handle more requests.
