Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Atomic operations in azure table storage

I am looking to implement a page view counter in azure table storage. If say two users visit the page at the same time, and the current value on PageViews = 100, is it guaranteed that the PageViews = 102 after the update operation?

like image 946
States Avatar asked Aug 07 '12 19:08

States


People also ask

Which elements compose a key in Azure Table storage?

What are the elements of an Azure Table storage key? Table name and column name Partition key and row key O Row number 2. When should you use a block blob, and when should you use a page blob? Use a block blob for unstructured data that requires random access to perform reads and writes.

What type of storage is Azure Table storage?

Azure Table storage is a cloud-based NoSQL datastore you can use to store large amounts of structured, non-relational data. Azure Table offers a schemaless design, which enables you to store a collection of entities in one table. An entity contains a set of properties, and each property defines a name-value pair.


1 Answers

The answer depends on how you implement your counter. :-)

Table storage doesn't have an "increment" operator, so you'd need to read the current value (100) and update it to the new value (101). Table storage employs optimistic concurrency, so if you do what comes naturally when using the .NET storage client library, you'd likely see an exception when two processes tried to do this simultaneously. This would be the flow:

  1. Process A reads the value of PageViews and receives 100.
  2. Process B reads the value of PageViews and receives 100.
  3. Process A makes a conditional update to PageViews that means "set PageViews to 101 as long as it's currently 100." This succeeds.
  4. Process B performs the same operations and fails, because the precondition (PageViews == 100) is false.

The obvious thing to do when you receive the error is to repeat the process. (Read the current value, which is now 101, and update to 102.) This will always (eventually) result in your counter having the correct value.

There are other possibilities, and we did an entire Cloud Cover episode about how to implement a truly scalable counter: http://channel9.msdn.com/Shows/Cloud+Cover/Cloud-Cover-Episode-43-Scalable-Counters-with-Windows-Azure.

What's described in that video is probably overkill if collisions are unlikely. I.e., if your hit rate is one-per-second, the normal "read, increment, write" pattern will be safe and efficient. If, on the other hand, you receive 1000 hits per second, you'll want to do something smarter.

EDIT

Just wanted to clarify for people who read this to understand optimistic concurrency... the conditional operation isn't really "set PageViews to 101 as long as it's currently 100." It's more like "set PageViews to 101 as long as it hasn't changed since the last time I looked at it." (This is accomplished by using the ETag that came back in the HTTP request.)

like image 96
user94559 Avatar answered Sep 22 '22 01:09

user94559