Question presented to candidate: "A producer in your system, say an ingestion API or an upstream service, can sometimes send work faster than the downstream consumer can process it. What is backpressure, what goes wrong if you ignore it, and what are your options for handling it?"
What a strong answer should cover:
- The definition: a way for a slow consumer to signal a fast producer to slow down, so work is not accepted faster than it can be finished
- Why ignoring it fails: an unbounded buffer grows until memory runs out, and latency grows with it (Little's law: wait = queue length / service rate)
- The options: bound the buffer, then block or slow the producer, shed load (drop or reject with
429/503), or switch to pull / credit-based flow where the consumer asks for N items - Real mechanisms: TCP flow control (receive window), Node.js streams (
write()returningfalse, the'drain'event,pipeline()), Reactive Streamsrequest(n), pull-based consumers like Kafka - Backpressure must propagate to the edge, where a client can be told to back off; buffering in the middle only delays the problem
- Trade-offs: blocking keeps every message but adds latency upstream; dropping keeps latency bounded but loses work; choose per data type
Code / implementation expected: Optional โ explaining write()/'drain' or a bounded queue is a plus; the examples simulate four strategies and run real Node.js stream backpressure.