Memcached-PHP cache: Is Memcache inferior to direct File Cache?

Source: Internet
Author: User
Use a local environment to Test 0.1 million and 1 million cached reads and writes. The test environment and results are as follows. Environment {code ...} code {code ...} result 10 thousand {code ...} result 0.1 million {code ...} memcache10 failed to test for times. The error message is as follows: {code ...} conclusion mem... use a local environment to Test 0.1 million and 1 million cached reads and writes. The test environment and results are as follows.

Environment
Win7 x64 AMD7750 dual-core memory 8 GApache 2.4.9PHP 5.5.12 ts vc11 memcache 2.2.7
Code

  Connect ('2017. 0.0.1 ', 11211); $ data = $ mem-> get ($ key); if (empty ($ data )) {$ data = date ('Y-m-d H: I: s'); $ mem-> set ($ key, $ data);} return $ data ;} $ t1 = microtime (true); $ I = 0; $ limit = 1000*100; // 0.1 million times $ data = null; while ($ I <$ limit) {// $ data = cacheFile ($ I); $ data = cacheMem ($ I); $ I ++ ;}$ timeUse = microtime (true)-$ t1; $ arr = ['cost' => sprintf ('%. 7fs ', $ timeUse), 'mem' => convert (memory_get_usage ()]; var_dump ($ arr );
Result 10 thousand times
Time-consuming memory: File 1.9 sec 250 kbMemcache 11 sec KB
Result 0.1 million times
Time-consuming memory: File 94 s 251.18 KBMemcache Timeout: 120 s Error
Memcache failed to perform the test for 0.1 million times. The error message is as follows:
Warning: Memcache::connect(): in D:\localhost\speed.php on line 37Warning: Memcache::get(): No servers added to memcache connection in D:\localhost\speed.phpWarning: Memcache::set(): No servers added to memcache connection in D:\localhost\speed.php on line 41Fatal error: Maximum execution time of 120 seconds exceeded in D:\localhost\speed.php on line 38
Conclusion

memcacheThis test was conducted because I did not use this memcache for caching and directly used File caching during the interview, the result is directly determined by the Technical Officer as yes.Junior programmers. But I passed this test, although it is under win, why is Memcahe performance inferior to File?
Or is there an error in my testing method?

20:52:17
After improvement, the speed is actually N times faster! 0.1 million no pressure.17.18 sec265KB memory

function cacheMem($key){    static $mem = null;    if ($mem === null) {        $mem = new Memcache();        $mem->connect('127.0.0.1', 11211);    }    $data = $mem->get($key);    if (empty($data)) {        $data = date('Y-m-d H:i:s');        $mem->set($key, $data);    }    return $data;}

Reply content:

Use a local environment to Test 0.1 million and 1 million cached reads and writes. The test environment and results are as follows.

Environment
Win7 x64 AMD7750 dual-core memory 8 GApache 2.4.9PHP 5.5.12 ts vc11 memcache 2.2.7
Code

  Connect ('2017. 0.0.1 ', 11211); $ data = $ mem-> get ($ key); if (empty ($ data )) {$ data = date ('Y-m-d H: I: s'); $ mem-> set ($ key, $ data);} return $ data ;} $ t1 = microtime (true); $ I = 0; $ limit = 1000*100; // 0.1 million times $ data = null; while ($ I <$ limit) {// $ data = cacheFile ($ I); $ data = cacheMem ($ I); $ I ++ ;}$ timeUse = microtime (true)-$ t1; $ arr = ['cost' => sprintf ('%. 7fs ', $ timeUse), 'mem' => convert (memory_get_usage ()]; var_dump ($ arr );
Result 10 thousand times
Time-consuming memory: File 1.9 sec 250 kbMemcache 11 sec KB
Result 0.1 million times
Time-consuming memory: File 94 s 251.18 KBMemcache Timeout: 120 s Error
Memcache failed to perform the test for 0.1 million times. The error message is as follows:
Warning: Memcache::connect(): in D:\localhost\speed.php on line 37Warning: Memcache::get(): No servers added to memcache connection in D:\localhost\speed.phpWarning: Memcache::set(): No servers added to memcache connection in D:\localhost\speed.php on line 41Fatal error: Maximum execution time of 120 seconds exceeded in D:\localhost\speed.php on line 38
Conclusion

