Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcache vs APC for a single server site data caching

People also ask

Is memcached distributed cache?

With Memcached you can build highly scalable distributed caching solutions designed to provide fast and consistent performance.

What are the two types of caching?

L1 cache, or primary cache, is extremely fast but relatively small, and is usually embedded in the processor chip as CPU cache. L2 cache, or secondary cache, is often more capacious than L1.

What happens when memcache is full?

From the memcached wiki: When the table is full, subsequent inserts cause older data to be purged in least recently used (LRU) order.


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