Memcached – Are GET and SET operations atomic?

自作多情 提交于 2019-12-23 10:12:49

问题


Here is the scenario: a simple website which queries a memcached cache. That same cache is updated by a batch job every 10-15 minutes. With that pattern is there anything that could go wrong (e.g. cache miss)?

I am concerned by all the possible racing condition that could happen. For example, if the website does a GET operation on an object cached in memcached while that same object is overridden by the batch job, what will happen?


回答1:


My initial instinct was that you should be able to read/write from a memcached cache with no side effects (other than a possibility of stale data due to race conditions).

On memcached's FAQ:

Is memcached atomic? Aside from any bugs you may come across, yes all commands are internally atomic. Issuing multiple sets at the same time has no ill effect, aside from the last one in being the one that sticks.

Without knowing more about your specific situation, I would not be able to advise on whether or not a cache miss was possible, but I do make a generalization that the cache should be a first tier for seeking data and not the ONLY tier; I would still have a database or some other source behind the cache to handle all of your cache misses.




回答2:


Here is the scenario: a simple website which queries a memcached cache. That same cache is updated by a batch job every 10-15 minutes. With that pattern is there anything that could go wrong (e.g. cache miss)?

Keep in mind that memcached doesn't guarantee that the data stored in it will always stay in the cache (e.g, it's a cache, not a database). If you're using memcached for other data, the results of this query may get evicted from the cache before the batch job reinserts them.




回答3:


If you receive data, it will be the latest correct data you've stored.

You will never receive half of an update, so from that perspective, it's atomic. Each request for a given key will return everything or nothing at all for that key.



来源:https://stackoverflow.com/questions/7223737/memcached-are-get-and-set-operations-atomic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!