DEV Community

Vinayak Savale
Vinayak Savale

Posted on

Day 5 – Ensuring Message Idempotency with Duplicate Detection in Azure Service Bus

In distributed systems, duplicate messages are inevitable. Network retries, client resends, or failover scenarios can cause the same message to be delivered multiple times.

👉 But in business workflows like payment processing or order creation, duplicates can be disastrous.

This is where Duplicate Detection in Azure Service Bus comes into play.

🔹 What is Duplicate Detection?
A built-in idempotency mechanism in Service Bus.

Detects and discards messages with the same MessageId if received within a configured time window.

Saves developers from building complex deduplication logic at the application level.

🔹 How It Works
Enable Duplicate Detection when creating the Queue/Topic.

Set a Duplicate Detection History Time Window (from 20 seconds up to 7 days).

Service Bus maintains a record of MessageIds within that window.

If a new message with the same MessageId arrives → it is ignored.

✅ Example Use Case
💳 Payments System

User clicks “Pay” twice by mistake.

Both requests send a message with the same TransactionId.

Duplicate Detection ensures only one message is processed, preventing double charges.

🔹 Enabling Duplicate Detection in .NET

Image

🔹 Key Benefits
✔ Prevents double processing of messages.

✔ Reduces risk of financial or logical inconsistencies.

✔ Simple configuration → no need for custom deduplication storage.

🔹 Best Practices
Always set a MessageId when sending messages.

Choose the right time window based on business needs.

For critical systems → combine with idempotent application logic for extra safety.

🔹 Wrapping Up
Duplicate Detection is one of the simplest yet most powerful features in Azure Service Bus. It protects your system against message duplication, ensuring data integrity and reliable workflows.

In Day 6, we’ll explore Message Ordering in Azure Service Bus — how to make sure messages are processed in the sequence they were sent.

💬 Have you faced issues with duplicate events in your applications? How did you solve them?

Top comments (0)