天蓝色表存储中的原子操作 [英] Atomic operations in azure table storage

查看:66
本文介绍了天蓝色表存储中的原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在azure表存储中实现页面视图计数器的方法.如果说两个用户同时访问该页面,并且PageViews的当前值= 100,是否可以保证更新操作后PageViews = 102?

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?

推荐答案

答案取决于您如何实现计数器. :-)

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

表存储没有递增"运算符,因此您需要读取当前值(100)并将其更新为新值(101).表存储采用开放式并发,因此,如果您使用.NET存储客户端库自然而然地进行了操作,则当两个进程尝试同时执行此操作时,您很可能会看到异常.这就是流程:

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. 进程A读取PageViews的值并接收100.
  2. 进程B读取PageViews的值并收到100.
  3. 进程A对PageViews进行有条件的更新,这意味着将PageViews设置为101,只要当前为100."这样成功.
  4. 由于先决条件(PageViews == 100)为false,因此进程B执行相同的操作并失败.

收到错误时,显而易见的事情是重复该过程. (读取当前值,现在为101,并更新为102.)这将始终(最终)使您的计数器具有正确的值.

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.

还有其他可能性,我们就如何实现真正的可扩展计数器做了整个Cloud Cover专题:

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.

如果不太可能发生碰撞,该视频中描述的内容可能会过大.也就是说,如果您的点击率是每秒一秒,则正常的读取,递增,写入"模式将是安全有效的.另一方面,如果您每秒收到1000次点击,则您想做点更聪明的事情.

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.

编辑

只是想澄清一下阅读此书的人,以了解开放式并发……条件操作实际上并不是将PageViews设置为101,只要当前是100."它更像是将PageViews设置为101,只要它自从我上次查看以来就没有改变过". (这是通过使用HTTP请求中返回的ETag来完成的.)

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.)

这篇关于天蓝色表存储中的原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