# Memcached 1.6.5 (Rails 2.3) 10x slower

**URL:** <https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374>\
**Category:** Rails\
**Created:** [April 13, 2009, 10:37am UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374 "2009-04-13T10:37:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 13, 2009, 10:37am UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/1 "2009-04-13T10:37:15Z")

</div>

The move to memcached\_client 1.6.5 in Rails 2.3 seems to have made the  
Rails cache about 10x slower. Since that’s the opposite effect I  
would expect, I was hoping somebody would explain where I’m misreading  
these numbers?

I noticed my fragment caching was slow – it shouldn’t take 2ms just  
to read a 2k string from a localhost memcached server:  
Cached fragment hit: views/sightings/1556/row (2.3ms)  
Cached fragment hit: views/sightings/1557/row (2.4ms)  
Cached fragment hit: views/sightings/1558/row (2.2ms)

Then i did some quick and dirty benchmarks on the console:

First, with the “1.6.5” version bundled with rails 2.3

# =\> cache benchmark: (182.9ms)

Then I switch memcache\_client versions, to 1.5.0:

# =\> cache benchmark: (18.6ms)

(full benchmark code – [Rails memcached benchmark · GitHub](http://gist.github.com/94196))

These numbers hold up on both OSX and Solaris. I’ll submit a ticket  
somewhere unless it turns out I’m missing something…

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 13, 2009, 10:56am UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/2 "2009-04-13T10:56:34Z")

</div>

On Sun, Apr 12, 2009 at 7:10 PM, whatcould [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Cached fragment hit: views/sightings/1558/row (2.2ms)
> 
> These numbers hold up on both OSX and Solaris. I’ll submit a ticket  
> somewhere unless it turns out I’m missing something…

Can you post a complete test case that can be ran?

-Conrad

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 13, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/3 "2009-04-13T17:01:01Z")

</div>

Hmm, good idea. There’s probably a better way to do this but here’s a  
test I ran in activesupport/test:

> <https://gist.github.com/whatcould/94481>

(you still have to manually replace the memcache client in  
activesupport/vendor/, not sure how to easily swap out the MemCache  
client in code.)

results:

# ruby -Ilib:test test/memcache\_client\_test.rb

Loaded suite test/memcache\_client\_test  
Started  
memcache\_client version: 1.6.4.99  
0.940000 0.390000 1.330000 ( 1.354539)  
.  
Finished in 1.374947 seconds.

# ========= and switching memcache\_client versions:

memcache\_client version: 1.5.0  
0.040000 0.020000 0.060000 ( 0.138320)  
.  
Finished in 0.15171 seconds.

=============

There must be something I’m doing wrong – I’m sure someone would have  
seen this if it were really this much slower??

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 13, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/4 "2009-04-13T18:18:59Z")

</div>

Ok, on a tip from mperham:

Turning off the timeouts makes memcache in Rails 2.3 almost as fast as  
it was in 2.2:

> <https://gist.github.com/whatcould/94481>

With timeouts:  
memcache\_client version: 1.6.4.99  
9.170000 4.200000 13.370000 ( 13.567950)  
.  
No timeouts:  
memcache\_client version: 1.6.4.99  
0.730000 0.230000 0.960000 ( 1.616430)

===

That would be something like this in your production.rb:

config.cache\_store = :mem\_cache\_store, ‘127.0.0.1:11211’, { :timeout  
=\> nil }

===

But there’s still something fishy here – in the mcache-client  
benchmarks, 1.6.5 is significantly faster even without turning off the  
timeouts. Doing some more research.

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 14, 2009, 8:19am UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/5 "2009-04-14T08:19:10Z")

</div>

I’ve added a FAQ entry explaining what is going on. 1.6+ is much  
faster than 1.5.0 only when multiple servers are being used. I’m  
guessing your benchmark is testing just localhost:11211.

> <https://github.com/mperham/memcache-client/blob/d5ae15d362c379c3a6692e2d5b9beae545392cba/FAQ.rdoc>

Simple answer, turn off socket timeouts in order to dramatically speed  
up memcache-client but realize you might be woken up at 3am when  
production melts down, as I was.

mike

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 13, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/6 "2009-04-13T17:26:48Z")

</div>

By the way – I should clarify that the _actual memcache-client  
library_ is significantly faster, as the benchmarks included in the  
new (Mike P.'s) branch of memcache-client show.

[http://github.com/mperham/memcache-client/tree](http://github.com/mperham/memcache-client/tree)

Time to run the whole benchmark suite, on my oldish MacBook:  
1.7.1: 49 seconds  
1.6.4.99: 126 seconds  
1.5: 376 seconds!!

So it’s not the memcache-client itself.

---

<div class="post-metadata">

**Author:** ![whatcould](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7feea3/32.png) [@whatcould](https://www.ruby-forum.com/u/whatcould)\
**Post date:** [April 14, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/memcached-1-6-5-rails-2-3-10x-slower/165374/7 "2009-04-14T15:55:03Z")

</div>

> Simple answer, turn off socket timeouts in order to dramatically speed  
> up memcache-client but realize you might be woken up at 3am when  
> production melts down, as I was.

Better answer – split the difference: install mike’s memcache-  
client-1.7.2 (from github) and the SystemTimer gem, which speeds up  
1.6.5 substantially without turning off the timeouts.

My problem is that I added fragment caching thinking .03ms fetch times  
were normal, and now when it takes 3-4ms (with the timeouts) to get  
each fragment… stuff gets really slow. So until i refactor, timeouts  
are off.

david
