Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcached Performance

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.

like image 303
Tester Avatar asked Feb 25 '14 22:02

Tester


1 Answers

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.

like image 147
Daniel Williams Avatar answered Sep 28 '22 15:09

Daniel Williams