Video in progress

What's the difference between the queue and topic (publish/subscribe) models?

In a queue, each message is taken by only one consumer: the work is divided. In a topic, the message goes to all subscribers: each subscriber group gets its own copy.

📖 Answer

With a simple example:

  • Queue: "resize the images" tasks. There are 5 workers, and each image is processed by one of them. There's no need to do it twice.
  • Topic: the "order created" event. email, warehouse and analytics each need to receive it for themselves.

In Kafka both are done through consumer groups:

topic: order-created
  group "email-service"     -> 3 instances, messages are split among them
  group "warehouse-service" -> 2 instances, they also get every message

So inside one group it works like a queue, and across groups it works like pub/sub.

In RabbitMQ it's built from exchanges and queues: a fanout exchange distributes copies to several queues, and each queue is read by its own consumers.