We run into an interesting scenario at work that I thought would make for a pretty good interview task. Consider a server that needs to proxy a request from the outside world to an internal service, something like this:
That isn’t that interesting. The interesting bit is that the network between the internal server and the proxy is running at 10Gb/sec and the external network is limited to 512Kb/sec.
Furthermore, the internal server expects the other side to just… take the load. It will blast the other side with as much data as it can, and if you can’t handle that, will cut the connection. What this means is that for small requests, the proxy can successfully pass the data to the external server, but for larger ones, it is unable to read the data quickly enough to do so and the internal server will disconnect from it.
It is the responsibility of the proxy to manage that scenario. That is the background for this task, practically speaking, this means that you have the following code, which works if the size is 8MB but fails if it is 64MB.
We have the SlowClientStream and the FastServerStream – which means that we are able to focus completely on the task at hand (ignoring networks, etc).
The requirement is to pass a 64 MB of data between those two streams (which have conflicting requirements)
- The FastServerStream requires that you’ll read from it in a rate of about 31Kb / sec.
- The SlowClientStream, on the other hand, will accept data at a maximum rate of about 30Kb/sec (but is variable across time).
You may not change the implementation of either stream (but may add behavior in separate classes).
You may not read the entire response from the server before sending to the client.
There is a memory limit of 32 MB on the maximum amount of memory you may use in the program.
How would you go about solving this?