Implement a round-robin reverse proxy with per-domain machine health and shared logs

Quick Overview

Implement an in-memory round-robin reverse proxy where each domain has its own queue of backend machines, health changes apply to one domain at a time, and each machine keeps one request log across all its domains. It tests queue rotation, per-connection state, ordering rules for removal and restore, and handling of invalid calls without side effects.

Implement a round-robin reverse proxy with per-domain machine health and shared logs

Company: BrowserStack

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Implement an in-memory simulation of a round-robin reverse proxy. It does not use real HTTP, processes, persistent files or threads. Everything is simulated with plain data structures. A `ReverseProxy` starts empty. In a setup phase, you register domains and add backend machines to each domain's queue. Each domain has its own round-robin queue, and a machine can belong to several domains. All setup calls happen before any request routing, health change or log query. Each request goes to the machine at the front of its domain's queue. That machine then moves to the back, and the request `domain + path` is added to the machine's log. A machine has one log, shared by all the domains it serves. Health events are connection-local: taking a machine down in one domain does not affect its other domains. Down machines leave the queue and the other machines keep their order. A restored machine goes back at the end of the current queue. Implement the following methods: - `addDomain(domain: str) -> bool`: Setup only. Creates an empty queue for the domain and returns true. If the domain already exists, returns false and changes nothing. - `addBackend(domain: str, machine: str) -> bool`: Setup only. Adds the machine as an up connection at the end of the domain's queue and returns true. If the machine is already in this domain, returns false and changes nothing. - `dispatch(domain: str, path: str) -> str`: Takes the first machine from the domain's queue, moves it to the back, appends `domain + path` to that machine's log and returns the machine ID. If the queue is empty, returns an empty string and logs nothing. - `machineDown(domain: str, machine: str) -> bool`: Removes the machine from this domain's queue only. The other machines keep their order. Returns false and changes nothing if the machine is not in this domain or is already down here. - `machineUp(domain: str, machine: str) -> bool`: Brings a down machine in this domain back up by adding it at the end of the domain's current queue. Returns false and changes nothing if the machine is already up here or is not in this domain. - `getLogs(machine: str) -> List[str]`: Returns a copy of every request this machine has handled across all domains, oldest first. Returns `[]` if the machine is unknown or has no requests. Example 1 shows rotation inside one domain and a log shared across domains. The comment after each call shows the domain's queue once the call returns. ```text addDomain("shop.com") -> true addDomain("blog.com") -> true addDomain("shop.com") -> false # already exists addBackend("shop.com", "m1") -> true # shop.com: [m1] addBackend("shop.com", "m2") -> true # shop.com: [m1, m2] addBackend("blog.com", "m1") -> true # blog.com: [m1] addBackend("shop.com", "m1") -> false # m1 is already in shop.com dispatch("shop.com", "/home") -> "m1" # shop.com: [m2, m1] dispatch("blog.com", "/post") -> "m1" # blog.com: [m1] dispatch("shop.com", "/cart") -> "m2" # shop.com: [m1, m2] dispatch("shop.com", "/pay") -> "m1" # shop.com: [m2, m1] getLogs("m1") -> ["shop.com/home", "blog.com/post", "shop.com/pay"] getLogs("m2") -> ["shop.com/cart"] getLogs("m7") -> [] ``` After the first dispatch, `m1` moves behind `m2`, so the next `shop.com` request goes to `m2`. The `blog.com` request in between comes from a separate queue, yet it lands in the same log as the `shop.com` requests `m1` served. Example 2 shows health changes. ```text addDomain("api.com") -> true addDomain("web.com") -> true addBackend("api.com", "m1") -> true # api.com: [m1] addBackend("api.com", "m2") -> true # api.com: [m1, m2] addBackend("api.com", "m3") -> true # api.com: [m1, m2, m3] addBackend("web.com", "m2") -> true # web.com: [m2] dispatch("api.com", "/v1") -> "m1" # api.com: [m2, m3, m1] machineDown("api.com", "m2") -> true # api.com: [m3, m1] machineDown("api.com", "m2") -> false # already down in api.com dispatch("web.com", "/home") -> "m2" # m2 is still up in web.com dispatch("api.com", "/v2") -> "m3" # api.com: [m1, m3] machineUp("api.com", "m2") -> true # api.com: [m1, m3, m2] dispatch("api.com", "/v3") -> "m1" # api.com: [m3, m2, m1] dispatch("api.com", "/v4") -> "m3" # api.com: [m2, m1, m3] dispatch("api.com", "/v5") -> "m2" # api.com: [m1, m3, m2] machineDown("web.com", "m2") -> true # web.com: [] dispatch("web.com", "/home") -> "" # empty queue, nothing logged machineUp("web.com", "m1") -> false # m1 is not in web.com getLogs("m2") -> ["web.com/home", "api.com/v5"] ``` Taking `m2` down in `api.com` removes it from that queue only, so `web.com` still routes to it. When `m2` comes back in `api.com`, it joins behind `m1` and `m3` even though it was registered between them. Once `web.com` has no up machine, its dispatch returns an empty string and `m2`'s log does not change. ```hint Where state lives Health belongs to a (domain, machine) pair, while the log belongs to the machine alone. Decide which structures are keyed by domain and which by machine before writing any method. ``` ```hint Down machines and the queue Choose whether a down machine stays in the queue and gets skipped, or leaves it. One choice keeps dispatch at constant time; work out what it costs machineDown and where it puts a restored machine. ``` ### Constraints & Assumptions - 1 to 100 domains. - 0 to 100 machines per domain pool. - At most ${10^4}$ total operations. - Every identifier and path has length 1 to 100. - Identifiers and paths contain only ASCII letters, digits, `.`, `-`, `/`, `_` and `?`. - Every `path` begins with `/`. - All `addDomain` and `addBackend` calls happen before any `dispatch`, `machineDown`, `machineUp` or `getLogs` call. - `addBackend`, `dispatch`, `machineDown` and `machineUp` always name an existing domain. ### What a Strong Answer Covers - The split of state: a per-domain queue of up machines, per-domain membership and health, and one per-machine log shared by all domains. - How dispatch rotates the queue in constant time and handles an empty queue without logging. - How machineDown and machineUp keep the order rules, and how every invalid call returns false before changing any state. - The time cost of each method and the total space, with getLogs returning a copy of the history. - Edge cases: a duplicate domain or backend, one machine in several domains, an unknown machine in getLogs, and a domain whose machines are all down. ### Follow-up Questions - How would you make machineDown run in constant time while keeping the same ordering rules? - How would you support weighted round robin, where a machine with weight 3 receives three requests per cycle for every one sent to a machine with weight 1? - If addBackend could be called after routing has started, where should the new machine enter the queue, and what in your design changes? - How would you make dispatch and the health methods safe when calls arrive from many threads at once?

