tags:

views:

304

answers:

2

I am using memcache for cacheing objects, but would like to add in addition an opcode accelerator like APC. Since they both involve cacheing, I am not sure if they will be "stepping on each others toes", i.e. I am not sure if memcache is already an OP code accelerator.

Can someone clarify? I would like to use them both - bit for different things. memcache for cacheing my objects and APC for code acceleration

+3  A: 

yes you can use them both together at the same time.

nickf
+7  A: 

Memcache is more along the lines of a distributed object cache vs something like APC or XCache, which stores PHP bytecode in memory so you avoid having to parse it each time. Their main purposes are different.

For example, if you had a very CPU intensive database query that people often requested, you could cache the resulting object in memcache and then refer to it instead of re-running that query all the time.

APC & XCache do have similar object caching features, but you are limited to the host machine. What if you wanted 10 different servers to all have access to that one object without having to re-do the query for each server? You'd just direct them to your memcache server and away you go. You still get a benefit if you only have a single server because using memcache will help you scale in the future if you need to branch out to more boxes.

The main thing to consider is if you think your app is going to need to scale. Memcache has more overhead since you have to use a TCP connection to access it, versus just a function call for APC/Xcache shared objects.

However, Memcache has the following benefits:

  • Faster than the disk or re-running query.
  • Scales to multiple servers.
  • Works with many different languages, your objects are not locked into PHP + APC/Xcache only.
  • All processes/languages have access to the same objects, so you don't have to worry if your PHP child processes have an empty object cache or not. This may not be as big a deal if you're running PHP-FPM though.

In most cases, I would recommend caching your objects in memcache as it's not much harder & is more flexible for the future.

Keep in mind that this is only regarding caching objects. Memcache does NOT have any bytecode or PHP acceleration features, which is why I would run it side-by-side with APC or Xcache

Klinky
"You still get a benefit if you only have a single server because using memcache will help you scale in the future if you need to branch out to more boxes." -- so there's no benefit on a single server?
nickf
@nickf, added more detail to my answer.
Klinky
+1. Another point to add: APC does not handle high-concurrency as well as memcached does. We use both memcached and APC in production, for storing appropriate application data -- and we're considering starting to use Redis as well (for persistence, and list operations). At high levels of concurrency, each one is good at certain things that the others aren't.
Frank Farmer