Skip to content

Load balancer

Absolutely! Here’s how you can do it:

  1. Create Multiple Dummy Python Servers
  2. Create a Python Load Balancer (Reverse Proxy) to distribute requests

1. Dummy Python Servers

You’ll run several identical Flask servers on different ports (e.g. 5001, 5002, 5003).

from flask import Flask
import sys

app = Flask(__name__)

@app.route('/')
def hello():
    return f"Hello from server on port {sys.argv[1]}!"

if __name__ == '__main__':
    port = int(sys.argv[1])
    app.run(host='0.0.0.0', port=port)

Run three instances:

python dummy_server.py 5001
python dummy_server.py 5002
python dummy_server.py 5003

2. Python Load Balancer (Reverse Proxy)

A simple round-robin HTTP load balancer using Flask to forward requests.

from flask import Flask, request, Response
import requests

app = Flask(__name__)

# Backend servers
backends = [
    '<http://localhost:5001>',
    '<http://localhost:5002>',
    '<http://localhost:5003>',
]
current = 0

@app.route('/', methods=['GET', 'POST', 'PUT', 'DELETE'])
def proxy():
    global current
    backend_url = backends[current]
    current = (current + 1) % len(backends)

    # Forward request to backend
    resp = requests.request(
        method=request.method,
        url=f"{backend_url}{request.path}",
        headers={key: value for key, value in request.headers if key != 'Host'},
        data=request.get_data(),
        cookies=request.cookies,
        allow_redirects=False
    )

    excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
    headers = [(name, value) for (name, value) in resp.headers.items()
               if name.lower() not in excluded_headers]

    response = Response(resp.content, resp.status_code, headers)
    return response

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

How to Run Everything

  1. Start three dummy servers:

    python dummy_server.py 5001
    python dummy_server.py 5002
    python dummy_server.py 5003
    
  2. Start the load balancer:

    python load_balancer.py
    
  3. Send requests to the load balancer on port 8000: You’ll see responses alternate between the three backend servers.

    curl <http://localhost:8000/>
    

Summary

  • Each request to the load balancer (localhost:8000) is forwarded to one of the dummy servers in round-robin fashion.
  • All code uses Flask for simplicity.