Bird
Raised Fist0
Interview Prepcomputer-networksmediumAmazonGoogleFlipkartSwiggyRazorpayPhonePeCRED

Load Balancing - Algorithms (Round Robin, Least Connections, IP Hash)

Choose your preparation mode2 modes available
Steps
setup

Initialize Load Balancer State

The load balancer initializes with three servers: S1, S2, and S3. It sets the round robin index to 0, connection counts to {S1:2, S2:1, S3:3}, and marks all servers as healthy.

💡 Initialization sets the baseline state for all algorithms, showing server health and current load.
Line:self.servers = servers self.index = 0 self.connections = {server: 0 for server in servers} self.healthy = {server: True for server in servers}
💡 The load balancer tracks server health and load to make informed routing decisions.
📊
Load Balancing - Algorithms (Round Robin, Least Connections, IP Hash) - Watch the Algorithm Execute, Step by Step
Watching the algorithm step-by-step reveals how server health, connection counts, and client IPs influence load balancing decisions, making the abstract code concrete.
Step 1/10
·Active fillAnswer cell
Load balancer internal state initialized with servers, connections, and health.
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Initialization: servers set, connections counts assigned, all healthy
Round Robin index incremented to 1 after selecting S1
Hop: 2
Server S1
Server S2
Server S3
Load Balancer
Client
📦Packet
fromLB
toS1
📄 payloadRequest
🔌 protocolTCP
🚩 flags
srcClient:12345
dstS1:80
dataRequest
Round Robin: selected S1 at index 0
Round Robin index incremented, skipped unhealthy S2
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Round Robin: skipped S2 as unhealthy
Healthy servers identified for least connections selection
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Least Connections: filtered healthy servers
Least Connections chose server with minimum active connections
Hop: 2
Server S1
Server S2
Server S3
Load Balancer
Client
📦Packet
fromLB
toS2
📄 payloadRequest
🔌 protocolTCP
🚩 flags
srcClient:12345
dstS2:80
dataRequest
Least Connections: selected S2 with 1 connection
Computed initial index for IP Hash selection
Hop: 1
Server S1
Server S2
Server S3
Load Balancer
Client
src192.168.1.100:—
dst—:—
IP Hash: computed hash for client IP
IP Hash selected healthy server at computed index
Hop: 2
Server S1
Server S2
Server S3
Load Balancer
Client
📦Packet
fromLB
toS3
📄 payloadRequest
🔌 protocolTCP
🚩 flags
src192.168.1.100:12345
dstS3:80
dataRequest
IP Hash: selected S3 at hashed index
Round Robin index updated for next selection
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Round Robin index incremented to 1
Connection count updated for selected server
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Least Connections: incremented connection count for S2
Final server selections completed for all algorithms
Hop: 0
Server S1
Server S2
Server S3
Load Balancer
Client
Round Robin selected S1
Least Connections selected S2
IP Hash selected S3

Key Takeaways

Round Robin cycles through servers in order, skipping unhealthy ones to ensure even distribution.

This is hard to see from code alone because the index update and health check interplay is subtle.

Least Connections selects the server with the fewest active connections, balancing load dynamically.

Visualizing connection counts clarifies how load balancing adapts to server usage.

IP Hash uses client IP hashing to consistently route requests to the same healthy server, supporting sticky sessions.

The hashing and fallback logic is easier to grasp when seen step-by-step.