Coyote
Coyote

Reputation: 196

Azure service bus message lock and message completion

I'm using Azure service bus queue and I have a worker which takes a message and processes it for ~15 minutes whereas the maximum queue message lock is for 5 minutes. I have noticed in the logs that one process is taking this message and start processing it and after like 7 minutes a second process is taking the same message (the message lock duration time is over) and start processing it too. While the 2nd process is working the 1st process finishes and returns true to the service bus, and then the 2nd process logs are gone, like it's disappearing. when processing a message service bus is picking the message and when it's over it's popping the message from the queue, I'm thinking that maybe this is the reason for this weird behavior but I couldn't find any information about it in the documentation. can anyone clarify this point? what happen if message processing is done and popped from the queue while the same message is being processed by another processor?

BTW I know that there is an option to renew the lock but I couldn't find a good way to do it on nodejs.

Upvotes: 2

Views: 1748

Answers (1)

Sean Feldman
Sean Feldman

Reputation: 25994

What you're describing is normal, known as competing consumers. One or more processes get messages from the Service Bus. If a message is received by one of the consumers, it's locked (leased) solely to that consumer until the lock time is up. That time is a maximum of 5 minutes today. Once the time is up, the message is unlocked and available to other consumers. The issue is that when a consumer gets a message, they get a copy of that message. When the lock expires, the consumer still holds on to the copy of the message as there's nothing to revoke it. At least the broker cannot do that. This leads to this behaviour, where the consumer can continue working, but another consumer (one or more) would get hold of the message and run processing in parallel. Eventually, the first consumer will attempt to complete the message, failing with the MessageLockLostException. And that's where extending the lock can help, as the consumer receiving the message can signal to the broker that it's not yet done with the message and needs to keep it to itself.

I'm not well versed in all the SDKs Azure supports but the latest Node.js SDK should have the ability to extend the lock. You could probably ask for a documentation link or clarification here.

Upvotes: 1

Related Questions