One Queue or Three

The broker now holds three queues: the order queue, the email queue, and the shipping queue. Could we use one queue instead of three? Each message would say what it is: an order to place, an email to send, or an order to ship.

One queue for all the work

It can work, but it costs more than three queues. A consumer takes whatever message is next on a queue. Suppose the shipping service takes the next message, and it is an email to send. The shipping service cannot send it, so it has to put the message back on the queue for another consumer.

This has two costs. Each consumer spends time taking messages that are not its own and putting them back. And if the notification service is down, no one sends the emails. The other consumers keep taking the emails and putting them back.

With three queues, each consumer takes only its own work.

Some brokers let the consumers of one queue take only the messages with a given label. RabbitMQ and Amazon SQS (Simple Queue Service), the two brokers from the previous chapter, do not. So we keep one queue for each kind of work.

Two messages for one order

For order 93, the order worker sends two messages. On the email queue, it puts this message:

{ "shopId": "mugshop", "orderId": "93", "email": "order-placed" }

On the shipping queue, it puts this one:

{ "shopId": "mugshop", "orderId": "93" }

Both messages say the same thing: order 93 was placed. Each service reads the other data it needs from the shared database. The notification service reads the shopper’s email address, and the shipping service reads the shopper’s address and the order’s items.

Point-to-point

Each message on a queue goes to one consumer. This is called point-to-point messaging. So to reach two services, the order worker sends two messages. It puts one on each service’s queue.

What the order worker has to know

Recall decoupling from the previous chapter: the producer knows only the queue, not which program handles the message or where it runs. That still holds for each of the two queues. The order worker does not know how the notification service sends an email or how the shipping service ships an order.

But the order worker has to know one queue for each service that acts on a placed order. Suppose another team adds a service that also needs to act on each placed order. The commerce service’s team has to change the order worker so that it puts a third message on the new service’s queue, and then release the commerce service. Placing an order works the same as before, but the commerce service still has to change.