Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Messaging Concepts and Patterns

Understand the core ideas behind message brokers: synchronous vs asynchronous calls, point-to-point vs publish/subscribe, delivery guarantees and idempotency.

Before you touch RabbitMQ commands, it helps to understand the ideas every messaging system shares. They explain why brokers are designed the way they are, and they protect you from the most common mistakes: lost work, duplicate work and tight coupling.

Synchronous and asynchronous communication

In a synchronous call (an HTTP request, a function call) the caller waits for the answer. If the callee is slow or down, the caller is slow or fails too. In an asynchronous call the caller sends a message and continues; the answer, if any, arrives later or not at all.

sequenceDiagram
    participant W as Web app
    participant B as Broker
    participant K as Worker
    W->>B: publish "send-email"
    B-->>W: accepted
    Note over W: web app continues at once
    B->>K: deliver "send-email"
    K->>K: do the slow work

The web app only waits for the broker to accept the message, not for the email to be sent.

Asynchronous messaging is not always better. You gain resilience and throughput; you lose the simplicity of "I called it and I know the result". Use messages for work that can happen later and for events that several parties care about.

Point-to-point and publish/subscribe

There are two basic delivery shapes:

  • Point-to-point (work queue): many producers may send, many consumers may listen, but each message is processed by one consumer. Use it to share jobs.
  • Publish/subscribe: a message is delivered to every interested subscriber. Use it to announce events such as user.registered.

In RabbitMQ both are built from the same parts. A single queue with several consumers gives you point-to-point. Several queues bound to one exchange give you publish/subscribe, because each queue gets its own copy of the message.

Python Two delivery shapes (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Messages, commands and events

Not every message means the same thing:

  • A command asks someone to do something: send-invoice. It has one intended handler.
  • An event states that something already happened: invoice.sent. Zero, one or many parties may react.
  • A document carries data, such as a price list update.

Naming matters. Commands are imperative (resize-image), events are past tense (order.placed). Mixing them up leads to services that quietly depend on each other.

Delivery guarantees

A network can fail between any two steps, so every messaging system has to choose what happens when it does. Three words describe the options:

  • At most once: a message is delivered zero or one time. It can be lost, never duplicated. Fast, but risky.
  • At least once: a message is delivered one or more times. It is never lost, but may be duplicated, for example when a consumer processes a job and crashes before telling the broker.
  • Exactly once: the message has its effect once. True exactly-once delivery across a network is not achievable in general; systems approximate it by combining at-least-once delivery with idempotent processing.

RabbitMQ gives you at-least-once delivery when you use publisher confirms, persistent messages and consumer acknowledgements (all covered in the reliability chapter). That means your consumers must be ready for duplicates.

Idempotency: handling duplicates

An operation is idempotent if doing it twice has the same result as doing it once. Setting a status to shipped is idempotent. Adding 10 to a balance is not.

The usual trick is to give every message a unique id and remember which ids you have already handled. The stand-in below simulates a broker that redelivers a message after a "crash" and a consumer that ignores duplicates.

Python At-least-once plus an idempotent consumer (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try removing the processed_ids check and see how the balance drifts. That is exactly the bug at-least-once delivery will expose in production.

Ordering

Messages in a single RabbitMQ queue are delivered in the order they were published, as long as there is one publisher channel and one consumer and nothing is redelivered. With several consumers working in parallel, or when a message is rejected and requeued, order across the whole queue is no longer guaranteed. If strict order matters, either use one consumer, or put related messages in the same queue and process them one at a time.

Backpressure and buffering

If producers are faster than consumers, a queue grows. That is fine for a burst, but a queue that grows forever means you are out of capacity. Brokers give you tools: limits on queue length, message TTLs, prefetch counts, and flow control that slows publishers when the broker is overloaded. You meet these tools in later lessons; for now remember that a queue is a buffer, not a bottomless storage.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which delivery shape sends each message to every interested subscriber?

    In publish/subscribe each subscriber gets its own copy. Point-to-point gives each message to one consumer only.
  2. 2. What can happen with at-least-once delivery?

    At-least-once means nothing is lost but duplicates are possible, for example after a lost acknowledgement. Consumers should be idempotent.
  3. 3. Which operation is idempotent?

    Setting a status to a fixed value gives the same result no matter how often you repeat it. The others change the result each time.
  4. 4. Which name looks like an event rather than a command?

    Events describe something that has already happened, usually in past tense, such as invoice.sent.

Summary

  • Asynchronous messaging trades simplicity for resilience and throughput.
  • A work queue gives each message to one consumer; publish/subscribe gives a copy to every subscriber queue.
  • Commands ask for work, events announce facts.
  • At-least-once delivery means duplicates can happen, so make consumers idempotent.
  • Ordering is only guaranteed in simple one-publisher, one-consumer cases.

Next: Installing and Running RabbitMQ