rfc:sync

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:sync [2014/09/30 02:59] – created guilhermeblancorfc:sync [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== PHP RFC: Add pecl_http to core ====== +====== PHP RFC: Add pecl_sync to core ====== 
-  * Version: 1.0+  * Version: 1.1
   * Date: 2014-09-29   * Date: 2014-09-29
   * Author: Guilherme Blanco <guilhermeblanco@php.net>   * Author: Guilherme Blanco <guilhermeblanco@php.net>
Line 7: Line 7:
  
 ===== Introduction ===== ===== Introduction =====
 +
 A discussion whether it is feasible to add [[http://pecl.php.net/package/sync|sync]] PECL extension to the core. A discussion whether it is feasible to add [[http://pecl.php.net/package/sync|sync]] PECL extension to the core.
 +
 +===== Rationale =====
 +
 +When dealing with high volume websites, caching is a common technique used by many to not overload web servers. Considering this scenario, upon a cache expire, you may potentially face cache stampede (aka. cache dogpiling), where multiple requests are trying to write same key at the same time.
 +Not all drivers support antidogpiling natively, leading to errors, notices or even unwanted siblings along the way. Mutexes could drastically reduce the number of write attempts if only one request is writing and others wait or just use non-cache behavior.
 +Worst scenario comes from cache drivers that supports siblings, suck as Riak (https://github.com/php-riak/php_riak), where a high volume web server starts to create on a cache key multiple siblings containing exact same data.
 +
 +Currently, PHP only offers basic locking concept through the following possibilities:
 +  * File locking emulation
 +  * Semaphore through System V compliant: sem_*
 +  * Relying on pthreads extension
  
 ===== Proposal ===== ===== Proposal =====
  
 Providing the functionality of pecl_sync with the core distribution. Providing the functionality of pecl_sync with the core distribution.
 +
 +'sync' extension introduces synchronization objects into PHP. Named and unnamed Mutex, Semaphore, Event, and Reader-Writer objects provide OS-level synchronization on both *NIX (POSIX semaphores required) and Windows platforms.
 +
 +===== Example =====
 +
 +<code php>
 +function saveEntry(Entry $entry) {
 +    $lock = new SyncMutex($entry->getKey());
 +
 +    if ($lock->lock(0)) {
 +       // Do expensive saving procedure
 +
 +       $lock->unlock();
 +    }
 +
 +    // Another process is dealing with save already... =)
 +}
 +</code>
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 28: Line 58:
  
   * pecl_sync has not been ported to PHPNG yet.   * pecl_sync has not been ported to PHPNG yet.
-  * Possible class renaming+  * Possible class renaming (ie. SyncMutex => Mutex, SyncSemaphore => Semaphore, etc)
   * Documentation   * Documentation
  
Line 38: Line 68:
 ===== Changelog ===== ===== Changelog =====
  
 +  * 1.1
 +    * Added rationale and code example
   * 1.0   * 1.0
     * Original draft     * Original draft
rfc/sync.1412045999.txt.gz · Last modified: 2017/09/22 13:28 (external edit)