# Messaging: Sending Small Messages Through a Queue Instead of Direct Calls

> How separate parts of a system pass small messages through a broker, why teams do it, and what it costs in simplicity and certainty.

**In short:** Messaging is how separate parts of a system pass small messages through a broker or queue instead of calling each other directly.

From "System design", a free course by Venkat R. Murty: https://designervenkat.online/learn/system-design/messaging

## What You Will Learn

Big systems are made of many parts. A shop website, a payment service, a stock service, and an email sender may all run on different machines. Messaging is a way for these parts to talk without calling each other directly. In this lesson you will learn why teams use it, the main parts involved, the delivery promises a queue can make, and the price you pay for it. By the end you should be able to explain when messaging is the right tool and when a plain direct call is better.

- **Message:** A small, self-contained piece of data that one part sends to another. It carries what the receiver needs to act.
- **Producer:** The part that creates a message and sends it. Also called the sender.
- **Broker:** A program in the middle that accepts messages, keeps them safe, and hands them on. It is the go-between.
- **Queue:** A waiting line of messages inside the broker, usually handled in the order they arrived.
- **Consumer:** The part that takes messages from the broker and does the work. Also called the receiver.
- **Direct call:** One part asks another part for something over the network and waits there for the reply.
- **Synchronous:** Happening in step, where the caller waits for the answer before it moves on.
- **Asynchronous:** Happening later, where the sender hands off work and continues without waiting for it to finish.
- **Decouple:** Loosen the ties between parts so one part does not need the other to be online, fast, or even known.

## Why Does Messaging Exist?

Suppose the checkout service calls the email service directly. Checkout is now tied to email. If email is slow, checkout is slow. If email is down, checkout fails. If email changes its address or its code, checkout may need changes too. Messaging loosens these ties. The sender drops a message with a broker and moves on. The receiver picks the message up when it is ready. Engineers call this decoupling, and it is the main reason messaging exists.

- **Decoupled in time:** The two parts do not need to be running at the same moment. The broker keeps the message until a consumer is ready for it.
- **Decoupled in speed:** A fast sender is not held back by a slow receiver. The queue absorbs the gap between how fast work arrives and how fast it gets done.
- **Decoupled in knowledge:** The sender does not need to know who reads its message or how many readers exist. A new consumer can be added without changing the sender.

Decoupling helps in three common situations. First, when a part fails for a short time, messages wait instead of disappearing, so the work can resume later. Second, when traffic jumps, such as during a sale, the queue holds the extra work instead of overwhelming the receiver. Third, each part can scale on its own. Scaling here means adding more machines or workers to handle more load. If email sending is the slow part, you can run more email workers without touching the checkout code.

> **Tip:** Messaging is not free magic. It moves the waiting from the user's screen into the queue. That is usually a good trade, but only if you accept the costs covered later in this lesson.

## What Are the Building Blocks?

Most messaging designs use the same four roles. You will see them in almost every tool, from open-source brokers such as RabbitMQ to cloud queue services.

- **Producer:** The part that creates a message describing a task or a fact, then sends it to the broker. It does not wait for the work to be done.
- **Message:** The package itself. It usually holds an ID, a type name, and the data needed to act. Keep it small, so it is cheap to store and send.
- **Broker and queue:** The broker accepts the message and stores it in a queue until a consumer takes it. The queue is the waiting line inside the broker.
- **Consumer:** The part that takes a message from the queue, does the work, and reports success. Several consumers can share one queue so the work is spread out.

The producer and consumer never need to talk to each other directly. They only talk to the broker. That single rule is what creates the decoupling described above.

### What Are Commands and Events?

Not every message means the same thing. The most useful split is between a command and an event. The difference is in what the sender is saying.

- **Command:** A message that asks for an action, such as charge this card or send this email. It has one intended receiver, and the sender expects that work to happen.
- **Event:** A message that reports something already happened, such as order placed. The sender does not tell anyone what to do. Any part that cares can react. Its name is usually a past-tense phrase.

> **Tip:** A command can be refused or can fail, so the receiver may say no. An event is a record of a fact that already took place. Choosing the right kind keeps your design honest about who owns the decision.

### Acknowledgments and Delivery Promises

A consumer needs a way to tell the broker that it finished. That signal is called an acknowledgment, or ack. When the broker gets the ack, it deletes the message. If the consumer crashes before sending the ack, the broker keeps the message and gives it out again. This safety net is why messages are rarely lost. It also creates the main problem covered in the next part.