memcacheThis test was conducted because I did not use this memcache for caching and directly used File caching during the interview, the result is directly determined by the Technical Officer as yes.Junior programmers. But I passed this test, although it is under win, why is Memcahe performance inferior to File?
Or is there an error in my testing method?

20:52:17
After improvement, the speed is actually N times faster! 0.1 million no pressure.17.18 sec265KB memory

function cacheMem($key){    static $mem = null;    if ($mem === null) {        $mem = new Memcache();        $mem->connect('127.0.0.1', 11211);    }    $data = $mem->get($key);    if (empty($data)) {        $data = date('Y-m-d H:i:s');        $mem->set($key, $data);    }    return $data;}

Wrong. Shouldn't you write connect... in while? How can I connect each time?

First, the overhead of establishing a TCP connection to Memcached must be greater than opening a local file. In your cli test, you have repeatedly established tens of thousands of connections, on the Web (such as PHP-FPM, MOD_PHP), you can use a persistent connection to Memcached, that is, a PHP-FPM worker maintains a persistent connection to Memcached to process multiple different requests:
Http://php.net/manual/zh/memcached.construct.php


  

Secondly, the operating system caches local files to the memory (that is, buffers/cache on Linux). The read performance is certainly good, but the frequent write performance is certainly not as good as Memcached, because Memcached read and write operations are always completed in the memory.

In addition, Memcached can be distributed (implemented by clients, such as PHP's PECL extension memcached), which is not an advantage of local file caching. Memcached is distributed by storing different keys on different servers.
Http://php.net/manual/zh/memcached.addserver.php

Note: PHP has two PECL extensions for Memcached. One is memcache and the other is memcached:
Http://php.net/manual/zh/intro.memcache.php
Http://php.net/manual/zh/intro.memcached.php
Among them, the extended memcached Based on libmemcached implements distributed, while memcache does not.

The above is correct. Your test method is incorrect because memcache has reached the maximum number of connections, so an error is reported.

But I want to know what disk you are using, SSD or FIO or SAS, What Is IOPS; note that the maximum key value in memcache is 250B, and the maximum value is 1 M, generally, when the value is greater than 1 M, that is, when the page size in memcache is exceeded, memcache is powerless, but you can change the source code to achieve or change redis (the maximum value is 512 M)

In fact, you do not need to test it. memcache data is stored in the memory. However, the memory and disk are not at the same level. We can analyze them as follows:

1 nanosecond equals one second in 1 billion, = 10 ^-9 seconds

Numbers Everyone shocould KnowL1 cache reference reads the CPU's level-1 cache 0.5 nsBranch mispredict (transfer, branch prediction) 5 nsL2 cache reference read CPU Level 2 cache 7 nsMutex lock/unlock mutex lock \ unlock 100 nsMain memory reference read memory data 100 nsCompress 1 K bytes with Zippy 1k bytes compression 10,000 nsSend 2 K bytes over 1 Gbps network sends 2 K bytes of 20,000 nsRead 1 MB sequentially from memory to read 1 MB of 250,000 nsRound trip within same datacenter from one data center one time, ping 500,000 nsDisk seek disk search 10,000,000 ns Read 1 MB sequentially from network Read 1 MB of Data sequentially from the network 10,000,000 nsRead 1 MB sequentially from disk Read 1 MB 30,000,000 ns Send from disk packet CA-> Netherlands-> CA, One packet Remote Access to 150,000,000 ns

Let's take a look at the access speed between memory and disk. The above refers to random access, which is 1000 times different, but it is about 7 times more than sequential access.

The landlord should take a good look at the design model. Your interviewer did not say wrong about you... Maybe Junior programmers Know How To Cache objects .. Who will connect to memcached every cycle?

If the file is used for storage, 100 requests will be sent. You can check the server's hard disk read/write. The server has crashed.

Disk IO latency, resource overhead, and file system overhead are also quite large.

I just want to ask what I used to measure the PHP runtime?

It turns out that you are a beginner in 1 or 2 years. But didn't the interviewer tell you why?

Also, I would like to give you a loyal suggestion. Don't be too impetuous! Or self-inflation. Looking at the current time as a node, you will always be at the bottom, and you may not understand things you have never experienced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.