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;
添加後就再也沒有出現過那種白屏錯誤的情況。特此記錄下來給其他可能遇到類似問題的朋友參考。
评论