Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I've just got a small vps. I've installed apc, which sped up normal pages by 20% - 30%. I was reading about memcached and came to the conclusion that I can use apc for the same thing (caching objects from database results) if I'm not distributing over other servers. Since I only have the one server, apc will be just as beneficial for caching things in memory.

I'm still in development mode, and I'm sure it's hard to tell what would be best for production mode. The thing is, my database queries seem pretty fast (between .0008 and .02). None of my pages are way database intensive.

Would it be beneficial to me to cache results in memory? If the database is running well right now, is it going to be having a hard time later? Also, is connecting to the database at all something that costs speed (even if I cache most of my queries, every page has to have a little database interaction for session data). So, basically if I have a limited ram, and one machine, will using apc rather than just letting the database be uncached be much faster?


share|improve this question
up vote 2 down vote accepted

Your database queries are fast now. But do they scale? Have you tried adding a realistic amount of real-world data and then trying again? Does that one query work great with 8 members but horribly with 400,000 members?

Whether to cache or not is completely application and situation specific. If you expect load to grow by a lot and your bottleneck is CPU then caching will help you, if on the other hand your bottleneck is RAM and you're using swap then it's going to hurt you.

I'm not really doing the whole to cache or not to cache discussion justice, but that's the very basic question you need to ask yourself.

share|improve this answer

It's hard to say where the best balance may be found. APC's object cache is usually used for storing objects that take a relatively long time to recreate, e.g. database resultsets that are too large to be cached by the database's own query cache or for temporary data structures that would be nice to persist between PHP requests.

Your database queries seem plenty fast. If your query cache hitrate is in the >95% range then already most results are being served from memory and APC's cache won't help much. If you do any processing on the db results, then it might pay of to store that result in memory.

Connecting to the database is relatively costly, so reusing a connection by making it a singleton is a good idea. Some database abstractions, such as the one in Zend Framework, delay making the actual connection until it is actually needed.

It's hard to give specific advice since the benefits are so dependent on your application and your server. I'd suggest using a profiler like Xdebug or Zend Debugger to see exactly where your time is going. Measuring is knowing.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.