Bird
Raised Fist0
Interview Prepcomputer-networksmediumAmazonGoogleFlipkartSwiggyRazorpayPhonePeCRED

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

Choose your preparation mode3 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 fill★Answer 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
src→Client:12345
dst→S1:80
data→Request
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
src→Client:12345
dst→S2:80
data→Request
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
src→192.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
src→192.168.1.100:12345
dst→S3:80
data→Request
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.

Practice

(1/5)
1. Trace the sequence of server state changes when a client sends a PATCH request to update a user's email address. Which of the following best describes what happens step-by-step?
easy
A. The server modifies only the specified email field without affecting other user data.
B. The server replaces the entire user resource with the new email data.
C. The server creates a new user resource with the updated email.
D. The server deletes the old email and then inserts the new email as a separate resource.

Solution

  1. Step 1: Understand PATCH semantics

    PATCH is used for partial updates, modifying only specified fields.
  2. Step 2: Differentiate from PUT

    PUT replaces the entire resource, while PATCH updates parts.
  3. Step 3: Analyze options

    The server modifies only the specified email field without affecting other user data correctly states partial modification; others describe full replacement or resource creation/deletion.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    PATCH modifies only the targeted fields without affecting the rest.
Hint: PATCH = partial update, PUT = full replace
Common Mistakes:
  • Confusing PATCH with PUT and thinking it replaces the whole resource
  • Assuming PATCH creates new resources
  • Believing PATCH deletes and reinserts fields
2. In which scenario is deploying a dual-stack network most appropriate during the transition from IPv4 to IPv6?
easy
A. When devices need to communicate over both IPv4 and IPv6 simultaneously
B. When all devices and routers in the network support only IPv6
C. When the network exclusively uses IPv4 and plans to upgrade later
D. When tunneling IPv6 packets over an IPv4-only infrastructure is required

Solution

  1. Step 1: Understand dual-stack purpose

    Dual-stack allows devices to run IPv4 and IPv6 simultaneously, enabling communication over both protocols.
  2. Step 2: Analyze options

    When all devices and routers in the network support only IPv6 is incorrect because dual-stack requires support for both protocols, not only IPv6. When the network exclusively uses IPv4 and plans to upgrade later is incorrect as dual-stack is not needed if only IPv4 is used. When tunneling IPv6 packets over an IPv4-only infrastructure is required describes tunneling, not dual-stack.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Dual-stack is the method to support both IPv4 and IPv6 concurrently.
Hint: Dual-stack means running IPv4 and IPv6 side-by-side [OK]
Common Mistakes:
  • Confusing dual-stack with tunneling
  • Assuming dual-stack requires IPv6-only devices
3. In a large enterprise network with frequent topology changes and a need for fast convergence, which routing algorithm is most appropriate to use?
easy
A. Distance Vector routing because it requires less memory and simpler computations
B. Link State routing because it uses periodic full routing table exchanges
C. Distance Vector routing because it avoids flooding the network with updates
D. Link State routing because it quickly propagates topology changes and converges faster

Solution

  1. Step 1: Understand the network requirements for fast convergence

    Large networks with frequent topology changes require routing algorithms that quickly adapt to changes to prevent downtime and routing loops.
  2. Step 2: Compare Distance Vector and Link State routing characteristics

    Distance Vector routing uses periodic updates and can suffer from slow convergence and count-to-infinity problems. Link State routing floods link state advertisements immediately upon topology changes and recalculates routes using Dijkstra's algorithm, leading to faster convergence.
  3. Step 3: Analyze the options

    Distance Vector routing because it requires less memory and simpler computations is incorrect because although Distance Vector requires less memory and simpler computations, it converges slower. Link State routing because it uses periodic full routing table exchanges is incorrect because Link State routing does not use periodic full routing table exchanges but floods link state changes immediately. Distance Vector routing because it avoids flooding the network with updates is incorrect because Distance Vector routing does not avoid flooding; it sends periodic updates to neighbors. Link State routing because it quickly propagates topology changes and converges faster is correct because Link State routing quickly propagates topology changes and converges faster.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Link State routing is preferred in large, dynamic networks for fast convergence.
Hint: Fast convergence and frequent changes -> Link State routing
Common Mistakes:
  • Assuming Distance Vector is always simpler and better for large networks
  • Confusing periodic updates with flooding
  • Believing Link State exchanges full routing tables periodically
4. Which of the following is a common misconception about the resource utilization of Server-Sent Events (SSE) compared to WebSockets?
medium
A. SSE uses fewer server resources than WebSockets because it only supports unidirectional communication
B. SSE cannot be used over HTTPS, limiting its security compared to WebSockets
C. SSE maintains a single persistent HTTP connection, reducing latency compared to HTTP Long Polling
D. SSE requires a new HTTP connection for each event, increasing overhead compared to WebSockets

Solution

  1. Step 1: Understand SSE connection model

    SSE maintains a single persistent HTTP connection over which events are streamed.
  2. Step 2: Analyze resource usage

    Because SSE uses one persistent connection, it avoids the overhead of repeatedly opening new connections.
  3. Step 3: Evaluate options

    SSE requires a new HTTP connection for each event, increasing overhead compared to WebSockets is false; SSE uses a single persistent connection. SSE uses fewer server resources than WebSockets because it only supports unidirectional communication is true. SSE maintains a single persistent HTTP connection, reducing latency compared to HTTP Long Polling is true. SSE cannot be used over HTTPS, limiting its security compared to WebSockets is a common misconception and is false; SSE works over HTTPS.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    SSE works over HTTPS; it is not limited in security compared to WebSockets
Hint: SSE = single persistent connection, supports HTTPS
Common Mistakes:
  • Assuming SSE opens a new HTTP request for every event
  • Believing SSE is bidirectional like WebSocket
  • Thinking SSE cannot be secured with HTTPS
5. If a CDN edge server uses a stale-while-revalidate caching strategy, what happens when a user requests content whose TTL has expired but the stale content is still served?
hard
A. User receives stale content immediately while edge server fetches fresh content asynchronously
B. User request is blocked until fresh content is fetched from the origin server
C. Edge server returns an error indicating content is expired
D. User is redirected to the origin server to get fresh content

Solution

  1. Step 1: Define stale-while-revalidate

    This strategy serves stale content immediately after TTL expiry while asynchronously fetching fresh content.
  2. Step 2: User experience

    User gets content without delay, improving perceived performance.
  3. Step 3: Background refresh

    Edge server updates cache with fresh content once fetched.
  4. Step 4: Analyze other options

    User request is blocked until fresh content is fetched from the origin server is incorrect because user is not blocked. Edge server returns an error indicating content is expired is false; no error is returned. User is redirected to the origin server to get fresh content is wrong; user is not redirected to origin.
  5. Final Answer:

    Option A -> Option A
  6. Quick Check:

    Stale-while-revalidate balances freshness and latency by serving stale content immediately.
Hint: Stale-while-revalidate = serve stale now, refresh in background
Common Mistakes:
  • Assuming user waits for fresh content
  • Thinking stale content causes errors
  • Believing user is redirected to origin on expiry
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard