We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Multi Consumers

A queue can have 0, 1, or many consumers.

  • If a queue has no consumers, messages will accumulate in the queue and never be processed.
  • If a queue has one consumer, that consumer will process all messages in the queue (assuming it can keep up).
  • If a queue has many consumers, messages will be distributed between them in a round-robin fashion (unless you set a priority).

Multiple queues each receive a copy of a message, but multiple consumers on one queue split the messages so each message is handled once.

The exclusive flag can be used to tell the RabbitMQ server to only allow one consumer to connect to the queue at a time. I've found that often my pub/sub needs fall into one of two categories:

  • Process an event once-per-server-instance (good for ephemeral, exclusive queues with one consumer)
  • Process an event once, period (good for durable, non-exclusive queues with many consumers)

Answer the questions about the following examples.

Example 1

Pretend we have an architecture where users can connect to one of three game servers for live game updates. When a player's guild goes into battle, we need to notify the connected user that they won. The trouble is, we don't know which server the user is connected to. For simplicity, we decide to send the message to all three servers, and let the user's server handle the message (send it if the user is connected, ignore it if they aren't).

Example 2

Pretend we have to resize, compress, and upload images to a cloud storage bucket. We have 20 servers that can handle the image processing. We want to make sure that each image is only processed once, but we don't care which server processes it.