- **At most once:** A message arrives zero or one times. It is never repeated, but it can be lost if the consumer fails halfway through the work.
- **At least once:** A message arrives one or more times. Nothing is lost, but the same message can come twice, for example after a crash or a network retry.
- **Exactly once:** The work happens one time only. Some vendors use this label, but in practice it usually means at-least-once delivery plus careful handling inside the consumer.

> **Tip:** Most common brokers deliver at least once by default. Design your consumers for repeats from day one, rather than hoping they never happen.

### Why Must Consumers Handle Duplicates?

Because of at-least-once delivery, a consumer must expect repeats. The standard fix is an idempotent consumer. Idempotent is a word for an action that gives the same end result no matter how many times you run it. A simple way to build one is to store the ID of every message you finish, then skip any message whose ID is already stored. Write that ID in the same database transaction as the work. A transaction is a group of database changes that either all succeed or all fail together, so the record and the work cannot drift apart.

Without this step, a repeated payment message could charge a customer twice. The sample below shows the idea. The broker sends the same payment message twice, but the consumer counts it once.

```python
from collections import deque

queue = deque()
seen_ids = set()
balance = 0

def send(message):
    queue.append(message)

def consume_next():
    global balance
    message = queue.popleft()
    if message['id'] in seen_ids:
        print('skip duplicate', message['id'])
        return
    balance += message['amount']
    seen_ids.add(message['id'])
    print('applied', message['id'], message['amount'])

send({'id': 'pay-1', 'amount': 5})
send({'id': 'pay-1', 'amount': 5})
send({'id': 'pay-2', 'amount': 3})
while queue:
    consume_next()
print('balance', balance)

```

Output:

```text
applied pay-1 5
skip duplicate pay-1
applied pay-2 3
balance 8

```

The second copy of pay-1 is skipped, so the total is 8, not 13. In a real system the seen-IDs would live in a database, not in memory, so they survive a restart.

## Worked Example: Placing an Order

Imagine a simple online store. A customer clicks Place order. The store does not make the customer wait while every task runs. The steps below show one way the flow can work. The services and numbers are a teaching model, not a copy of any real store's design.

1. **Save the essentials now:** The checkout service checks the cart and saves the order record. These are the things the customer needs to know right away. Then the screen shows a confirmation.
2. **Put a message on the queue:** Checkout sends an order-placed event to the broker. The message holds the order ID and the key facts, not the whole web page. Checkout then returns to the browser.
3. **Payment picks it up:** The payment service takes the event when it has room, charges the card, and sends its ack. If payment is slow, the queue simply grows for a while. The customer's screen has already responded.
4. **Stock and email work at their own pace:** The inventory service lowers the stock count, and the email service sends the confirmation. Each has its own queue, so a slow email server does not hold up the stock update.
5. **Skip repeats:** If the email service receives the event twice after a crash, it checks the order ID it has already handled. It skips the second copy, so the customer gets one email.

The example shows the core idea. The user gets a fast answer, the slow work happens later, and each consumer protects itself from repeats. How one event reaches several services is a topic for a later lesson.

## Messaging Versus Direct Calls

A direct call is synchronous: the caller waits for the answer. Messaging is asynchronous: the caller hands off the work and continues. Neither style is always better. Each one trades something away.

Engineers often describe a synchronous call as simple to follow but risky when many calls are chained together. One slow or failed service can stall every caller behind it. Asynchronous messaging removes that chain, but it adds its own costs.

### What Do You Gain and What Do You Pay?

- **Resilience:** A failed receiver does not instantly break the sender. Messages wait in the queue until the receiver recovers.
- **Eventual results:** The work finishes later, so the data may be out of date for a short time. Copies agree after a while, not at once. Engineers call this eventual consistency.
- **Harder debugging:** One request now crosses a broker and several workers. There is no single call stack to read. Teams need logs that carry message IDs, so they can follow one message from start to finish.
- **Message ordering:** Messages do not always arrive in the order they were sent. A duplicate can even arrive after later messages. If order matters for your task, the design must handle it on purpose.

That matches what engineers see in practice.

### When a Direct Call Is the Better Choice

Use a direct call when the caller needs the answer now to decide its next step. For example, a page cannot show a total until a price service returns a number. A quick lookup is another good case. Adding a queue there would add moving parts and delay without real benefit. Many good systems mix both styles: a small direct path for the answer the user needs, and messages for follow-up work that can wait.

> **Tip:** Ask two questions. Does the caller need the result now to continue? Can the work be retried later without a person waiting? If the answer to the first is no and the second is yes, messaging is a strong candidate.

