Anyone experienced with high-performance, scalable PHP development is familiar with APC and memcached. But used alone, they each have serious limitations:
APC
- Advantages
- Low latency
- No need to serialize/unserialize items
- Scales perfectly with more web servers
- Disadvantages
- No enforced consistency across multiple web servers
- Cache is not shared; each web server must generate each item
memcached
- Advantages
- Consistent across multiple web servers
- Cache is shared across all web servers; items only need to be generated once
- Disadvantages
- High latency
- Requires serializing/unserializing items
- Easily shards data across multiple web servers, but is still a big, shared cache
Combining the two
Traditionally, application developers simply think about consistency needs. If consistency is unnecessary (or the scope of the application is one web server), APC is great. Otherwise, memcached is the choice. There is, however, a third, hybrid option: use memcached as a coordination system for invalidation with APC as the main item cache. This functions as a loose L1/L2 cache structure. To borrow terminology from multimaster replication systems, memcached stores “tombstone” records.