Calls and messages
What you'll learn:
- explain why services need a message broker and what you pay for it;
- choose a delivery guarantee and write a Go consumer that repeats cannot hurt;
- explain how Kafka, RabbitMQ and NATS work and what each of them promises on a failure;
- apply the patterns around a broker and pick a broker for the task, the way interviews ask about them.

You need to give a friend a book. You can call them and agree to meet: until they pick up, you wait, and if they are on vacation, the book stays with you. Or you can hand the book to a courier service and get on with your day. The service keeps the parcel at its warehouse until your friend is home and delivers it when it suits them.
Services talk to each other in the same two ways. The phone call is a synchronous call, and the parcel sent through a courier service is a message sent through a broker.
A synchronous call
An online shop places an order. The order service saves the order to the database and then calls the email service over HTTP: "send the customer a confirmation". This is how a synchronous call works:
- the sender waits for the reply and cannot finish its own work until it arrives;
- the time the customer waits adds up from the time of every call in the chain;
- if the receiver is down or hangs, the call ends in an error or a time-out, and the sender has to decide what to do about it.
While the email service is fast, all is well. But let the email service stall for five seconds, and the customer stares at a spinning wheel for five seconds. And if the email service is down, the order is not placed at all, although the customer could have lived without the email.
A message through a broker
Now the order service does not call the email service. It hands the broker the message "order 42 created" and answers the customer at once. The email service picks the message up from the broker and sends the email at its own pace.
A producer is a program that sends messages. A consumer is a program that reads and processes messages. A broker is a server that accepts messages from producers, stores them and passes them on to consumers.
In our example the order service is the producer, the email service is the consumer, and the broker is the courier service with its warehouse.
What changed:
- The producer waits only for the broker to reply that the message is accepted, and that is fast. It does not wait for the processing.
- If the consumer is down, the message stays with the broker. The consumer reads it when it starts again.
- The producer and the consumer do not call each other directly and do not have to run at the same time.
Such processing is called asynchronous: it runs apart from the producer's request and usually later.
The analogy breaks down in one place: a parcel exists as a single copy, while a message is data. The broker can hand a copy of one message to several consumers at once.
A call or a message
A message does not replace a call everywhere. The choice depends on whether the sender needs an answer right now:
| Task | Way | Why |
|---|---|---|
| Check a password at sign-in | call | without the answer the user cannot be let in |
| Get the stock level before an order | call | the answer is needed to show the "Buy" button |
| Send the order email | message | the customer does not need to wait for the email to see "order placed" |
| Update analytics and recommendations | message | the work can wait a minute, and its failure must not break the order |
Interviewers like to ask this: "Why move slow side work out of the synchronous response?" The answer has two parts:
- the response time stops depending on the side work: the handler does only what the response needs and hands the rest to the broker as a task message;
- a failure of the side service no longer breaks the order: the order is already placed, and a consumer retries once the service is back.
Consumers that take such background tasks from the broker and run them are called workers.
Common mistake
Calling the side work right inside the request handler:
func createOrder(w http.ResponseWriter, r *http.Request) {
id, err := saveOrder(r)
if err != nil {
http.Error(w, "cannot save order", http.StatusInternalServerError)
return
}
if err := sendConfirmation(id); err != nil { // ✗
http.Error(w, "cannot send email", http.StatusBadGateway)
return
}
fmt.Fprintf(w, "order %d created\n", id)
}The order is already saved, but the email service did not answer, and the customer sees an error. They click "Buy" again, and the database gets a second order. And while the email service answers slowly, the shop itself answers slowly too.
The fix: after saveOrder the handler hands the broker the message "order created" and answers the customer at once. The email service sends the email when it reads the message.
Key points
- In a synchronous call the sender waits for the reply, so a slow or stopped receiver becomes its problem too.
- The producer hands a message to the broker and moves on, and the broker stores the message until a consumer reads it.
- The producer and the consumer do not call each other and do not have to run at the same time.
- Slow side work moves into messages: the handler no longer waits for it or depends on its failures.