Benefits and What We Left Out

Here are the benefits of a message queue, with the examples we saw in Shopend:

  • Asynchronous processing: the producer does not wait for the consumer. It responds as soon as the broker has the message. The commerce service responded “processing” without waiting for order 93 to be placed.
  • Load leveling: a burst waits in the queue, and the consumer takes the messages at the pace it can handle. During the flash sale, the orders waited in the order queue, and the order worker placed them at the pace the primary allowed.
  • Decoupling: the producer knows only the queue, not which consumer handles the message or where it runs. The producer and the consumer can each change and be deployed on their own. The order worker puts a message on the email queue and does not know how the notification service sends the email.
  • Resilience: the producer keeps working when a consumer is slow or down. The consumer’s messages wait until it is back. When the notification service is down, the emails wait in the email queue, and orders are still placed.
  • Durability: the broker saves each message to disk and copies it to a second server. So a message is not lost when one server fails. That is what we required of Shopend’s broker.
  • Scalability: when the queue grows, we can run more consumers.

In this chapter, we used the simplest setup: one consumer for each queue, and nothing failing. We left out these topics:

  • Acknowledgment: when the broker may remove a message, and what happens if a consumer crashes before it is done.
  • Delivery guarantees: at most once, at least once, and why “exactly once” is hard.
  • Idempotent consumers: handling the same message delivered twice.
  • Dead-letter queues: what to do with a message that fails every time.
  • Competing consumers: several consumers on one queue, and what happens to message order.
  • Publish/subscribe: one message delivered to several consumers, with topics and filtering.
  • Transactional outbox: saving data and sending the message as one step.

We will deal with these in a later chapter.