Design an async, batched key-value fetcher with a 100 ms window
Context
You have a single-threaded, event-loop environment (e.g., JS/TS). A backend endpoint serves numeric values for keys via HTTP GET:
-
Endpoint:
GET /read?keys=k1,k2,...
-
Response: a JSON object mapping each requested key to its numeric value, for example:
{ "foo": 12, "bar": 100 }
.
Available primitives:
-
httpGetAsync(urlPath, callback) -> void
where
callback(err, jsonString)
.
-
parseKvs(jsonString) -> Map<string, number>
that parses the JSON string into a map of key → value.
Task
Implement a single-threaded (event-loop) interface:
-
getKeyAsync(key, callback)
that supports concurrent I/O and batches multiple incoming requests.
Requirements
-
Batch all requests that arrive within a 100 ms window into a single HTTP call to
/read?keys=...
.
-
Deduplicate keys within a batch; if multiple requests ask for the same key, invoke each request’s callback exactly once.
-
Enforce consistency: a batch response may only satisfy callbacks for requests that arrived before that batch was dispatched. Requests arriving afterward must be served by a subsequent batch.
-
Allow multiple in-flight batches without mixing responses.
-
Define the data structures (queues, maps, sets), control flow (timers, scheduling), and how responses are correlated to waiting callbacks.
-
Specify behavior for missing keys, partial failures, timeouts/retries, and cancellation/backpressure.
-
Discuss complexity (time/space), single-threaded ordering pitfalls, and a brief test plan.