Queues, topics and events
Why use asynchronous messaging?
A message is a piece of data sent between components. A broker is a service that accepts and forwards or stores messages according to configured rules. Asynchronous communication lets a producer hand off work without waiting for the consumer to finish it. This separates the speed of accepting an order from the speed of packing it, but introduces delayed completion and new failure cases.
At Byte Books, the order API records an order and arranges for a packing request to be published. A warehouse worker processes that request later. The response to the shopper should indicate that the order was accepted, not claim that packing is complete. A successful publish acknowledgment also does not prove that the warehouse has finished.
An event describes something that happened, such as OrderAccepted; a command asks a component to do something, such as PackOrder. Both can travel as messages. The difference is intent and the contract between components. Do not rely on naming alone for security or validation.
Queue versus topic versus event bus
Order API -> packing queue -> worker A OR worker BOrderAccepted -> topic -> inventory subscription -> reporting subscriptionFileUploaded -> event bus -> rule -> thumbnail handlerA queue distributes work among competing consumers. One logical task should be completed by one worker, but delivery can repeat. A topic enables fan-out: independent subscriptions receive their own copies, and workers sharing one subscription divide its work. A subscriber that is offline may accumulate messages when retention and subscription settings support it. A topic without a retained subscription is not universally a durable backlog.
An event bus matches events to destinations using rules. Routing by event type or attributes means the publisher need not know every target. A stream is a retained ordered log, often partitioned, where readers track positions and can replay available records. A queue is usually consumed and acknowledged; a stream can support multiple independent readers without removing each record on read. Products can overlap, so verify their actual retention and delivery contracts.
Example: use a packing queue for 120 orders, a topic with two matching subscriptions for inventory and analytics, and an event router to send only FileUploaded to an image-processing service. The topic creates 240 logical subscription deliveries before retries if all 120 events match both subscriptions.
Compare the major providers
Choose by required behavior, not by assuming similar service names mean identical APIs.
- AWS: Amazon SQS buffers work for consumers; SNS distributes notifications to subscribers; EventBridge routes events by rules. SQS Standard may duplicate or reorder messages. FIFO queues and SNS FIFO combinations offer scoped ordering and send deduplication, not unconditional exactly-once business effects.
- Azure: Service Bus queues and topics support brokered business messages; sessions can provide ordered handling for related messages. Event Grid distributes discrete events to handlers, while Event Hubs is a partitioned ingestion and replay service for streams such as telemetry. Verify tier and mode for sessions, duplicate detection and transactions.
- Google Cloud: Pub/Sub topics and subscriptions provide asynchronous distribution; several workers can consume one subscription or separate subscriptions can fan out. Eventarc Standard routes events to supported destinations. Pub/Sub ordering is opt-in and scoped to ordering keys and publishing region; exactly-once is a separate pull-subscription feature.
Sources: AWS messaging decision guide, Azure service comparison, Pub/Sub subscription properties, Eventarc retry behavior. Reviewed on 2026-11-28. No live product parity or tier limits are implied.
Size workers and backlog
Suppose incoming work is 80 messages/s and four workers each finish 15 messages/s. The theoretical service capacity is 60 messages/s. Under constant arrivals and service, the backlog grows by 20 messages/s, or 1,200 messages in 60 seconds. If traffic drops to 30 messages/s and capacity remains 60, the net drain rate is 30 messages/s; it takes 40 seconds to drain that backlog.
To handle 80 messages/s with 15 messages/s per worker, the minimum theoretical count is six (round 80/15 up). This does not leave much headroom. Seven workers provide 105 messages/s only if downstream storage, locks and networking can sustain the work. Real processing rates vary. Use oldest unprocessed message age, backlog, worker throughput, error rate and downstream saturation together. A high count alone does not distinguish a burst from a stuck consumer.
Batching groups several messages into one receive or publish call, reducing per-call overhead. It does not remove per-message work or guarantee atomic effects. For 103 messages at most 10 per batch, at least 11 batches are needed if every batch except the last is full. Providers may bill by bytes, requests, operations or throughput units, so batch count is not a universal billing formula.
Backpressure means limiting input or in-flight work to match processing capacity. Bound worker concurrency and queue retention, avoid unlimited retry loops and apply producer-side rate control when necessary. Use a queue to absorb temporary bursts, not to hide persistent undercapacity.
Design the message contract and permissions
A small fictional event can be represented as:
{ "event_id": "E17", "type": "OrderAccepted", "schema_version": 1, "order_id": "B17", "quantity": 2}Keep event IDs stable across retries of the same logical event. A correlation ID links work across services; it need not uniquely identify one message. Use schema versions and compatible changes, validate untrusted fields, and document who owns the event type. Avoid credentials, unnecessary personal data and large payloads; a reference to approved storage may be preferable when the consumer has permission to retrieve it.
Grant the producer only publish rights and workers only the consumption rights required for their subscription or queue. Separate administrative redrive privileges, protect transport and stored data, and redact sensitive message bodies from logs. A queue is not an authorization boundary for every business action. The consumer must still enforce its data and state invariants.
Practice: choose a topology for one packing operation and two independent downstream reactions, calculate worker capacity, then explain what a successful publish actually confirms. Next, learn how to handle redelivery safely.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Queues, topics and events — knowledge check
Choose the best answer for each offline scenario.
Which topology assigns one packing task to competing workers?
120 events match two independent subscriptions. How many logical deliveries before retries?
Which Azure service is intended for retained telemetry streams?
80 messages/s arrive and workers finish 60/s for 60 seconds. How much backlog grows?
A producer gets a successful publish confirmation. What does it establish?