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.
The “extremely fresh” check for the APC item (see below) allows throttling hits to memcached. Even a one-second tolerance for cache incoherency massively limits the amount of traffic to the shared memcached pool.
Reading
The algorithm below may not be perfect, but I’ll revise it as I continue work on an implementation.
- Attempt to load the item from APC:
- On an APC hit, check if the item is extremely fresh or recently verified as fresh against memcached. (For perfect cache coherency, the answer is always “not fresh.”)
- If fresh, retu\r\n \the item.
- If not fresh, check if there is a tombstone record in memcached:
- If there is no tombstone (or the tombstone post-dates the local item):
- Update the freshness timestamp on the local item.
- Retu\r\n \the local item.
- Otherwise, treat as an APC miss.
- If there is no tombstone (or the tombstone post-dates the local item):
- On an APC miss, attempt to load the item from memcached:
- On a memcache hit:
- Store the item into APC.
- Retu\r\n \the item.
- On a soft memcache miss (the item is available but due for replacement), attempt to take out a semaphore in APC:
- If the APC semaphore was successful, attempt to take out a semaphore in memcached:
- If the memcached semaphore was successful:
- Write the semaphore to APC.
- Rebuild the cache item and write it (see below).
- Release the semaphore in memcached. (The semaphore in APC should clear itself very quickly.)
- If the memcached semaphore was unsuccessful:
- Copy the memcached rebuild semaphore to APC. Store this very briefly (a second or so); it is only to prevent hammering memcached for semaphore checks.
- Retu\r\n \the slightly stale item from memcache.
- If the memcached semaphore was successful:
- If the APC semaphore was unsuccessful:
- Retu\r\n \the slightly stale item.
- If the APC semaphore was successful, attempt to take out a semaphore in memcached:
- On a hard memcache miss (no item available at all):
- Is a stampede to generate the item acceptable?
- If yes:
- Generate the item real-time.
- Store to the cache.
- If no:
- Use the APC/memcache semaphore system (see above) to lock regeneration of the item.
- If the current request cannot grab the semaphore, fail as elegantly as possible.
- If yes:
- Is a stampede to generate the item acceptable?
- On a memcache hit:
- On an APC hit, check if the item is extremely fresh or recently verified as fresh against memcached. (For perfect cache coherency, the answer is always “not fresh.”)
Writing/invalidating
- Write to/delete from memcached.
- Write to/delete from APC.
- Set the tombstone record in memcached. This record should persist long enough for all web servers to notice that their local cache needs to be updated.
Making the web a better place to teach, learn, and advocate starts here...
When you subscribe to our newsletter!