DEV Community

Cover image for βš–οΈ Cloud Load Balancing and Auto Scaling πŸ“ˆ
Madhurima Rawat
Madhurima Rawat

Posted on • Edited on • Originally published at github.com

βš–οΈ Cloud Load Balancing and Auto Scaling πŸ“ˆ

πŸ” Hello Clouders!

May your serverless functions always stay within bounds or prepare for a billing surprise! πŸ’Έβš‘

πŸ“š All code, docs, and resources are available in my GitHub repository:

GitHub logo madhurimarawat / Cloud-Computing

This repository focuses on cloud computing and demonstrates how to set up virtual machines, S3, and other services using LocalStack. It provides a comprehensive guide to simulating AWS services locally for development and testing purposes.

Cloud-Computing

This repository focuses on cloud computing and demonstrates how to set up virtual machines, S3, and other services using LocalStack. It provides a comprehensive guide to simulating AWS services locally for development and testing purposes.

Repo Size GitHub Stars GitHub Forks GitHub Issues Closed Issues Open Pull Requests Closed Pull Requests GitHub Discussions GitHub Contributors Top Language License Last Commit Repository Age Workflow Status GitHub Watchers


Tools and Technologies βš™οΈπŸ’»

1. AWS CLI

AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. It simplifies managing cloud resources by providing commands for a wide range of AWS services, enabling tasks such as provisioning, managing, and automating workflows with ease.

LocalStack is a fully functional, local testing environment for AWS services. It enables developers to simulate AWS services on their local machines, facilitating the development and testing of cloud-based applications without needing access to an actual AWS account.

3. Docker

Docker is a containerization platform that allows developers to build, share, and run applications in isolated environments called…




In the last post,

we explored the world of serverless computing, where AWS Lambda took center stage.

Now, it’s time to dive deeper into the magic that makes cloud apps resilient and responsive:
Cloud Load Balancing and Auto Scaling, your dynamic duo for performance and efficiency.
Let’s balance the load, scale the servers, and cloud like pros. β˜οΈπŸš€

First, I’ll explain the design behind load balancing and auto scaling.
Then, we’ll implement it using Python and LocalStack β€” simulating traffic with a simple Flask app instead of using paid services like NGINX.

I’ve also explained all the steps for setting up NGINX-based load balancing, which I initially tried.
So if you’re using the paid version or actual AWS services, everything will work as expected too. βœ…


What is Load Balancing?

Load balancing is the process of distributing incoming network traffic across multiple servers to ensure no single server bears too much demand. This improves performance, reliability, and fault tolerance by preventing overload and ensuring continuous availability.

Key Benefits of Load Balancing:

  • Improved Performance: Spreads traffic efficiently across multiple servers.
  • High Availability & Redundancy: Ensures uptime even if a server fails.
  • Scalability: Easily accommodates increased traffic by adding more servers.
  • Optimized Resource Utilization: Ensures servers are used efficiently.

Types of Load Balancing:

  1. Round Robin: Distributes requests sequentially across servers.
  2. Least Connections: Directs traffic to the server with the fewest connections.
  3. IP Hash: Uses the client's IP address to route requests consistently to the same server.
  4. Weighted Load Balancing: Assigns different weights to servers based on their capacity.

Load Balancing Illustration

This image visually represents load balancing, showing how traffic is distributed among multiple servers to ensure high availability and reliability.


What is Nginx?

Nginx (pronounced "engine-x") is a high-performance web server and reverse proxy designed for speed, stability, and scalability. It is widely used for load balancing, caching, and serving static content efficiently.

Why Use Nginx?

  • Reverse Proxy & Load Balancing: Distributes requests across multiple backend servers.
  • High Performance: Handles thousands of concurrent connections efficiently.
  • Security Features: Protects against DDoS attacks and unauthorized access.
  • Efficient Static Content Handling: Quickly serves HTML, CSS, JavaScript, and images.
  • Support for Microservices: Works seamlessly with modern containerized applications.

Nginx Overview

This image provides a high-level view of Nginx’s role in handling requests, acting as a reverse proxy, load balancer, and web server for modern applications.


πŸ–ΌοΈ About the Cover Image:
It shows growing user traffic (πŸ‘₯πŸ›’) reaching a load balancer (βš–οΈ), which then distributes tasks efficiently.
Auto scaling (πŸ“ˆπŸ“‰) ensures resources scale up or down as demand changes β€” keeping your cloud setup both effective and economical.

Simple Load balancing Function with LocalStack and Flask

1. Creating a Virtual Private Cloud (VPC):

To begin, create a VPC using the AWS CLI.

aws ec2 create-vpc --cidr-block 10.0.0.0/16 --endpoint-
url=http://localhost:4566
Enter fullscreen mode Exit fullscreen mode

2. Creating a Subnet:

Once the VPC is created, define a subnet within it.

aws ec2 create-subnet --vpc-id vpc-66375b0cbe498b519
--cidr-block 10.0.1.0/24 --endpoint-url=http://localhost:4566
Enter fullscreen mode Exit fullscreen mode

3. Creating an Application Load Balancer:

Create an ALB and associate it with the subnet.

aws elbv2 create-load-balancer --name my-load-balancer
--subnets subnet-3f40f7c6e3a26040f --security-groups default
--type application --endpoint-url=http://localhost:4566
Enter fullscreen mode Exit fullscreen mode

Setting Up Nginx Containers with Load Balancing

4. Running Backend Containers:

Navigate to the project directory before running containers.

cd "C:\Users\rawat\Documents\8 SEMESTER\Cloud
Computing\Lab\Experiment 6\Codes"
Enter fullscreen mode Exit fullscreen mode

Run the backend containers using Docker:

docker run -d --name backend1 nginx
docker run -d --name backend2 nginx
Enter fullscreen mode Exit fullscreen mode

If the image is not found locally, Docker will pull it from the repository.

5. Verify Available Docker Images:

To check the downloaded images:

docker images
Enter fullscreen mode Exit fullscreen mode

Expected output:

REPOSITORY                     TAG        IMAGE ID       CREATED        SIZE
nginx                          latest     b52e0b094bc0   4 weeks ago    192MB
flask-app                      latest     269bf42596ed   3 weeks ago    126MB
...
Enter fullscreen mode Exit fullscreen mode

6. Creating and Running a Load Balancer Container:

docker run -d --name load-balancer -p 8080:80 -v "C:/Users/rawat/Documents
/8 SEMESTER/Cloud
Computing/Lab/Experiment 6/Codes/nginx.conf:/etc/nginx/nginx.conf" nginx
Enter fullscreen mode Exit fullscreen mode

Check logs for potential errors:

docker logs load-balancer
Enter fullscreen mode Exit fullscreen mode

7. Fixing 'Host Not Found' Error:

The error "host not found in upstream 'backend1:80'" means Nginx cannot resolve backend1. All containers must be in the same network.


Fix: Use a Custom Docker Network

Step 1: Create a Network

docker network create my-network
Enter fullscreen mode Exit fullscreen mode

Step 2: Start Backend Containers in the Same Network

Remove existing backend containers:

docker rm -f backend1 backend2
Enter fullscreen mode Exit fullscreen mode

Restart them within the custom network:

docker run -d --name backend1 --network my-network nginx
docker run -d --name backend2 --network my-network nginx
Enter fullscreen mode Exit fullscreen mode

Step 3: Update nginx.conf

Edit your nginx.conf file to ensure proper load balancing:

events { }

