开发者

Memcache vs APC for a single server site data caching

开发者 https://www.devze.com 2022-12-12 05:26 出处:网络
I have a single server site thats pushing 200k unqiues per day, and the traffic doubles roughly every 40 days (for the last 5 months anyway).

I have a single server site thats pushing 200k unqiues per day, and the traffic doubles roughly every 40 days (for the last 5 months anyway).

I pretty much only plan to cache the output of mysql_query functions for an hour or so. If cache is older than that, run query, put result back into the cache for another hour.

My mysql DB is about 200mb in size (grows by maybe 10-20mb/month).

Im doing a lot of file caching by writing HTML outputs and using them for a few minutes, and then regenerating开发者_JAVA技巧 the html.

Unfortunately, since its a database site, that allows for many sorting, searching and ordering methods, as well as pagination.... there are over 150,000 cached pages. Im also not caching the search queries, which cause most of the load.

I'd like to implement a caching system, and I wanted to know which one is faster. Would love to see some benchmarks.


A quick Googling says that APC is 5 times faster than Memcached.

My experience say that APC is nearly 7-8 times faster than Memcached.. but, memchached can be accessed by different services (for example, if you run mainly on apache and delegates some traffic, e.g. static contents like images or pure html, to another web-service, like lighttpd), that can be really usefull, if not indispensable.

APC have less feature than memcached and is easly to use and optimize, but this depends on your needs.


Like you mentioned there are a few different aspects of caching. I probably would focus on the following aspects of caching in your php app:

  • opcode caching which caches the compiled bytecode of php scripts. You can see a benchmark here (albeit an older article): http://itst.net/654-php-on-fire-three-opcode-caches-compared Note: I strongly recommend using opcode caching.

  • Caching user data - APC and others do this. This would be your reference data or data that is fairly static and doesn't change often. You can clear the cache every day or trigger a clean cache when this reference data changes. This is also strongly recommended since typically reference data is used frequently and doesn't change often.

  • Caching sql queries - I know that Zend makes this task easy with a simple setup. Since these queries don't change this is another obvious one (like you mentioned)

Additional (if possible):

  • caching html pages - obviously caching a static page is faster than a generated one and typically this is hard to do since most pages in apps are so dynamic. Worth it if you can do it although if your queries are cached and your SQL is simple I wouldn't focus on this.

  • caching sql results - personally I stay away from this. I'll let the database do its work and what it does best since the DBMS typically has caching. I may cache the results for the thread of execution (i.e., I just retrieved this so don't do it again) but I don't go much beyond that.

I've used APC and eAccelerator successfully (I personally like to work with APC and it supposed opcode caching and user data caching for my reference data and sql queries). Use XDebug to profile your code.


You want to compare APC key-value store vs Memcache right? Because APC also does opcode cache, which is a different thing.

Well, on a single machine, APC k-v cache is way faster than memcache. Memcache has more functionality, but is intended for distributed environments, while APC works on single servers only.

I did a benchmark recently to set and then get 1 million keys in both, each key was a sequential integer, and the values were a 32byte string.

Over localhost, memcache could retrieve 12k keys/second in a single thread. APC returned 90K/second. However, if you use multi-threads or "multi_get" with memcache, it gets very close to APC performance.

The benchmark ran on a 1GB vps at slicehost.


in my case apc is 59 times faster than memcache

<?php
ini_set('apc.enable_cli','1'); //if u run in cli you may need to do changes in php.ini
error_reporting(E_ALL);
$mem=new Memcache();
$mem->connect('127.0.0.1',11211);
$mem->replace('testin','something');
$i=0;
$time=time()+microtime();
apc_store ( 'testin','something');
$num=1000000;
while($i<$num){
 $mem->get('testin');
$i++;
}
echo "memcache took: ",time()+microtime()-$time," for 1 million gets","\n";
$time=time()+microtime();
$i=0;
print_r(apc_fetch('testin'));
while($i<$num) {
apc_fetch('testin');
$i++;
}
echo "apc took: ",time()+microtime()-$time,"for 1 million gets \n";

here is the output

memcache took: 37.657398939133 for 1 million gets
somethingapc took: 0.64599800109863for 1 million gets


It's almost impossible to accurately predict which would be faster. I would run tests with both in a development environment with similar data.

When performance is of importance, always use a profiler.


Im use IPB 3.1.4 with APC it works justy two times faster then without it.

Requests per second:    43.46 [#/sec] (mean)
Requests per second: 24.23 [#/sec] (mean)

Don't test IPB with memcached yet

0

精彩评论

暂无评论...
验证码 换一张
取 消