Hone logo
Hone
Problems

Simple Round Robin Load Balancer in Python

Load balancing is a crucial technique for distributing network traffic across multiple servers to prevent overload on any single server, improve responsiveness, and increase the availability of applications. This challenge asks you to implement a basic round-robin load balancer in Python, which sequentially distributes requests to a list of servers. This is a foundational concept in distributed systems and cloud computing.

Problem Description

You are tasked with creating a Python class called RoundRobinLoadBalancer that distributes incoming requests across a list of servers in a round-robin fashion. The class should maintain a list of servers and a counter to track the current server being used. Each time a request is made, the load balancer should return the next server in the list, incrementing the counter and wrapping around to the beginning of the list when it reaches the end.

Key Requirements:

  • Initialization: The class should be initialized with a list of server URLs (strings).
  • get_next_server() method: This method should return the next server URL in the round-robin sequence. It should also update the internal counter to point to the next server.
  • Error Handling: If the server list is empty during initialization or when get_next_server() is called, raise a ValueError with an appropriate message.

Expected Behavior:

The get_next_server() method should consistently return servers in a cyclical order. The first call should return the first server, the second call the second server, and so on, wrapping around to the first server after the last server in the list is reached.

Edge Cases to Consider:

  • Empty server list during initialization.
  • Calling get_next_server() when the server list is empty after some requests have been made (though this shouldn't happen in normal operation).
  • Server URLs are strings.

Examples

Example 1:

Input: servers = ["server1.com", "server2.com", "server3.com"]
balancer = RoundRobinLoadBalancer(servers)
print(balancer.get_next_server()) # Output: server1.com
print(balancer.get_next_server()) # Output: server2.com
print(balancer.get_next_server()) # Output: server3.com
print(balancer.get_next_server()) # Output: server1.com
print(balancer.get_next_server()) # Output: server2.com

Explanation: The servers are returned in a round-robin order, cycling through the list.

Example 2:

Input: servers = ["serverA.net", "serverB.net"]
balancer = RoundRobinLoadBalancer(servers)
print(balancer.get_next_server()) # Output: serverA.net
print(balancer.get_next_server()) # Output: serverB.net
print(balancer.get_next_server()) # Output: serverA.net

Explanation: A smaller list demonstrates the cyclical behavior.

Example 3: (Edge Case)

Input: servers = []
balancer = RoundRobinLoadBalancer(servers) # Raises ValueError: "Server list cannot be empty."

Explanation: An empty server list during initialization results in a ValueError.

Constraints

  • The server list will contain only strings representing URLs.
  • The number of servers in the list will be between 1 and 100 (inclusive).
  • The get_next_server() method should be efficient; it should return the next server in O(1) time.
  • The class should be well-documented with docstrings explaining the purpose of the class and its methods.

Notes

  • Focus on the core round-robin logic. You don't need to implement actual network requests or server health checks.
  • Consider using a counter variable to keep track of the current server index.
  • Think about how to handle the case where the counter reaches the end of the server list (wrapping around).
  • Error handling is important for robustness. Make sure your code handles invalid input gracefully.
  • The goal is to create a clean, readable, and efficient implementation of a round-robin load balancer.
Loading editor...
python