http {
    upstream backend_servers {
        server backend1:80;
        server backend2:80;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://backend_servers;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Save the file and restart the load balancer container:

docker rm -f load-balancer
docker run -d --name load-balancer --network my-network -p 8080:80 -v
"C:/Users/rawat/Documents/8 SEMESTER/Cloud
Computing/Lab/Experiment 6/Codes/nginx.conf:/etc/nginx/nginx.conf" nginx
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify Setup

Check if the load balancer is running properly:

docker ps
Enter fullscreen mode Exit fullscreen mode

Access the load balancer at:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

If this is not showing up properly, then go to the next step.


Step 5: Restart the Load Balancer

First, remove the existing container:

docker rm -f load-balancer
Enter fullscreen mode Exit fullscreen mode

Then start it again, within the same network:

docker run -d --name load-balancer --network my-network -p 8080:80 -v
"C:/Users/rawat/Documents/8 SEMESTER/Cloud
Computing/Lab/Experiment 6/Codes/nginx.conf:/etc/nginx/nginx.conf" nginx
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Everything

Check if containers are running:

docker ps
Enter fullscreen mode Exit fullscreen mode

Then try opening:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

LocalStack Load Balancer - Flask Simulation

Since Nginx doesn't support load balancing for LocalStack, a Python Flask application is used for round-robin load balancing simulation.

from flask import Flask, render_template_string
import itertools
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import io
import base64

app = Flask(__name__)

# Backend servers
backends = ["Backend 1", "Backend 2"]

# Simulated processes
processes = [f"P{i+1}" for i in range(10)]
load_values = np.random.randint(3, 15, size=len(processes))

# Round-robin assignment
backend_cycle = itertools.cycle(backends)
schedule = {process: next(backend_cycle) for process in processes}

@app.route("/")
def home():
    """Renders the home page with the load balancer visualization."""
    try:
        sns.set_theme(style="darkgrid")
        fig, ax = plt.subplots(figsize=(9, 5))

        x_labels = list(schedule.keys())
        y_labels = list(schedule.values())

        custom_colors = [
            "#52796F", "#E63946", "#6A0572", "#457B9D", "#9C6644",
            "#8D99AE", "#2A9D8F", "#E76F51", "#6D597A", "#B56576"
        ]

        bars = ax.bar(x_labels, load_values, color=custom_colors[: len(processes)], width=0.6)

        for bar, (backend, load) in zip(bars, zip(y_labels, load_values)):
            ax.text(
                bar.get_x() + bar.get_width() / 2,
                bar.get_height() + 0.4,
                backend,
                ha="center", fontsize=10, fontweight="bold",
                bbox=dict(facecolor="white", alpha=0.6, edgecolor="black", boxstyle="round,pad=0.3")
            )

        ax.set_ylabel("Assigned Load")
        ax.set_title("Round Robin Load Balancer Simulation", fontsize=14, fontweight="bold")
        ax.set_xticklabels(x_labels, rotation=30, ha="right")

        img = io.BytesIO()
        plt.savefig(img, format="png", bbox_inches="tight")
        img.seek(0)
        plt.close(fig)

        image_data = base64.b64encode(img.getvalue()).decode()

        html_template = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Load Balancer</title>
            <style>
                body { font-family: Arial, sans-serif; background-color: #f0f2f5; text-align: center; padding: 20px; }
                .container { background: white; padding: 20px; box-shadow: 0 0 15px rgba(0,0,0,0.1); max-width: 850px; margin: auto; border-radius: 10px; }
                h2 { color: #2c3e50; font-size: 24px; margin-bottom: 10px; }
                p { font-size: 16px; color: #555; margin-bottom: 20px; }
                img { max-width: 100%; border-radius: 10px; margin-top: 10px; }
            </style>
        </head>
        <body>
            <div class="container">
                <h2>Round Robin Load Balancer Simulation</h2>
                <p>
                    Load balancing distributes tasks among multiple backend servers to optimize performance and reliability.
                    This simulation demonstrates a Round Robin approach, where each process is assigned to a backend in a cyclic order.
                </p>
                <img src="data:image/png;base64,{{ image_data }}" alt="Load Balancer Graph">
            </div>
        </body>
        </html>
        """
        return render_template_string(html_template, image_data=image_data)

    except Exception as e:
        return f"Error generating graph: {str(e)}", 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Enter fullscreen mode Exit fullscreen mode

This application displays a bar chart illustrating the distribution of processes across backend servers using the round-robin approach. The graph helps in understanding how the tasks are assigned dynamically.

Run the app using:

python app.py
Enter fullscreen mode Exit fullscreen mode

Once the server is running, open a web browser and navigate to http://localhost:5000 to access the application.


πŸ“„ Want to see how it all worked step by step? Check it out here:

πŸ”— Experiment 6 Output (PDF)

🧠 Curious about how each command runs and responds? See the detailed input-output flow here:

πŸ–₯️ Load Balancing & Auto Scaling Setup Flow (PDF)

πŸŽ‰ And that’s a wrap on Load Balancing and Auto Scaling!

πŸ’‘ I’ve explained both the simulated LocalStack + Flask setup and the full NGINX-based approach, so if you’re using a paid tier, you can follow the complete setup seamlessly.

πŸ’¬ Do tell me about your own experience, whether you used NGINX, LocalStack, or any other tools for load balancing. What worked, what didn’t?

πŸ“š Found any great resources, tutorials, or articles? Drop them in the comments. I’d love to include helpful links in the article for others to explore!

πŸ”₯ Stay tuned for the next article!
We’re diving into Cloud Databases and Data Management, where I’ll talk about storing data the cloud-native way. πŸ—„οΈπŸŒ©οΈ

Top comments (0)