views:

683

answers:

4

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 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.

+2  A: 

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.

Ben S
A: 

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.

Arthur Frankel
Im not sure I understand the difference between sql query caching... and sql result caching. Why wouldn't I wanna cache this?
Yegor
Sql query caching is caching the sql statements (i.e., select credit from account where id = ?) - these don't change. The results of that query may be $200.00, but that amount may change depending upon the type of data you're dealing with. Typically the queries are just a "few" compared to the possible results.
Arthur Frankel
Here's an explanation from Doctrine (ORM) - http://www.doctrine-project.org/documentation/manual/1_0/en/caching
Arthur Frankel
A: 

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.

Diego
+1  A: 

Google say that APC is 5 times faster than Memcached, my experience say that APC is nearly 7-8 times faster than Memcached.. but, memcanched 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.

DaNieL