当前位置

Drupal启用Memcache的锁定踩踏保护机制

James Qi 在 2018年8月29日 - 15:07 提交

  Memcache几年前我们就开始在Drupal站上启用,对于降低数据库负载的作用很明显,偶尔会出现Memcache自己停了需要重启Memcache服务的情况,但很少很少,几个月也难得一次。

  6月底开始申请熊掌号后,重新提交URL、提交MIP版本/AMP版本,带来大量百度爬虫,服务器负载明显增高,另外其它采集者也多起来了,导致我们网站有一阵子总是卡住,甚至随机出现白屏打不开的情况,这种白屏故障会一直持续,直到用drush cc all清理缓存后恢复。

  这对网站的访问影响太大了,如果没有及时发现,可能几个小时、半天都打不开,仔细查看RDS的实时查询进程,发现一种block很容易堆积,我们使用专门的SQL语句查询来替换了Drupal自身的block,负载可以明显下降,但白屏情况依然不能杜绝。

  再仔细排查,当出现白屏故障的时候,我们用service memcached restart重启memcached服务,网站可以恢复访问,这就说明是memcache的缓存出现了问题,重点来排查memcache的设置。

  从Drupal的Memcache模块README.txt文件阅读配置办法,发现有两个地方我们以前没有留意、没有特别设置:

## LOCKING ##

The memcache-lock.inc file included with this module can be used as a drop-in
replacement for the database-mediated locking mechanism provided by Drupal
core. To enable, define the following in your settings.php:

  $conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';

Locks are written in the 'semaphore' table, which will map to the 'default'
memcache cluster unless you explicitly configure a 'semaphore' cluster.

## STAMPEDE PROTECTION ##

Memcache includes stampede protection for rebuilding expired and invalid cache
items.  To enable stampede protection, define the following in settings.php:

  $conf['memcache_stampede_protection'] = TRUE;

To avoid lock stampedes, it is important that you enable the memcache lock
implementation when enabling stampede protection -- enabling stampede protection
without enabling the Memcache lock implementation can cause worse performance and
can result in dropped locks due to key-length truncation.

Memcache stampede protection is primarily designed to benefit the following
caching pattern: a miss on a cache_get() for a specific cid is immediately
followed by a cache_set() for that cid. Of course, this is not the only caching
pattern used in Drupal, so stampede protection can be selectively disabled for
optimal performance.  For example, a cache miss in Drupal core's
module_implements() won't execute a cache_set until drupal_page_footer()
calls module_implements_write_cache() which can occur much later in page
generation.  To avoid long hanging locks, stampede protection should be
disabled for these delayed caching patterns.

  在流量很高的情况下,没有设置这个锁定和踩踏保护机制,就可能出现缓存出错的情况。在原来的settings.php文件中添加以下两行:

$conf['lock_inc'] = 'sites/all/modules/memcache/memcache-lock.inc';
$conf['memcache_stampede_protection'] = TRUE;

  添加后就再也没有出现过那种白屏错误的情况。特此记录下来给其他可能遇到类似问题的朋友参考。

自由标签:

添加新评论

Plain text

  • 不允许使用HTML标签。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。