Search code examples
c#azureazureservicebusservicebus

Service bus topic inserted duplicate message record for same MessageId


[FunctionName("MyProcess")]
public static void Run(BrokeredMessage currentMessage,[ServiceBus("mytopic",
    Connection = "ConnServiceBus")]ICollector<BrokeredMessage> mytopicDemo, TraceWriter log)
{
    string messageBody = currentMessage.GetBody<string>();
    BrokeredMessage brokeredMessage = new BrokeredMessage(result);
    //extracted Id and Name from body using some code for simplicity making it as messageBody.Id and messageBody.Name
    brokeredMessage.Properties.Add("Id", messageBody.Id);    
    brokeredMessage.Properties.Add("Name", messageBody.Name);
    brokeredMessage.MessageId = "ZZ"; // I also tried GUID here messageBody.Id
    mytopicDemo.Add(brokeredMessage);

    ...                     

service bus unable to identify duplicate messageId message.

It isinserting multiple records for same messageId why?

It should avoid toinsert duplicate record if it is already present.enter image description here


Solution

  • The probable causes might be

    • If an application encounters a fatal error immediately after it sends a message, and the restarted application instance erroneously believes that the prior message delivery did not occur, a subsequent send causes the same message to appear in the system twice.

    • It is also possible for an error at the client or network level to
      occur a moment earlier, and for a sent message to be committed into
      the queue, with the acknowledgment not successfully returned to the
      client. This scenario leaves the client in doubt about the outcome of the send operation.

    Enabling duplicate detection solves this problem by discarding the duplicate messages.

    The duplicate detection time history defaults to 30 seconds for queues and topics, with a maximum value of 7 days.

    The duplicate detection time history must be large to detect as much as duplicate messages .Also note that the size of the window directly impact the queue (and topic) throughput.

    Click here for more information.