Vikas Badoni

TCP load Balancer using socket programming

In this project we will be creating a load balancer and a group of backend servers using python socket programming. We will redirect/ load balance the request coming from client to load balancer to the different backend server choosen based on an algorithm.

For both backend server and load balancer, we first start by initialising the socket. Then bind that socket to a ip address and port pair (This is where the process listens on). We then finally start listening for the connection.

lb_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
lb_add = (lb_host, lb_port)
lb_socket.bind(lb_add)
lb_socket.listen(queue_con)

For each backend server we create separate threads and start listening.

queue_con variable represents the number of connections that the kernel will keep in the internal queue. Any connection more then the queue length would be rejected at the kernel level.

lb_socket.listen(queue_con)

We use a infinite while loop while picking up connection from these respective queues using accept() method.

Once the client_socket is created we use recv() method to get the client’s data. Response can sent by using sendall() method on the same socket.

client_data = client_socket.recv(2048).decode()
client_socket.sendall(response.encode())

On the Load balancer side we create separate threads for each client which are connecting, Pass the socket created to the function hadling client’s request. Then create a backend socket and choose one of the backend server using the rebalance() function. connect to that backend address and use sendall() to send data to this address. We can then finally write the response returned from this connection back to client socket.

threading.Thread(target=server_client, args=(client_socket,)).start()
client_data = client_socket.recv(2048).decode()
backend_add = rebalance()
backend_sock.connect(backend_add)
...

client_socket.sendall(backend_response)  #sending the same reponse to client.

Thank You!

Github Code Link