萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 實作 Memcached 一致性雜湊

前言

傳統 hash(key) % N 分散式快取在增減節點時會導致大量鍵值失效(快取雪崩)。一致性雜湊將節點與鍵映射到同一個環狀雜湊空間,僅影響鄰近節點的鍵值,大幅降低失效率。

核心原理

  1. 將每個實體 Memcached 節點虛擬為多個虛擬節點(預設 100-200 個),均勻分佈在 0 ~ 2^32-1 環上。
  2. 鍵值經雜湊(CRC32)後落在環上,順時針尋找最近的虛擬節點,對應到實體節點。
  3. 新增/移除節點時,僅影響該節點負責的鍵值區間。

完整實作(含單例、二分搜尋、虛擬節點)

<?php
class ConsistentHashMemcached
{
    private $nodes = [];           // hash => 'host:port_vnode'
    private $sortedHashes = [];    // 排序後的 hash 陣列
    private $connections = [];     // host:port => Memcached instance
    private $virtualNodes = 150;   // 每實體節點虛擬節點數
    private $servers = [];         // 實體伺服器清單

    public function __construct(array $servers, $virtualNodes = 150)
    {
        $this->servers = $servers;
        $this->virtualNodes = $virtualNodes;
        $this->buildRing();
    }

    private function buildRing()
    {
        foreach ($this->servers as $server) {
            for ($i = 0; $i < $this->virtualNodes; $i++) {
                $key = $server . '_' . $i;
                $hash = sprintf('%u', crc32($key));
                $this->nodes[$hash] = $key;
            }
        }
        ksort($this->nodes);
        $this->sortedHashes = array_keys($this->nodes);
    }

    private function getConnection($serverKey)
    {
        if (!isset($this->connections[$serverKey])) {
            list($host, $port) = explode(':', $serverKey);
            $mc = new Memcached();
            $mc->addServer($host, (int)$port);
            // 建議設定
            $mc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
            $mc->setOption(Memcached::OPT_TCP_NODELAY, true);
            $mc->setOption(Memcached::OPT_NO_BLOCK, true);
            $this->connections[$serverKey] = $mc;
        }
        return $this->connections[$serverKey];
    }

    private function findNode($keyHash)
    {
        $hashes = $this->sortedHashes;
        $count = count($hashes);
        if ($count === 0) throw new RuntimeException('無可用節點');

        // 二分搜尋找第一個 >= keyHash 的節點
        $low = 0; $high = $count - 1;
        while ($low <= $high) {
            $mid = (($low + $high) >> 1);
            if ($hashes[$mid] < $keyHash) {
                $low = $mid + 1;
            } else {
                $high = $mid - 1;
            }
        }
        // 環狀回繞
        $index = $low % $count;
        return $this->nodes[$hashes[$index]];
    }

    public function get($key)
    {
        $hash = sprintf('%u', crc32($key));
        $nodeKey = $this->findNode($hash);
        list($server) = explode('_', $nodeKey);
        return $this->getConnection($server)->get($key);
    }

    public function set($key, $value, $expire = 0)
    {
        $hash = sprintf('%u', crc32($key));
        $nodeKey = $this->findNode($hash);
        list($server) = explode('_', $nodeKey);
        return $this->getConnection($server)->set($key, $value, $expire);
    }

    public function add($key, $value, $expire = 0)
    {
        $hash = sprintf('%u', crc32($key));
        $nodeKey = $this->findNode($hash);
        list($server) = explode('_', $nodeKey);
        return $this->getConnection($server)->add($key, $value, $expire);
    }

    public function delete($key)
    {
        $hash = sprintf('%u', crc32($key));
        $nodeKey = $this->findNode($hash);
        list($server) = explode('_', $nodeKey);
        return $this->getConnection($server)->delete($key);
    }

    // 靜態工廠方法
    public static function createDefault()
    {
        $servers = [
            '127.0.0.1:11211',
            '127.0.0.1:11212',
            '127.0.0.1:11213',
            '127.0.0.1:11214',
            '127.0.0.1:11215',
        ];
        return new self($servers);
    }
}

// 使用範例
$cache = ConsistentHashMemcached::createDefault();
$cache->set('user:1001', ['name' => 'John', 'email' => 'john@example.com'], 3600);
$data = $cache->get('user:1001');
var_dump($data);
?>

效能測試結果(參考)

場景單節點 Memcached一致性雜湊 (5節點 x 150虛擬節點)
10,000 次 set+get0.42 秒2.15 秒
記憶體佔用基準+15%

PHP 純實作效能較 C 擴充套件慢約 5 倍。高流量建議:

  • 使用 php-memcached 擴充套件內建一致性雜湊(Memcached::DISTRIBUTION_CONSISTENT)
  • 或改用 Redis Cluster 原生分片
  • 或將演算法移至 PHP 擴充套件 / Swoole 進程常駐

總結

一致性雜湊有效解決分散式快取擴縮容時的雪崩問題。PHP 純實作適合中小流量或學習用途,大規模生產環境建議使用原生支援的客戶端庫或遷移至 Redis Cluster。

copyright © 萬盛學電腦網 all rights reserved