## Check Your Understanding

Try these three short scenarios. Each one needs the ideas above to answer.

**Quick check:** A shop's checkout calls the email service directly. During a sale, the email service slows down, and customers see checkout hang or fail. Which change fixes this, and what must the email side now be ready for?

A. Give checkout a longer timeout; the email side needs no change
B. Checkout puts an order message on a queue; the email consumer must handle duplicate messages
C. Move the email code into checkout; the email side has nothing new to handle
D. Checkout waits for email in parallel; the email side must answer faster

**Answer:** B. A queue removes the wait, so checkout no longer depends on email being fast. Because at-least-once delivery can repeat a message, the email consumer must recognize and skip messages it has already handled.

**Quick check:** A photo app's resize worker finishes a resize job, then crashes before it sends the ack. After a restart, the broker sends the same job again. What is the best design for the worker?

A. Assume the broker never resends jobs, so process every job it receives
B. Record the ID of each finished job and skip any job ID already recorded
C. Send the ack before resizing, so the broker never has a reason to resend

**Answer:** B. Repeats are normal under at-least-once delivery, so the worker must be idempotent. Storing finished IDs lets it skip the repeat. Acking before the work is done risks losing the job if the work then fails.

**Quick check:** A web page needs a customer's current account balance before it can show anything useful. Which design fits best?

A. Send a balance request to a queue and show the page when a reply arrives later
B. Make a direct call to the account service and wait for the balance
C. Send a message to a queue and show a blank balance until a worker fills it in

**Answer:** B. The page needs the answer now to decide what to show, so the caller must wait. A direct call is simpler here, and a queue would add delay and moving parts without a benefit.

## Key Takeaways

Messaging lets a producer hand work to a broker and move on, while a consumer handles that work later. It decouples the parts in time, speed, and knowledge, which helps with failures, traffic spikes, and independent scaling. The price is delayed results, harder debugging, and the need for ordering and duplicate handling. Use it for work that can wait, and keep a direct call for answers the user needs right now.

## Sources

- [Azure Architecture Center: Idempotent Consumer pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/idempotent-consumer)
- [microservices.io: Handling duplicate messages using the Idempotent consumer pattern](https://microservices.io/post/microservices/patterns/2020/10/16/idempotent-consumer.html)
- [CloudAMQP: RabbitMQ for beginners, what is RabbitMQ?](https://www.cloudamqp.com/blog/part1-rabbitmq-for-beginners-what-is-rabbitmq/)
- [Racklify: Message Queue, a beginner's guide](https://racklify.com/encyclopedia/message-queue-a-beginners-guide/)
- [Message Queues Explained: Producers, Consumers, and Brokers (sysdespro)](https://sysdespro.com/oleksandr-andrushchenko/message-queues-explained-producers-consumers-and-brokers)
- [How Message Queues Actually Work (Level Up Coding)](https://blog.levelupcoding.com/p/how-message-queues-actually-work)
- [Message Queues: Deep Dive (Hookdeck)](https://hookdeck.com/blog/message-queues-deep-dive)
- [Idempotency and job queues: at-least-once delivery, retries and exactly-once effects (DEV Community)](https://dev.to/byd_tech/idempotency-and-job-queues-at-least-once-delivery-retries-and-exactly-once-effects-2lg8)
- [Idempotent Consumer Pattern: Handling Duplicate Messages in Microservices (The DailyByte)](https://thedailybyte.substack.com/p/idempotent-consumer-pattern-handling)
- [Exactly-Once vs At-Least-Once Delivery, Explained (CODERCOPS)](https://blog.codercops.com/blog/exactly-once-vs-at-least-once-delivery-guide)
- [Sync Vs Async Communication (AlgoMaster, system design)](https://algomaster.io/learn/system-design/sync-vs-async-communication)
- [Synchronous vs Asynchronous Communication (AlgoMaster, microservices)](https://algomaster.io/learn/microservices/sync-vs-async-communication)
- [Synchronous vs Asynchronous Communication: Trade-offs and Examples (adevguide)](https://adevguide.com/backend/messaging/synchronous-vs-asynchronous/)
- [Synchronous vs asynchronous communications: A complete guide (TechTarget)](https://www.techtarget.com/it-infrastructure/tip/Synchronous-vs-asynchronous-communications-A-complete-guide)
- [Decision Models for Selecting Patterns and Strategies in Microservices Systems (arXiv)](https://arxiv.org/pdf/2201.05825)