Overview: Implement an in-memory round-robin reverse proxy where each domain has its own queue of backend machines, health changes apply to one domain at a time, and each machine keeps one request log across all its domains. It tests queue rotation, per-connection state, ordering rules for removal and restore, and handling of invalid calls without side effects.

BrowserStack logo
BrowserStack
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Implement an in-memory simulation of a round-robin reverse proxy. It does not use real HTTP, processes, persistent files or threads. Everything is simulated with plain data structures.

A ReverseProxy starts empty. In a setup phase, you register domains and add backend machines to each domain's queue. Each domain has its own round-robin queue, and a machine can belong to several domains. All setup calls happen before any request routing, health change or log query.

Each request goes to the machine at the front of its domain's queue. That machine then moves to the back, and the request domain + path is added to the machine's log. A machine has one log, shared by all the domains it serves. Health events are connection-local: taking a machine down in one domain does not affect its other domains. Down machines leave the queue and the other machines keep their order. A restored machine goes back at the end of the current queue.

Implement the following methods:

  • addDomain(domain: str) -> bool : Setup only. Creates an empty queue for the domain and returns true. If the domain already exists, returns false and changes nothing.
  • addBackend(domain: str, machine: str) -> bool : Setup only. Adds the machine as an up connection at the end of the domain's queue and returns true. If the machine is already in this domain, returns false and changes nothing.
  • dispatch(domain: str, path: str) -> str : Takes the first machine from the domain's queue, moves it to the back, appends domain + path to that machine's log and returns the machine ID. If the queue is empty, returns an empty string and logs nothing.
  • machineDown(domain: str, machine: str) -> bool : Removes the machine from this domain's queue only. The other machines keep their order. Returns false and changes nothing if the machine is not in this domain or is already down here.
  • machineUp(domain: str, machine: str) -> bool : Brings a down machine in this domain back up by adding it at the end of the domain's current queue. Returns false and changes nothing if the machine is already up here or is not in this domain.
  • getLogs(machine: str) -> List[str] : Returns a copy of every request this machine has handled across all domains, oldest first. Returns [] if the machine is unknown or has no requests.

