Message Queues
Overview
Message queues and publish-subscribe are two asynchronous messaging styles that let services communicate without calling each other directly. Point-to-point delivery routes each message to exactly one consumer, while publish-subscribe broadcasts each message to every interested subscriber; both decouple producers from consumers in time and in load.
Key Concepts
Both styles put a broker between senders and receivers so the producer never needs to know who consumes its messages or when.
- Point-to-point (queue): producers append messages to a queue, and competing consumers pull from it. Each message is delivered to only one consumer, so adding consumers spreads the workload. This is ideal for task distribution, such as processing orders or resizing images.
- Publish-subscribe (topic): publishers send messages to a topic, and every subscriber receives its own copy. This fans one event out to many independent consumers, such as notifying billing, search indexing, and analytics about the same order.
In the queue, only one of Consumer A or B receives any given message; in the topic, both subscribers receive every message. Because delivery is usually at-least-once, consumers can see duplicates, which makes idempotency essential. This decoupling is the foundation of event-driven architecture, where components react to events rather than invoking one another.
| Property | Point-to-point queue | Publish-subscribe topic |
|---|---|---|
| Recipients per message | Exactly one consumer | All subscribers |
| Primary use | Distribute work | Broadcast events |
| Adding consumers | Increases throughput | Adds another full copy stream |
Trade-offs
Asynchronous messaging absorbs traffic spikes and lets producers and consumers scale and fail independently, but it adds a broker to operate and makes end-to-end flows harder to trace. Queues give natural load sharing yet a single logical stream; topics give broad fan-out yet can overwhelm slow subscribers. Ordering and exactly-once semantics are limited and often require partition keys or deduplication on top.
Interview Tips
- Match the style to intent: "one worker should do this" means a queue; "many services care about this" means a topic.
- Always mention at-least-once delivery and pair it with idempotent consumers.
- Discuss ordering explicitly, since global ordering is rare and usually scoped to a partition key.
- Call out back-pressure and dead-letter handling for messages that repeatedly fail.
Summary
- Message queues and publish-subscribe are asynchronous styles that decouple producers from consumers.
- Point-to-point delivers each message to exactly one consumer, spreading work across a pool.
- Publish-subscribe broadcasts each message to every subscriber, fanning one event to many services.
- At-least-once delivery is common, so consumers must be idempotent to tolerate duplicates.
- Both styles buffer spikes and let producers and consumers scale independently through a broker.