Comparing APC and Memcache as local PHP content cache. When you build PHP applications you need cache storage to keep your calculated data in. There are quite a few options and use case decides which solution is better.
I knew that APC is faster than memcached as there is much less overhead but I wanted to see how memcached would compare to APC user cache.
What did i test and how
I have created simple performance script that would generate strings of differnet length and try to store and load them from the cache.
I have used 128 bytes long strings for keys as usually cache keys would be at least 32 chars ;- ) I also did quite a few samples of data size. I wanted to see if performance would decrease radically with the data size. In final comparison I used 256bytes and 64KB. This covers most use cases that I work with so its good enough range for me. Values in between were scaling lineray so I do not include them.
I performed a few simple tests in tight loops trying to:
- Get keys that do not exist (miss 256 and miss 64K)
- Add new keys to the cache store (add 256 and add 64K)
- Update existing values with new strings (update 256 and update 64K)
- Read existing cache values (read 256, read 64K)
- Mixed operations read, miss, set in ratio 4,2,1. Time is averaged over 7 calls. (mixed 256, mixed 64K)
I ran all the tests on local laptop so its not 64bit and I only checked one or two threads but in both cases performance did not suffer from running 2 threads concurrently. They were scaling similarly.

Taller bar means worse performance, values averaged over 3000 requests
The only thing that looks bad is that memcache was generating much more load on the machine. I believe its because of the translation to the memecahce protocol, copying memory probably more than once etc.
In case of APC requests were flying and running mixed queries did not impace cache performance much.
Speed of APC and Memcached get calls
Just to see the order of magnitude APC get averages around 20 microseconds! That is 50 get calls within 1ms. That is light speed in PHP world! I would say caching with such speed means that APC call times can be ignored in performance tuning of your application.
Memcached get from PHP took on average about 0.00015s which is 150 microseconds. Still very good. From my experience though memcached calls from PHP on my local machine are not that fast. During optimizations about year ago I rememebr get requests were taking about 0.5ms. But it could be different environment impact.
Comparison of APC and Memcached add and update calls
Memcahced losses here much more comparing to APC. APC insert and update calls took about 0.15ms (150 microseconds) and memcached took about 0.25ms on localmachine doing nothing else.
Finally reads and writes mixture in a tight loop was toughest test for memcached. It was slower anything from 2 up to 32 times than APC!
Final verdict Memcached vs APC
In final conclusion I would say APC is much faster than memcached. If you can live with the fact that it is local cache only. You will never be able to connect to external instance. And you can control the amount of data you cache then APC is for you!
APC and Memcached both have their nuances, especially around evictions and memory limits. If you have relatively small cache scope that has to be super fast APC is the way to go!!
Comments
Post new comment