Memcached, блокировка и условия состязания

Если Вы используете это:

div#footer li{
  width:160px;
  display:block;
  float: left;
  text-align:center;
}

Все это выглядит прекрасным: D

19
задан Micael 22 October 2009 в 17:54
поделиться

3 ответа

If you're dealing with data that doesn't necessarily need to be updated realtime, and to me the view count is one of them, then you could add an expires field to the objects that are stored in memcache.

Once that expiration happens, it'll go back to the database and read the new value, but until then it will leave it alone.

Of course for new posts you may want this updated more often, but you can code for this.

Memcache only stores one copy of your object in one of its instances, not in many of them, so I wouldn't worry about object locking or anything. That is for the database to handle, not your cache.

Edit:

Memcache offers no guarantee that when you're getting and setting from varied servers that your data won't get clobbered.

From memcache docs:

  • A series of commands is not atomic. If you issue a 'get' against an item, operate on the data, then wish to 'set' it back into memcached, you are not guaranteed to be the only process working on that value. In parallel, you could end up overwriting a value set by something else.

Race conditions and stale data

One thing to keep in mind as you design your application to cache data, is how to deal with race conditions and occasional stale data.

Say you cache the latest five comments for display on a sidebar in your application. You decide that the data only needs to be refreshed once per minute. However, you neglect to remember that this sidebar display is renderred 50 times per second! Thus, once 60 seconds rolls around and the cache expires, suddenly 10+ processes are running the same SQL query to repopulate that cache. Every time the cache expires, a sudden burst of SQL traffic will result.

Worse yet, you have multiple processes updating the same data, and the wrong one ends up dating the cache. Then you have stale, outdated data floating about.

One should be mindful about possible issues in populating or repopulating our cache. Remember that the process of checking memcached, fetching SQL, and storing into memcached, is not atomic at all!

15
ответ дан 30 November 2019 в 04:44
поделиться

Думаю, решением может быть сохранение количества просмотров отдельно от объекта Post, а затем выполнение на нем INCR. Конечно, для отображения информации потребуется прочитать 2 отдельных значения из memcached.

3
ответ дан 30 November 2019 в 04:44
поделиться

Операции с memcached атомарны. серверный процесс будет ставить запросы в очередь и полностью обслуживать каждый из них перед переходом к следующему, поэтому нет необходимости в блокировке.

edit: memcached имеет команду приращения, которая является атомарной. Вам просто нужно сохранить счетчик как отдельное значение в кеше.

1
ответ дан 30 November 2019 в 04:44
поделиться
Другие вопросы по тегам:

Похожие вопросы: