Memcached expiration time: a horrible API fail

February 26, 2014 Dalli memcached Ruby Ruby on Rails

The description of the memcached protocol says that the expiration time:

Can be up to 30 days. After 30 days, is treated as a unix timestamp of an exact date.

This means that if you, unknowingly, decide to set an expiration time of a year or ten years - perfectly reasonable for stable data:

Rails.cache.fetch "dictionary", expires_in: 1.year do

the expiration time of one year will be considered a unix timestamp, that is, for practical purposes, interpreted as one year since January 1, 1970. Which means, your data will never get saved.

What’s even worse, there won’t be any error message because this is a valid operation from the API’s point of view. Joke’s on you for not reading the API docs.

The lesson here is: don’t mix two logical parameters into one. I guess they did it to shave a byte (or even a bit) from the message payload.

Fixing the problem

While the misunderstanding is the API’s fault, it has to be prevented by failsafes in the client. In my case, the Dalli Ruby gem.

I have submitted a pull request that should fix the issue and make the external (Dalli’s) API consistent.

Buy Me a Coffee at