Hone logo
Hone
Problems

Simple Round Robin Load Balancer in Go

This challenge asks you to implement a basic round-robin load balancer in Go. Load balancing is a crucial technique for distributing network traffic across multiple servers to prevent overload on any single server, improving performance and reliability. Your task is to create a Go program that cycles through a list of backend servers, directing requests to each server in a sequential order.

Problem Description

You need to implement a LoadBalancer struct with a Serve() method. The LoadBalancer struct should hold a slice of backend server addresses (strings). The Serve() method should return the next server address in a round-robin fashion. The method should maintain an internal state (a counter) to track the current server index. Each call to Serve() should return the next server in the list, wrapping around to the beginning when the end of the list is reached.

Key Requirements:

  • Round-Robin Logic: The core functionality must implement a true round-robin algorithm.
  • Server List Management: The load balancer must manage a list of backend servers.
  • Stateful Behavior: The load balancer must maintain internal state to track the current server.
  • Error Handling: If the server list is empty, Serve() should return an error.

Expected Behavior:

  • The first call to Serve() should return the first server in the list.
  • Subsequent calls should return the next server in the list, cycling back to the first server when the end of the list is reached.
  • The server list should not be modified by the Serve() method.

Edge Cases to Consider:

  • Empty Server List: What should happen if the server list is empty?
  • Single Server: What happens if there's only one server in the list?
  • Large Number of Servers: Consider the potential for a large number of servers in the list.

Examples

Example 1:

Input: servers := []string{"server1.com", "server2.com", "server3.com"}
Serve() called repeatedly

Output:

server1.com
server2.com
server3.com
server1.com
server2.com
...

Explanation: The Serve() method cycles through the servers in the order they appear in the slice.

Example 2:

Input: servers := []string{"server1.com"}
Serve() called repeatedly

Output:

server1.com
server1.com
server1.com
...

Explanation: With only one server, the Serve() method always returns the same server.

Example 3:

Input: servers := []string{}
Serve()

Output:

Error: No servers available

Explanation: When the server list is empty, Serve() returns an error.

Constraints

  • The server addresses are strings.
  • The number of servers in the list can range from 0 to 1000.
  • The Serve() method should be efficient; avoid unnecessary computations.
  • The LoadBalancer struct should be thread-safe (although this challenge doesn't explicitly require concurrency testing, consider the implications).

Notes

  • Consider using a counter variable to keep track of the current server index.
  • The modulo operator (%) can be helpful for implementing the round-robin logic and wrapping around the server list.
  • Think about how to handle the edge case of an empty server list gracefully. Returning an error is a good approach.
  • While not required for this basic implementation, consider how you might extend this to support more sophisticated load balancing algorithms (e.g., weighted round robin, least connections).
Loading editor...
go