Library
9 min read

Queue vs pub/sub vs event stream

CoreQueues and streams

All three let one part of a system hand messages to another without waiting for it. They differ in who gets each message and how long it is kept.

  • A queue is a stack of orders at a restaurant pass. Each ticket is picked up by one cook, and once cooked it is gone.
  • Pub/sub is a WhatsApp group. Each message goes to every member who is in the group right now.
  • An event stream is the restaurant's order book. Every order is written down in sequence and kept. The kitchen reads it, the accountant reads it at night, and a new manager can read last month's from page one.
Your answer

When an order is placed on Swiggy, the restaurant must be notified, an invoice emailed, loyalty points added, and the analytics team wants every order since 2023 to train a model. Would one queue do? What would you use for each?

Who gets the message
Competing consumers. Each message goes to exactly one worker, and is deleted once acked. Add workers to go faster. This is how you spread work: sending emails, resizing images, generating invoices.

Queue#

The point of a queue is distributing work. Producers drop jobs in; a pool of workers pulls them; each job is done once (at least once, strictly; see delivery guarantees) and removed. Queues absorb bursts (the queue grows, workers catch up), let you scale workers independently, and retry failures with a dead letter queue for messages that keep failing.

Amazon SQS, RabbitMQ queues, Celery and Sidekiq on Redis, and Postgres tables with SKIP LOCKED all play this role. Ordering is usually not guaranteed across workers (SQS FIFO queues and RabbitMQ single consumers are exceptions).