Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcache with transactions?

Although I couldn't find anything on it, I thought I would double check - does memcache support transactions?

If not, which I'm betting is the likely answer, then what is the correct method to work with memcache in an environment with transactions? Wouldn't you have to read from the DB every time you plan to update, even if the data is in cache, just so you can set your locks? For example, a script that updates some data would look like this:

  1. BEGIN; SELECT ... FOR UPDATE;
  2. Calculate...
  3. UPDATE TABLE ...;
  4. Update cache
  5. COMMIT;

I think you have to update cache after you run your update query, in case you hit a deadlock and need to rollback. But you should also update cache before committing, in case any other thread is waiting to read your data and may accidentally update it's cache with even newer data before you, resulting in your now-out-of-date data overwriting it.

Is this the correct sequence of steps? Is there any way to not have to hit the db on reads for update?

like image 524
Tesserex Avatar asked Mar 03 '11 22:03

Tesserex


People also ask

Is Memcache better than Redis?

Redis uses a single core and shows better performance than Memcached in storing small datasets when measured in terms of cores. Memcached implements a multi-threaded architecture by utilizing multiple cores. Therefore, for storing larger datasets, Memcached can perform better than Redis.

What happens when memcache is full?

From the memcached wiki: When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.

Is Memcached still used?

Just a note: as it was confirmed by the current maintainer on Twitter, Memcached is still actively developed / maintained.

How does memcache store data?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.


1 Answers

Memcache does have an operator called CAS (Check And Set - or Compare And Swap) which may help you. The PHP manual has some documentation on it, Memcached::cas, but it should also be supported in other libraries and languages.

like image 184
Alister Bulman Avatar answered Sep 28 '22 03:09

Alister Bulman