Example 1 shows rotation inside one domain and a log shared across domains. The comment after each call shows the domain's queue once the call returns.

addDomain("shop.com")            -> true
addDomain("blog.com")            -> true
addDomain("shop.com")            -> false   # already exists
addBackend("shop.com", "m1")     -> true    # shop.com: [m1]
addBackend("shop.com", "m2")     -> true    # shop.com: [m1, m2]
addBackend("blog.com", "m1")     -> true    # blog.com: [m1]
addBackend("shop.com", "m1")     -> false   # m1 is already in shop.com
dispatch("shop.com", "/home")    -> "m1"    # shop.com: [m2, m1]
dispatch("blog.com", "/post")    -> "m1"    # blog.com: [m1]
dispatch("shop.com", "/cart")    -> "m2"    # shop.com: [m1, m2]
dispatch("shop.com", "/pay")     -> "m1"    # shop.com: [m2, m1]
getLogs("m1")                    -> ["shop.com/home", "blog.com/post", "shop.com/pay"]
getLogs("m2")                    -> ["shop.com/cart"]
getLogs("m7")                    -> []

After the first dispatch, m1 moves behind m2, so the next shop.com request goes to m2. The blog.com request in between comes from a separate queue, yet it lands in the same log as the shop.com requests m1 served.

Example 2 shows health changes.

addDomain("api.com")             -> true
addDomain("web.com")             -> true
addBackend("api.com", "m1")      -> true    # api.com: [m1]
addBackend("api.com", "m2")      -> true    # api.com: [m1, m2]
addBackend("api.com", "m3")      -> true    # api.com: [m1, m2, m3]
addBackend("web.com", "m2")      -> true    # web.com: [m2]
dispatch("api.com", "/v1")       -> "m1"    # api.com: [m2, m3, m1]
machineDown("api.com", "m2")     -> true    # api.com: [m3, m1]
machineDown("api.com", "m2")     -> false   # already down in api.com
dispatch("web.com", "/home")     -> "m2"    # m2 is still up in web.com
dispatch("api.com", "/v2")       -> "m3"    # api.com: [m1, m3]
machineUp("api.com", "m2")       -> true    # api.com: [m1, m3, m2]
dispatch("api.com", "/v3")       -> "m1"    # api.com: [m3, m2, m1]
dispatch("api.com", "/v4")       -> "m3"    # api.com: [m2, m1, m3]
dispatch("api.com", "/v5")       -> "m2"    # api.com: [m1, m3, m2]
machineDown("web.com", "m2")     -> true    # web.com: []
dispatch("web.com", "/home")     -> ""      # empty queue, nothing logged
machineUp("web.com", "m1")       -> false   # m1 is not in web.com
getLogs("m2")                    -> ["web.com/home", "api.com/v5"]

Taking m2 down in api.com removes it from that queue only, so web.com still routes to it. When m2 comes back in api.com, it joins behind m1 and m3 even though it was registered between them. Once web.com has no up machine, its dispatch returns an empty string and m2's log does not change.

Constraints & Assumptions

  • 1 to 100 domains.
  • 0 to 100 machines per domain pool.
  • At most 104{10^4} total operations.
  • Every identifier and path has length 1 to 100.
  • Identifiers and paths contain only ASCII letters, digits, . , - , / , _ and ? .
  • Every path begins with / .
  • All addDomain and addBackend calls happen before any dispatch , machineDown , machineUp or getLogs call.
  • addBackend , dispatch , machineDown and machineUp always name an existing domain.

What a Strong Answer Covers Guidance

  • The split of state: a per-domain queue of up machines, per-domain membership and health, and one per-machine log shared by all domains.
  • How dispatch rotates the queue in constant time and handles an empty queue without logging.
  • How machineDown and machineUp keep the order rules, and how every invalid call returns false before changing any state.
  • The time cost of each method and the total space, with getLogs returning a copy of the history.
  • Edge cases: a duplicate domain or backend, one machine in several domains, an unknown machine in getLogs, and a domain whose machines are all down.

Follow-up Questions Guidance

  • How would you make machineDown run in constant time while keeping the same ordering rules?
  • How would you support weighted round robin, where a machine with weight 3 receives three requests per cycle for every one sent to a machine with weight 1?
  • If addBackend could be called after routing has started, where should the new machine enter the queue, and what in your design changes?
  • How would you make dispatch and the health methods safe when calls arrive from many threads at once?