Press "Enter" to skip to content

Yac 2.1 Upgrade Notes

Yac (Yet Another Cache) is also something I developed back in my Weibo days — a lock-free, shared-memory user data cache for PHP, meant to replace the local Memcache and APC that Weibo had installed on its PHP machines at the time. Because of the particular requirements back then, the original design was completely lock-free. But that carries a hidden risk: a user could potentially read "wrong" data, even though in earlier testing the probability was extremely, extremely low. For the design of Yac, you can refer to the article I wrote seven years ago, Yac (Yet Another Cache) - Lock-free Shared Memory Cache.

As an important member of my "Ya" family, once I had done a round of optimizing Yaf, Yar and Yaconf, Yac obviously couldn't be left out.

2.1.0 mainly made the following improvements:

  • Use CAS atomic operations to protect reads and writes of the key. In Yac, to avoid collisions, the key carries a lot of information, most importantly a CRC checksum of the content. This upgrade uses CAS atomic operations to protect only the reads and writes of the key — while keeping the lock-free design and performance intact — which substantially lowers the probability of collisions, and in turn substantially lowers the chance of reading wrong data.

    Of course, doing this introduces a side effect. To keep the CAS from spinning forever, I set a limited number of attempts; if it still fails, it returns. That leads to one possible problem: Yac->set may fail. So if you have some particularly important content that you need to guarantee gets set, you may need to:

    <?php
    while(!($yac->set("important", "value")));
    ?>
    
  • Use the built-in SSE4.2 crc32 instruction in place of the original crc32. This gives a very noticeable performance improvement for the crc32 calls that Yac uses heavily to verify data correctness. (One more thing: this CRC32 instruction implements CRC32-C, which differs from the conventional CRC-32-IEEE 802.3 we normally use — but in Yac's scenario CRC32 is only used for self-verification, so it's not a problem.)
  • Some memory optimizations to reduce memory footprint.

After this round of upgrades, Yac's stability is greatly improved, and it can be used in PHP for cross-process content exchange and caching.

Yac 2.1 has been released: Yac At PECL
For the documentation, and for any questions while using it, come to Github: Yac At Github

enjoy!

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.