Implement getKeyAsync with 100 ms batching and a single GET per batch
You are working in a single-threaded environment with concurrent I O (think event loop). Implement a function:
-
getKeyAsync(key, callback) -> void
Within any 100 ms window, the implementation must batch multiple incoming key requests and issue exactly one HTTP request per batch:
-
HTTP GET to path: /read?keys=k1,k2,...
You are given two helpers:
-
httpGetAsync(urlPath, callback) -> void, which asynchronously returns either an error or a JSON payload
-
parseKvs(json) -> Map<String, Integer>, which parses the JSON into a map from key to integer
After the batch response arrives, parse it and invoke each original per-key callback with its value.
Consistency requirement:
-
A batch response may only serve callbacks for requests that arrived before the batch HTTP call was initiated. It must not serve later requests.
Also specify and design for the following:
-
Handling duplicate keys within the same window
-
Missing keys in the response
-
Callback ordering
-
Late arrivals that straddle batch boundaries
-
Timeouts and retries
-
Backpressure when requests outpace the network
-
Error propagation to callbacks
-
Graceful shutdown or drain behavior
Include a brief rationale about when you would choose callbacks versus multi-threading for this problem, and the trade-offs in complexity, latency, and throughput.