KatInterview

Fix a Rotating Server Picker, Then Build a Hash Ring

Data Structure DesignModel answer
MediumModel answerData Structure Designload-balancingconsistent-hashingdebugging

Last updated

Find and fix the bugs in a round-robin server selector that handles pool changes and threads, then build a consistent-hash ring with virtual nodes that keeps key movement minimal.

A traffic router sends each incoming request to one server in a pool. Servers are identified by unique strings, and the pool can grow or shrink while the system is running. A single selector object is shared by every worker thread in the process.

Part 1: Repair the rotating selector

The class below should return servers in strict rotation (A, B, C, A, B, C, ...) and keep rotating sensibly when servers join or leave.

class RoundRobinBalancer:
    def __init__(self, servers):
        self.servers = servers
        self.index = 0

    def add_server(self, server):
        self.servers.append(server)

    def remove_server(self, server):
        self.servers.remove(server)

    def next_server(self):
        server = self.servers[self.index]
        self.index = self.index + 1
        if self.index > len(self.servers):
            self.index = 0
        return server

Find as many defects as you can. For each, give a short call sequence that triggers it and name the root cause. Then write a corrected class. Your answer should also say what happens with an empty pool, what happens when many threads call it at once, and which server follows a removal that happens mid-rotation.

Part 2: Build a consistent-hash ring

Rotation ignores what the request is about, so the same user can hit a different server every time, which defeats per-server caches and session state. Implement a ring where a given key always resolves to the same server while the pool is unchanged. Adding or removing one server should move only about that server's share of keys, rather than reshuffling nearly everything as hash(key) % server_count does.

class ConsistentHashRing:
    def add_server(self, server: str) -> None: ...
    def remove_server(self, server: str) -> None: ...
    def get_server(self, key: str) -> str: ...

Explain and justify:

  • The data structure, and the cost of lookups and membership changes.
  • Your hash function, including whether the mapping must agree across processes and restarts.
  • How you keep load even across servers, and how you would give bigger servers more traffic.
  • Which keys move, and where they go, when a server joins or leaves.
  • How you would test both parts.

Hints

Hint 1

Trace the index by hand with a three-server pool. Check what happens when it reaches the end of the list, and what happens when you remove a server that sits before, at, or after the index.

Hint 2

Several bugs are not about the index at all: think about who owns the list passed to the constructor, what happens on an empty pool, and what two threads can do between reading and updating the index.

Hint 3

For the ring, hash each server to many points on a circle (virtual nodes), keep the points sorted, and find a key's owner with a binary search for the first point at or after the key's hash, wrapping around at the end. Avoid Python's built-in hash() for strings.