Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached – Are GET and SET operations atomic?

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?

like image 535
Martin Avatar asked Aug 28 '11 20:08

Martin


People also ask

Are Memcached operations atomic?

Is memcached atomic? Aside from any bugs you may come across, yes all commands are internally atomic.

What is Memcached and how it works?

What is Memcached? Memcached is an easy-to-use, high-performance, in-memory data store. It offers a mature, scalable, open-source solution for delivering sub-millisecond response times making it useful as a cache or session store.

Which is a difference between Memcached and Redis?

Memcached is designed for simplicity while Redis offers a rich set of features that make it effective for a wide range of use cases. Understand your requirements and what each engine offers to decide which solution better meets your needs.

Is Memcached single threaded?

Because Memcached is multithreaded, you can easily scale up by giving it more computational resources, but you will lose part or all of the cached data (depending on whether you use consistent hashing). Redis, which is mostly single-threaded, can scale horizontally via clustering without loss of data.


1 Answers

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.

like image 199
Sean Freitag Avatar answered Sep 22 '22 16:09

Sean Freitag