Memcached Performance
Asked Answered
U

1

5

I feel the speed of Memcached in my website is slower than Mysql queries. Please see the screenshot of performance of my website I got from New Relic.

enter image description here

I don't know how to optimize memcached in my CentOS server. Please see the configuration and performance screenshots of Memcached. I feel the number of Total Connections is high.

enter image description here

Please see Live Stats below enter image description here

The following is how I use Memcached in my website

<?php


class dataCache {

        function setMemData($key, $var, $flag = false, $expire = 36000) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }
                if ($result = $memcache->set($key, $var, $flag, time() + $expire)) {
                        return TRUE;
                } else {
                        return FALSE;
                }
        }

        function getMemData($key) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }

                if ($data = $memcache->get($key)) {
                        return $data;
                } else {
                        return FALSE;
                }
        }

        function delMemData($key) {
                global $memcache;
                if (!$memcache) {
                        $memcache = new Memcache;
                        $memcache->connect('localhost', 11211) or die("Could not connect");
                }

                if ($result = $memcache->delete($key)) {
                        return TRUE;
                } else {
                        return FALSE;
                }
        }
}

And at the end of each PHP page, I use the following way to close the connection

if(isset($memcache)){                                                                 
     $memcache->close();
}

Do I need more memories for this server? How to reduce the number of connections? Any suggestions to improve the performance?

--------------EDIT------------------------

As the comments mentioned, the current connections are 9. The total connections are 671731. The number of connections might be not a problem.

Unprincipled answered 25/2, 2014 at 22:44 Comment(12)
Can you post the code where you initialize your memcached connection from PHP? That is far too many connections assuming 1 PHP server and 1 memcached server.Dye
I think this question might be more appropriate on serverfault.comGland
@DanielWilliams, Thanks for suggestions. I just added the PHP code. The problem might in the code.Unprincipled
"I feel the number of Total Connections is high." --- that's easy: don't connect to memcached and the total number of connections will be low.Hieratic
What makes you thinking your memcached is slower than mysql?Hieratic
@zerkms, please see the first chart I got from New Relic. I hope the speed of Memcached is much faster than Database.Unprincipled
@Don Li: first chart shows that generally it takes more time to interact with memcached than with mysql and it doesn't say anything about their speed. It might be 100 requests to memcached and 1 to mysql per a page load. In that case the memcached is ~100 times faster than mysql according your chart.Hieratic
@zerkms, you are right. But in my website, I don't think there are so many requests in one page, Usually 1-5 requests. Like DanielWilliams mentioned, there are too many connections. I don't know how to decrease the connections.Unprincipled
@Don Li: "too many" is too vague. Why do you think so? What would be "that's exactly what I want" number of connections? "I don't know how to decrease the connections" --- as I mentioned before - for this very question the only answer: just shut memcached server down and the number of connections will decrease to the ideal 0.Hieratic
@zerkms, you can see from my screenshot. I set the Max Connection to 2014, but the total connection now is more than 670K. I need to know the reason. I feel it may affect performance.Unprincipled
Does "total connections" above indicate total connections since the server started (opened + closed) or just currently open?Rub
@Don Li: it's a number of connections since the server was started. For the last 10 hours as per your screenshot. "I feel it may affect performance." --- when you're discussing about performance optimization you should trust numbers not feelings.Hieratic
D
9

Here are a few ways to make this go faster.

Your total connections are actually how many connections have been created to memcached.

First, use the memcached php extension NOT the memcache extension. They are entirely different and the memcache extension is pretty much deprecated to death. Memcached extension uses libmemcached which is incredibly faster and has better features (like binary protocol, better timeouts, udp)

Second, use persistent connections. For your workload these should be entirely sufficient and reduce the cost of constantly reconnecting to memcache.

Third, use multi get/set/delete/etc. If you know you will need 10 memcache keys in your request, ask for all 10 at once. This can give you a big performance increase if you are looping over memcache requests at any point.

Another caveat is that NewRelic is historically BAD at reporting time spent in memcache. It can misreport time spent in php as time spent in memcache due to how the instrument the zend engine.

As for why your memcache and mysql performance are so close, you are most likely running rather simple queries so the time spent on memcache and on mysql are comparable. Memcache is extremely fast but it is also a network hop and that is usually the largest amount of the time spent waiting for memcache. You could try running memcache locally or using APC instead of memcache if you really only have 1 server.

Dye answered 25/2, 2014 at 23:47 Comment(3)
is APC reliable to store 4GB of data?Hieratic
Yes it can. However, you would require 4GB of spare ram on your web server to accommodate this. If you aren't already running APC you should be since it it can greatly increase the performance of your PHP code (regardless of using it as a key value cache).Dye
just a side note: I'm not an OP and I do run APC already (not for data caching purposes though) :-)Hieratic

© 2022 - 2024 — McMap. All rights reserved.