1

I have site which is using Memcached hard. Now it takes 7Gb of memory, every page makes a couple of gets and sets storing full pages and so on. Also I have a very dynamic sidebar which is also stored in Memcached updated and requested very often. I've noticed that this particular key takes too long to fetch. For example fetching full page takes 1-2 ms but fetching sidebar takes about 50ms! I do this request on every page and I don't like it at all, when full request takes 200-300ms 50ms for memcached get is not acceptable.

Does it have anything to do with frequent updates for this key? If yes is there any way to fix it? Maybe I should use Redis or MySQL? Or maybe switching to PHP Memcached extension instead of Memcache will help? If not, what other reason can cause this behaviour?

UPD: memcache set on this key takes CRAZY 250ms!

yefrem
  • 666
  • 7
  • 20

1 Answers1

1

I found the problem and it was my fault. But since I see 2 stars on the question I will answer to let that people know what actually happened. So because of the bug in my code the value I stored was very large (array with 3500 items instead of 20 I actually needed) and that was the reason of huge speed decrease. Before I found this I checked memcached thinking of switching to it. Here are time measures for this problematic key (in ms):

memcache get
42.184829711914
memcache set
153.13792228699
memcached get
14.186859130859
memcached set
0.093936920166016

Memcached shows much better result for get (but still not 0-1ms) and great result for write. I was ready to use it but then checked another key which is simple and works fast with memcache:

memcache get
0.17499923706055
memcache set
0.10800361633301
memcached get
6.850004196167
memcached set
0.012874603271484

So on my server memcached shows great speed on writes but not so good on reads. Good thing is that is doesn't decrease it depending on the value size. Maybe some day this information will become useful for me :)

yefrem
  • 666
  • 7
  • 20
  • If I'm not mistaken, Memcache can store up to 1 MB for values and 250b for key names, did you modify it somehow to allow for larger values? Also, how did you measure this? Did you take latency into account? Your results might be misleading without reproduction scenarios. – N.B. Oct 24 '14 at 13:38
  • @N.B. hm, you're right and that's strange. I had an array with 3500 items containing mostly text data. To check the size I used a piece of code from [here](http://stackoverflow.com/questions/2192657/how-to-determine-the-memory-footprint-size-of-a-variable), it showed me something like 7000000 bytes which is even more then 6MB. It was very rough test and the answer was just "too large", especially considering I only needed 20 array items :) I don't know why it worked, maybe MEMCACHE_COMPRESSED did the trick as it was text data. – yefrem Oct 24 '14 at 13:57
  • @N.B. maybe I'm not right, I checked my Pinba logs and see that now requests take about 4Kb of memory but took about 32Kb before I fixed the bug. From another point if I'm mistaken and the code showed me 700Kb or something it doesn't seem big enough to take 100 times more time than usual. I will update the answer and just say "very large". – yefrem Oct 24 '14 at 14:08
  • On the other hand, since Memcache has latency involved and various limits, yet you're storing text (html probably too) - why not create a ramdisk and just dump the text there from PHP? It should be MUCH faster since no TCP overhead will exist, and no limits that Memcache has will apply? This is a very fishy thingy you have going on here, and I don't think you might be utilizing Memcache in the most efficient way. Think of alternative storage strategies (MySQL is extremely quick, when configured for such purposes). – N.B. Oct 24 '14 at 14:22
  • @N.B. I see what you mean but the situation appeared because of my mistake and disappeared after I fixed the code. I do save HTML in memcache also (but that's not what the question is about) and it works well showing about 1ms for reads and writes but also adding TTL feature if compared to ram disk. – yefrem Oct 24 '14 at 14:28