Design async batched key-value fetcher

Quick Overview

This question evaluates understanding of designing asynchronous, batched request handling in a single-threaded event-loop, covering batching windows, key deduplication, response-to-callback correlation, consistency boundaries, error handling, timeouts/retries, cancellation, and backpressure.

Design async batched key-value fetcher

Company: Anrok

Role: Software Engineer

Category: System Design

Difficulty: hard

Interview Round: Technical Screen

You are given a backend endpoint: HTTP GET /read?keys=k1,k2,... which returns a JSON object mapping each key to a numeric value (e.g., { "foo": 12, "bar": 100 }). You also have two primitives: ( 1) httpGetAsync(urlPath, callback) -> void, and ( 2) parseKvs(json) -> a dictionary mapping keys to values. 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.

Overview: This question evaluates understanding of designing asynchronous, batched request handling in a single-threaded event-loop, covering batching windows, key deduplication, response-to-callback correlation, consistency boundaries, error handling, timeouts/retries, cancellation, and backpressure.

|Home/System Design/Anrok
Anrok logo
Anrok
Sep 6, 2025
hardSoftware EngineerTechnical ScreenSystem Design
17
0

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

  1. Batch all requests that arrive within a 100 ms window into a single HTTP call to /read?keys=... .
  2. Deduplicate keys within a batch; if multiple requests ask for the same key, invoke each request’s callback exactly once.
  3. 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.
  4. Allow multiple in-flight batches without mixing responses.
  5. Define the data structures (queues, maps, sets), control flow (timers, scheduling), and how responses are correlated to waiting callbacks.
  6. Specify behavior for missing keys, partial failures, timeouts/retries, and cancellation/backpressure.
  7. Discuss complexity (time/space), single-threaded ordering pitfalls, and a brief test plan.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...