LoginSignup
22
10

More than 5 years have passed since last update.

Memcachedの30days problemとはなんなのか

Last updated at Posted at 2017-01-16

今北産業

Memcachedではexpiration timeに絶対指定と相対指定をすることができる
expiration timeに30日を超える秒数を指定すると絶対指定(epoch)として扱われる
つまりexpiration timeに30日を超える秒数を相対指定のつもりで指定した場合、絶対指定として処理されて大抵の場合は死に至る

Memcachedのexpiration

Memcachedのexpirationには無効値とするまでの相対的な時間を秒数で指定する方法が一般的だ。
だが、実はMemcachedはepochによる絶対指定によるexpirationもサポートしている。
https://github.com/memcached/memcached/wiki/Commands#standard-protocol

ドキュメントを引用:

An expiration time, in seconds. '0' means never expire. Can be up to 30 days. After 30 days, is treated as a unix timestamp of an exact date.

ざっくり適当に和訳するとこう:

expiration time(有効期間)は秒数で指定します。0は無限を意味します。30日が上限です。30日以上はUNIX時間(epoch)による絶対指定として扱われます。

30日以上のexpiration timeはepochとして扱うとある。

ソースコードもみてみよう:
https://github.com/memcached/memcached/blob/1.4.34/memcached.c#L155
https://github.com/memcached/memcached/blob/1.4.34/memcached.c#L167-L177

REALTIME_MAXDELTA という定数をもとに判定している。
memcachedは内部的にはサーバーの起動時刻からの起動秒数(process_started)からの秒数でexpiration timeを管理しているのでこのような処理となる。

つまり、expiration timeに30日を超える秒数を相対指定のつもりで指定した場合、絶対指定として処理されることになる。
大抵の場合は現在時刻よりはるかに前の時刻を指定した扱いとなるため、即座にexpireされる(ように見える)。

expiration timeを動的に計算する場合など、長い時間を指定する可能性がある場合は気をつけると良い。

宣伝

YAPC::Kansai 2017 OSAKAが3/4(土)に新大阪のMOTEX HALLで開催されます。

絶賛トークプロポーザルを募集中です。チケットも残りわずか。ぜひぜひ。

22
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
10