Skip to content
Theory · Delivery guarantees · lesson 5 of 9

Duplicates and the idempotent consumer

Acknowledging after the work saves a message from being lost, but it brings repeats in return. Under at-least-once every consumer will sooner or later receive the same message twice. So the consumer has to know in advance what to do with a repeat.

The courier brought the parcel a second time: last time the signature went missing. An attentive customer looks at the order number on the label, sees that this order has already arrived and hands the parcel back unopened. The shop wrote the order number, not the courier, so it is the same on both parcels.

Where duplicates come from

As a reminder, a duplicate is a copy of a message that has already been delivered. Duplicates have several causes:

  • the consumer did the work and crashed before the acknowledgement — you saw this case in the previous lesson;
  • the consumer sent the acknowledgement, but the connection dropped before the broker received it;
  • in Kafka the consumer group redistributed the work among its members (this is called a rebalance), and the new consumer reread messages the old one had already processed;
  • the producer did not get a confirmation from the broker and sent the message again, although the first one was already stored.

None of these causes can be removed entirely: networks drop and programs crash. So duplicates will come for sure.

The redelivered flag says "maybe"

In the previous lesson RabbitMQ marked the returned messages with the redelivered flag. It is tempting to filter duplicates by it, but the flag has two problems.

First: the flag is set on any message the broker has already handed out without an acknowledgement, even if the consumer never processed it. Look at m3 once more:

TEXT
$ go run .
got m1 redelivered=false
got m2 redelivered=false
crash before ack
exit status 1
$ go run . drain
got m2 redelivered=true, ack
got m3 redelivered=true, ack

The first consumer printed only m1 and m2, but m3 also arrived with redelivered=true on the next run. The broker had handed it out in advance, and the program crashed before getting to it.

Second: the flag is not set when the producer sent the message twice. To the broker these are two different messages, and it hands out each of them for the first time.

So redelivered=true means "maybe this is a repeat", and redelivered=false does not promise that there is no repeat.

The idempotent consumer

An idempotent consumer is one for which processing a message again gives the same result as the first time. Duplicates do not hurt it.

The action is idempotent by itself

"Set the status of order 42 to 'paid'": however many times you run it, there is one status. Such a message can be processed again without checks.

The action is not idempotent

"Credit 100 rubles to account 7": each run adds another 100 rubles. Here a repeat means a mistake with the customer's money, so the consumer needs deduplication.

Deduplication by message id

Deduplication is a technique in which the consumer remembers the ids of processed messages and skips a message if it has already seen that id:

  1. The consumer receives the message and takes its id.
  2. If this id has already been processed, it acknowledges the message and does nothing else.
  3. Otherwise it performs the action.
  4. It remembers the id.
  5. It acknowledges the message.

Where the id comes from

The id must be the same on every copy of one message, so the producer sets it:

  • the MessageId message property in AMQP, the RabbitMQ protocol; in amqp091-go this is the field d.MessageId;
  • a business key: the payment number or the order number.

Numbers the broker assigns do not work:

  • d.DeliveryTag in RabbitMQ is the number the broker assigned to this delivery, and a repeated delivery gets a different one;
  • an offset in Kafka is the number of a record in the log, and if the producer sent a message twice, the copies have different offsets.

Common mistake

Remembering the id before the action:

GO
func handle(d amqp.Delivery, seen map[string]bool) error {
	if seen[d.MessageId] {
		return d.Ack(false)
	}
	seen[d.MessageId] = true // ✗
	if err := apply(d); err != nil {
		return err
	}
	return d.Ack(false)
}

If apply returns an error, for example because the database is down, the id is already stored. The broker hands the message out again, but the consumer decides it has already processed it and skips it: the money is never credited. Remember the id only after the action has succeeded; then the message the consumer failed on is processed again on the repeat.

seen is a map in memory, so everything it remembers disappears with the process. A real service keeps ids in the database.

Key points

  • Under at-least-once duplicates will come for sure: after a crash, a lost acknowledgement, a rebalance or a resend by the producer.
  • The redelivered flag only means "maybe a repeat" and does not catch repeats from the producer.
  • An idempotent consumer gives one result for any number of deliveries; a non-idempotent action is protected by deduplication.
  • The producer sets the id, and the consumer remembers it only after the action has succeeded.