君たちは永遠にそいつらより若い

技術と音楽と日々のこと。

memcached で session.save_path が合っているのに保存できない



過ぎ去りしある日のこと。

memcached に書き込めないファイルがあるよ!

Warning: Unknown: Failed to write session data (memcache). Please verify that the current setting of session.save_path is correct (tcp://xxx.xxx.xxx.xxx:11211) in Unknown on line 0

ちょっと大きめの数百KBのデータを順次 memcache に突っ込んでいたら、書き込めるファイルとそうでないファイルがあった。ファイルサイズの閾値にも今ひとつ法則性が無い。

あのとき調べたこと

memcached は立ち上っているとして、以下あたりを見ていった。エラーメッセージを元に調べた際、大体の他記事はtcp://が付いてねーよ!と言うものだった。付いてるし!!

  • memcached の session.save_path は合ってる?ちゃんとtcp://ついてる?
  • php.ini の upload_max_filesize に引っかかってない?
  • apache の LimitRequestBody とかの制限サイズに引っかかってない?
  • memcached の limit_maxbytes は十分?メモリ不足してない?
  • memcached の maximum object size に引っかかってない? ←私はこれにひっかかっていた!

maximum object size is 1MB ですよ奥さん

いつ消えるかわからないので、該当項引用。

http://docs.oracle.com/cd/E17952_01/refman-5.6-en/ha-memcached-faq.html

16.6.5.2: What is the maximum size of an object you can store in memcached? Is that configurable?

The default maximum object size is 1MB. In memcached 1.4.2 and later, you can change the maximum size of an object using the -I command line option.

For versions before this, to increase this size, you have to re-compile memcached. You can modify the value of the POWER_BLOCK within the slabs.c file within the source.

In memcached 1.4.2 and higher, you can configure the maximum supported object size by using the -I command-line option. For example, to increase the maximum object size to 5MB:

$ memcached -I 5m

If an object is larger than the maximum object size, you must manually split it. memcached is very simple: you give it a key and some data, it tries to cache it in RAM. If you try to store more than the default maximum size, the value is just truncated for speed reasons.

要するに、オブジェクトの上限は一般的な設定では1MBですよ。ということ。意味不明な発生条件だったのは、私が調子こいてデータ突っ込んでいるうちに、累計1MBを超過して、データが書き込めなくなったその瞬間にのみエラーが発生していたということ。

一つの解決方法は、-Iオプションで、上限を調節すること。以下は、上限を5MBに設定して、いいの!?って聞かれているところ。よくないので止めて、実装を変えました^^

# memcached -u root -I 5m -d WARNING: Setting item max size above 1MB is not recommended! Raising this limit increases the minimum memory requirements and will decrease your memory efficiency.

参考にした記事

Failed to write session data, php and memcached

http://stackoverflow.com/questions/12112319/failed-to-write-session-data-php-and-memcached