This project implements a simple load balancer using Python and Flask. A load balancer is a critical component in distributed systems that helps distribute incoming network traffic across multiple backend servers to ensure optimal resource utilization, maximize throughput, and minimize response time. In this project, we have created a basic load balancer that randomly selects backend servers from a predefined list and redirects incoming requests to one of these servers.
The project consists of two main components:
-
server.py: This file defines a Flask application that acts as a proxy server and load balancer. It maintains a list of backend servers and periodically updates the response times of these servers. The/statsendpoint provides real-time information about the response times of each backend server. The load balancer randomly selects a backend server from the list and redirects incoming requests to that server. -
visual.py: This script fetches the server statistics from the/statsendpoint of the Flask application and visualizes the response times using matplotlib. It continuously fetches the server stats and updates the visualization every 3 seconds.
To use the load balancer project:
- Run the
server.pyscript to start the Flask application. - Run the
visual.pyscript to fetch and visualize the server statistics.
To address the Cross-Origin Resource Sharing (CORS) policy issue, we have enabled CORS for all routes in the Flask application. This allows requests from different origins, such as frontend applications running on http://127.0.0.1:5500, to access the /stats endpoint without encountering CORS errors.
Some potential improvements for this project include:
- Implementing more advanced load balancing algorithms (e.g., round-robin, least connections).
- Adding health checks to monitor the status of backend servers.
- Enhancing the visualization of server statistics for better insights.
This project provides a basic implementation of a load balancer using Python and Flask. By understanding the principles of load balancing and exploring this project, developers can gain valuable insights into building scalable and efficient distributed systems.
Feel free to contribute, provide feedback, or explore further enhancements to this project!