In my solution I used a simple round robin implementation to choose between the two servers. I separated the balancer module from the frequency to make the implementation cleaner. I've also moved allocated/0
and deallocated/1
to this module since they are the interfaces.
Suppose that the frequency server needs to serve more frequencies. It would be possible to do this just by having a longer list of frequencies available, but it is likely that this scaling is required not only because more frequencies are needed but also because there are more requests to allocate and de-allocate them.
Sharding the handling of frequencies
The idea here is that we can “shard” the handling of the frequencies, so that multiple processes are used to handle different subsets of them.
Design a shared server which has two (old) frequency servers running: one process will be handling the frequencies 10–15 and the other process 20–25. In order for this to present the same API to its clients, you will also need to implement a front-end “router” process that will ensure that requests for allocation are routed to the appropriate server.
Obviously, a request to de-allocate a frequency will need to be routed to the appropriate back-end server, but what should be done in the case of allocation requests? Options include:
- Alternately sending routing requests to one back-end server then the other.
- Keeping a record in the front end of the number of frequencies remaining in each back-end server, and sending the allocation request to the server with the larger number; in the case of a tie another choice mechanism will be needed.