0

I have a Function app that inserts some data into a Azure table. Is it possible that I will get a race condition and end up inserting data with the same ID twice, or does Azure make the Insert operations under some table level lock that would ensure I can't insert the same thing twice? The duplicate fields would be the partition key and row key.

In my case, I need to insert if row does not already exists, and read if it exists. In my Function app, I try to read before writing, but it is possible that another instance does a write op with same key in the meantime. If inserts are serialized, I could then fall back to reading when Insert command fails.

Shane
  • 369
  • 1
  • 2
  • 13

1 Answers1

1

Is it possible that I will get a race condition and end up inserting data with the same ID twice, or does Azure make the Insert operations under some table level lock that would ensure I can't insert the same thing twice?

It is certainly possible to get a race condition however having duplicate entries is not possible i.e for one PartitionKey/RowKey combination, there can be only one entity in a table.

In my case, I need to insert if row does not already exists, and read if it exists.

Assuming you're using .Net SDK, one way to achieve this would be to insert the entity using TableOperation.Insert and catch the exception. If the entity already exists, then you will get a Conflict (409) error. When you get this error, you can implement the logic of reading the entity.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • And if it succeeded, then that is the row in the table now :) – juunas Apr 02 '18 at 09:50
  • That was the plan if I could guarantee that each INSERT is a atomic transaction :) – Shane Apr 02 '18 at 16:51
  • Could you tell me if there's any official doc that says the above? Or if you worked on Azure team, would have been helpful haha – Shane Apr 02 '18 at 16:54