tags:

views:

168

answers:

3

Let say I have Table in Azure Table Storage

public class MyTable
{
  public string PK {get; set;}
  public string RowPK {get; set;}

  public double Amount {get; set;}
}

And message in Azure Queue which says Add 10 to Amount.

Now let say one worker role

  1. Takes this message from queue
  2. Takes row from table
  3. Amount += 10
  4. Updates Row in Table
  5. And Fails

After a while message is available in queue again. So next worker role:

  1. Takes this message from queue
  2. Takes row from table
  3. Amount += 10
  4. Updates Row in Table
  5. Removes message from queue

Those actions results in Amount += 20 instead of Amount += 10.

How can I avoid such situations?

+1  A: 

All the messages that you put on the queue must be idempotent. There is always a chance a worker role won't finish his job so the message must be repeatable.

So instead of amount += 10 as a task do something like amount = 300. Get the current amount in the webrole add 10 to it and place the new amount on the queue.

I'm not sure if this is the correct way. If you do it like this there will be a problem if two webroles try to add 10 at the same moment.

Korenaga
A: 

Hello,

I would suggest that you implement a sort of optimistic concurrency. The message you send to update the row should contain both the "previous value" and the "new value" of the amount property.

So the second worker role that tries to update the row will first check that the current value is still equal to "previous value". If not the worker role knows something went wrong and he can for example just cancel the message without doing the update. And perhaps also raise an error in some log.

FrenchData
A: 

did you implemented this or are the lines of code up there just a few thoughts?

"amount" implies that you are thinking of some kind of bank transaction scenario. It would be probably better to work directly with SQL Azure (since you have ACID guarantees: http://blogs.msdn.com/ssds/archive/2009/03/12/9471765.aspx „We have always supported full ACID capabilities in the service and will continue to do so.”)

Afaik, we can say that "tables" in windows azure are something like googles bigtable, aren't they?

dayscott