سؤال

We have a Queue and multiple instances of same worker role reading the messages from the queue. The processing of each message is very fast. How do we avoid a case where two instances try to read same message from the queue at exact same point of time.

هل كانت مفيدة؟

المحلول

You are not going to have any problems with multiple clients accessing an Azure Storage Queue simultaneously, provided you follow a Read-and-Delete pattern:

The message is not automatically deleted from the queue, but after it has been retrieved, it is not visible to other clients for the time interval specified by the visibilitytimeout parameter.

Source: http://msdn.microsoft.com/en-us/library/windowsazure/dd179474.aspx

The nature of queues requires that every operation on the head of the queue must be atomic. In the case of queues as services, if it were possible for multiple clients to perform concurrent operations which caused overlapped reads, the queue would be utterly unusable.

نصائح أخرى

Windows Azure Queues support 2 mechanisms for reading messages from a queue - Get and Peek. To avoid race condition, recommendation would be to read the messages using Get mechanism as when you read the message using Get, the message becomes invisible to other callers (worker role instances in your case) for some amount of time thus only one instance will be able to read and process the message.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top