Q8 · How do you decide when to use queues, streams, or direct calls?
Back to 24 Architecture Questions.
The question is not “which broker”. The axis it turns on: whether the consumer needs history and order, or only the work. Three mechanisms move information, and they answer three different questions asked by the receiver.
What the answer should contain
Route by the receiver’s question. “Answer me now”: direct call, where the callee’s latency and failures become the caller’s. “Give me something to do”: queue. “Tell me everything that happened”: stream. The receiver’s need decides; the brand of broker does not.
Direct call: coupled moments. Both sides must exist and be healthy at the same instant. The callee’s latency is your latency, and its failure is your failure to handle. Use it when the answer is part of the caller’s own answer (auth, reservation, validation).
Queue: work only. Delivered, acknowledged, gone. The consumer needs the task, not the context, and nobody needs it twice: at-least-once delivery plus consumers that tolerate a repeat. Cheap to operate per message, but there is no going back: no replay, no late joiners, no rebuilding state from it.
Stream: history and order. An ordered, replayable log of facts. Consumers keep their own cursor, can join late, can re-read everything, and can rebuild a view from scratch. This is what feeds read models, audit trails, and analytics. Heavier to operate, and it pays for itself the first time you rebuild a consumer without asking the producers for anything.
Ordering is a per-key contract. A stream gives ordering within a partition or key; a queue gives ordering within a subscription, at best. Neither gives global ordering for free, and almost no business needs it either.
Ideas worth mentioning out loud
- “A queue moves work; a stream moves truth. Confusing the two is how systems end up trying to rebuild their database from a to-do list.”
- A stream can masquerade as a queue (consumer groups): decide which you are using, because the ops story differs: retention, lag monitoring, rebuilds.
- Direct calls inside a loop are fan-out done the slow way. A queue turns that loop into parallel consumers.
Follow-ups to expect
- Can a queue give ordering? Within a FIFO subset, yes; history, never. If the consumer ever needs to ask “what did I miss”, it needed a stream.
- Why not everything on the stream? Direct answers and one-shot jobs pay stream overhead for nothing. Match the mechanism to the receiver’s question.
- What about the broker dying? Queues and streams both need HA replication; direct calls need explicit timeouts, capped retries, and their own pools instead.
Red flags in an answer
- One mechanism chosen for every interaction.
- A stream used for fire-and-forget jobs, with retention and lag monitoring nobody watches.
- “Eventually consistent” reached by accident, because nobody decided what the receiver needs.
Sources
- The Log: What every software engineer should know about real-time data’s unifying abstraction, Jay Kreps: the case for the stream as a first-class design element, including why a log can play a queue but not the reverse.
- Pattern: Messaging, Chris Richardson, microservices.io: the queue’s semantics, including the ordering caveats that make history and order the stream’s distinguishing claim.