RabbitMQ gives applications a reliable way to move work between producers and consumers. A producer can publish a message, a consumer can process it, and the consumer can acknowledge the message after the work is complete.
The difficult part is what happens when processing succeeds but the acknowledgement does not reach RabbitMQ.
RabbitMQ can then deliver the same message again.
This means a consumer should not assume that receiving a message once means it will only ever process that message once. In a reliable messaging system, duplicate delivery is something the application needs to handle.
This article explains why RabbitMQ messages can be processed more than once, how acknowledgements affect delivery, and how to design C# consumers so duplicate messages do not create duplicate business operations.
Why a RabbitMQ Message Can Be Delivered Twice
A common RabbitMQ consumer flow looks like this:
Producer
|
v
RabbitMQ Queue
|
v
Consumer
|
v
Process Message
|
v
Acknowledge MessageThe consumer usually acknowledges the message after successful processing.
The problem occurs when the application finishes its work but fails before the acknowledgement is completed.
For example:
1. RabbitMQ delivers message
2. Consumer receives message
3. Consumer writes to database
4. Database operation succeeds
5. Consumer crashes
6. RabbitMQ never receives the acknowledgement
7. RabbitMQ delivers the message againThe database operation already happened.
The second delivery does not know that.
First delivery -> Database INSERT succeeds
Second delivery -> Database INSERT happens againThis is one of the most important concepts when building RabbitMQ consumers.
Message acknowledgement and business-operation completion are two different things.
Understanding RabbitMQ Acknowledgements
RabbitMQ supports acknowledgements that tell the broker whether the consumer successfully handled a message.
A simplified flow looks like this:
RabbitMQ
|
| Message
v
Consumer
|
| Process
v
Business Operation
|
| Success
v
ACK
|
v
RabbitMQ removes messageIf the consumer does not acknowledge the message, RabbitMQ can keep it available for redelivery.
This is intentional.
The broker cannot safely assume that an unacknowledged message was processed successfully.
That behavior protects messages from being lost when consumers crash.
The trade-off is that reliable delivery can result in duplicate processing.
Automatic Acknowledgement Is Usually the Wrong Choice for Business Operations
RabbitMQ clients can acknowledge messages automatically.
That can simplify the consumer, but it creates a serious reliability problem.
If the broker considers the message handled before the application finishes its business operation, a process failure can result in lost work.
For important operations, manual acknowledgement is generally easier to reason about.
A typical C# consumer can use manual acknowledgement:
var consumer = new AsyncEventingBasicConsumer(channel);
consumer.ReceivedAsync += async (_, args) =>
{
try
{
var message = Encoding.UTF8.GetString(args.Body.ToArray());
await ProcessMessageAsync(message);
await channel.BasicAckAsync(
args.DeliveryTag,
multiple: false);
}
catch (Exception ex)
{
await channel.BasicNackAsync(
args.DeliveryTag,
multiple: false,
requeue: true);
}
};
await channel.BasicConsumeAsync(
queue: "orders",
autoAck: false,
consumer: consumer);The important part is the order:
Process message
|
v
Successful business operation
|
v
ACKThe acknowledgement should not happen before the operation that the acknowledgement represents.
The Real Problem Is Not RabbitMQ
It is tempting to think:
RabbitMQ delivered the same message twice, so RabbitMQ caused the duplicate.
That is not the right way to look at it.
RabbitMQ is protecting the message.
From the broker's perspective:
Message delivered
ACK received? No
Consumer state uncertain
Redeliver messageThe broker does not know whether the consumer:
- never received the message
- started processing it
- completed the operation
- crashed after completion
- timed out
- lost its network connection
- failed before acknowledgement
The consumer needs to make duplicate processing safe.
This leads to the concept of idempotency.
What Is Idempotent Message Processing?
An operation is idempotent when executing it multiple times produces the same intended business result as executing it once.
For example, this is dangerous:
await db.Orders.AddAsync(order);
await db.SaveChangesAsync();If the same message is delivered twice and both executions create a new order, the application may create two records.
A better design gives every message a stable identifier.
For example:
{
"messageId": "8f7b9d22-6c9a-4e31-a8c2-7f5d1c4e21ab",
"orderId": 1501,
"amount": 2500
}The consumer can then record which message IDs have already been processed.
Use a Message ID for Deduplication
A common approach is to maintain a processed-message table.
For example:
ProcessedMessages
----------------------------
MessageId
ProcessedAtBefore performing the business operation:
Check MessageId
|
+---- Exists -> Skip
|
+---- Missing
|
v
Perform operation
|
v
Record MessageIdThe important detail is that the check and business operation need to be designed carefully.
This implementation is unsafe:
if (!await ExistsAsync(messageId))
{
await ProcessOrderAsync(message);
await MarkProcessedAsync(messageId);
}Two consumers can execute this simultaneously:
Consumer A -> Exists? No
Consumer B -> Exists? No
Consumer A -> Process
Consumer B -> ProcessBoth consumers passed the check.
The deduplication operation needs a database constraint or another atomic mechanism.
Add a Unique Constraint
A database unique constraint is a reliable way to prevent two consumers from recording the same message ID.
For example:
CREATE UNIQUE INDEX IX_ProcessedMessages_MessageId
ON ProcessedMessages(MessageId);The application can then attempt to insert the message ID.
If another consumer already inserted it, the database rejects the duplicate.
This provides a much stronger guarantee than performing a separate read followed by a write.
Combine Business Data and Deduplication in One Transaction
This is one of the most important patterns for reliable consumers.
Suppose the message represents an order payment.
The consumer needs to:
- Create or update the payment.
- Record that the message was processed.
- Commit both operations.
These operations should happen inside the same database transaction.
await using var transaction =
await db.Database.BeginTransactionAsync(cancellationToken);
try
{
var alreadyProcessed =
await db.ProcessedMessages
.AnyAsync(
x => x.MessageId == messageId,
cancellationToken);
if (alreadyProcessed)
{
await transaction.CommitAsync(cancellationToken);
await channel.BasicAckAsync(
deliveryTag,
multiple: false);
return;
}
db.Payments.Add(payment);
db.ProcessedMessages.Add(
new ProcessedMessage
{
MessageId = messageId,
ProcessedAt = DateTimeOffset.UtcNow
});
await db.SaveChangesAsync(cancellationToken);
await transaction.CommitAsync(cancellationToken);
await channel.BasicAckAsync(
deliveryTag,
multiple: false);
}
catch
{
await transaction.RollbackAsync(cancellationToken);
throw;
}Now the database has one atomic unit of work.
Either both records are committed or neither is.
The message should only be acknowledged after the transaction succeeds.
Why the Order Matters
Consider this sequence:
Database transaction succeeds
|
v
Application crashes
|
v
ACK never reaches RabbitMQ
|
v
Message is delivered againThe second consumer sees the message ID in the database.
It knows the business operation already succeeded.
It can safely acknowledge the duplicate without performing the operation again.
Second delivery
|
v
Message ID already exists
|
v
Skip business operation
|
v
ACKThis is the basic pattern that turns at-least-once delivery into effectively-once business behavior.
The messaging infrastructure still delivers at least once. The application makes the business operation idempotent.
Do Not Rely Only on the RabbitMQ Delivery Tag
RabbitMQ provides a delivery tag for identifying deliveries on a channel.
It is useful for acknowledgements, but it should not normally be treated as a permanent business-level message ID.
Your application needs an identifier that travels with the message.
For example:
{
"messageId": "c2e4d4f1-3f7e-45b6-bc1d-9b0e6eab18a4",
"eventType": "OrderPaid",
"orderId": 1501
}The message ID should remain stable when RabbitMQ redelivers the message.
That is what allows the consumer to recognize a duplicate.
Join the conversation! Your thoughts help the community grow.