Custom LoadBalancer Implementation using .Net Core 3.1 / C#
TO Run
- Open Servers Solution, and run. It should run as project, and have 3 servers running in 3 different ports.
- Open LoadBalancerService Solution, and run. It has the loadbalancer service running in port 4000.
- Import the Postman collection, and it has 5 different client calls, name Get Client 1 .. 5 - LB
Highlights
- Load Balancer routes the traffic to differnt servers based on number of active connections in each server, and picks the server with least connections.
- Active connection for client to a server is 30 seconds.
- During the active session, client is sticky session to the same server.
- New Servers can be added by Service Discovery, adding to appSettings.json. In real app, can be done by Consistent Hashing
To Do-
- Use MinHeap to keep track of least active connections. C# does not have an implementation for PriorityQueue. We need to build a custom Minheap. As this will improve performance to get least active server in O(1), and updating the server is O(log n). Currently it is O(n log n) where n is number of active servers.
- Add Health checks on each server, so if server is down, Load balancer does not pick it.
- Active connections in each server and which client is connected to which server information is stored in memory. Need to be stored in caching server. So we can have Load balancer service running as a cluster as well.
- Current implementation is not Thread safe. Real App with data from cache like Redis, can be made to be Thread safe using Locks and Concurrent collections.