{"id":295,"date":"2026-09-06T01:22:48","date_gmt":"2026-09-05T17:22:48","guid":{"rendered":"https:\/\/www.ziliaochi.cn\/index.php\/2026\/09\/06\/consistent-hashing-cap\/"},"modified":"2026-09-06T01:22:48","modified_gmt":"2026-09-05T17:22:48","slug":"consistent-hashing-cap","status":"publish","type":"post","link":"https:\/\/www.ziliaochi.cn\/index.php\/2026\/09\/06\/consistent-hashing-cap\/","title":{"rendered":"\u4e00\u81f4\u6027\u54c8\u5e0c\u4e0e\u6570\u636e\u5206\u5e03\uff1a\u54c8\u5e0c\u73af\u3001\u865a\u62df\u8282\u70b9\u4e0e\u54c8\u5e0c\u69fd"},"content":{"rendered":"<div style=\"font-family:-apple-system,BlinkMacSystemFont,\"Segoe UI\",\"PingFang SC\",\"Hiragino Sans GB\",\"Microsoft YaHei\",sans-serif;font-size:16.5px;line-height:1.9;color:#35435c;letter-spacing:.2px\">\n<div style=\"background:linear-gradient(135deg,#f5f9ff,#eef5ff);border:1px solid #dce8f8;border-left:4px solid #6d8fb8;border-radius:12px;padding:16px 20px;margin:0 0 24px;font-size:14.5px;color:#3d5570;line-height:2\"><span style=\"display:inline-block;background:#6d8fb8;color:#fff;font-size:12.5px;font-weight:700;padding:3px 10px;border-radius:6px;margin-right:12px;letter-spacing:1px;vertical-align:1px\">\u5173\u952e\u8bcd<\/span>\u4e00\u81f4\u6027\u54c8\u5e0c\u3001\u54c8\u5e0c\u73af\u3001\u865a\u62df\u8282\u70b9\u3001Redis Cluster\u3001\u54c8\u5e0c\u69fd\u3001\u6570\u636e\u5206\u7247<\/div>\n<div style=\"background:#fafbfd;border:1px solid #e8edf5;border-radius:12px;padding:18px 22px;margin:0 0 30px\">\n<div style=\"font-size:13.5px;font-weight:700;color:#8a99ae;letter-spacing:2px;margin-bottom:12px\">\u76ee\u5f55<\/div>\n<ol style=\"margin:0;padding-left:20px\">\n<li style=\"margin:8px 0;font-size:15px;line-height:1.6\"><a href=\"#sec-1\" style=\"color:#42546e;text-decoration:none;border:none\">\u4e00\u3001\u6734\u7d20\u54c8\u5e0c\u7684\u6b7b\u7a74\uff1a\u8282\u70b9\u6570\u53d8\u4e86\uff0c\u4e00\u5207\u91cd\u6765<\/a><\/li>\n<li style=\"margin:8px 0;font-size:15px;line-height:1.6\"><a href=\"#sec-2\" style=\"color:#42546e;text-decoration:none;border:none\">\u4e8c\u3001\u4e00\u81f4\u6027\u54c8\u5e0c\uff1a\u628a\u300c\u5bf9\u8282\u70b9\u53d6\u6a21\u300d\u6539\u6210\u300c\u5728\u73af\u4e0a\u627e\u4f4d\u7f6e\u300d<\/a><\/li>\n<li style=\"margin:8px 0;font-size:15px;line-height:1.6\"><a href=\"#sec-3\" style=\"color:#42546e;text-decoration:none;border:none\">\u4e09\u3001\u865a\u62df\u8282\u70b9\uff1a\u89e3\u51b3\u300c\u73af\u4e0d\u5747\u5300\u300d\u4e0e\u300c\u70ed\u70b9\u300d<\/a><\/li>\n<li style=\"margin:8px 0;font-size:15px;line-height:1.6\"><a href=\"#sec-4\" style=\"color:#42546e;text-decoration:none;border:none\">\u56db\u3001\u4e00\u81f4\u6027\u54c8\u5e0c\u4e0e CAP\uff1a\u5206\u533a\u65f6\u7684\u5f52\u5c5e\u6743\u95ee\u9898<\/a><\/li>\n<\/ol>\n<\/div>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\"><span style=\"font-size:17px;color:#1e2f47\">\u300c\u6570\u636e\u91cf\u6da8\u5341\u500d\uff0c\u6269\u5bb9\u8981\u8fc1\u79fb\u591a\u5c11\u6570\u636e\uff1f\u300d\u2014\u2014\u8fd9\u4e2a\u95ee\u9898\u7684\u7b54\u6848\uff0c\u5c31\u662f\u4e00\u81f4\u6027\u54c8\u5e0c\u5b58\u5728\u7684\u7406\u7531\u3002\u5b83\u662f\u5206\u5e03\u5f0f\u5b58\u50a8\uff08Dynamo\u3001Cassandra\u3001Redis Cluster \u7684\u8bbe\u8ba1\u524d\u8eab\uff09\u7684\u57fa\u77f3\u7b97\u6cd5\uff0c\u4e5f\u662f\u9762\u8bd5\u4e2d\u68c0\u9a8c\u5206\u5e03\u5f0f\u601d\u7ef4\u6df1\u5ea6\u7684\u7ecf\u5178\u9898\u3002\u8fd9\u7bc7\u4ece\u6734\u7d20\u54c8\u5e0c\u7684\u95ee\u9898\u51fa\u53d1\uff0c\u8bb2\u6e05\u73af\u3001\u865a\u62df\u8282\u70b9\u3001\u70ed\u70b9\u6cbb\u7406\u4e09\u5c42\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u5b83\u4e0e CAP \u7684\u5173\u7cfb\u3002<\/p>\n<h2 style=\"font-size:23px;line-height:1.45;font-weight:700;color:#0f1c2e;margin:46px 0 18px;padding:0 0 12px 14px;border-left:5px solid #6d8fb8;border-bottom:1px solid #eef2f7;letter-spacing:.3px\" id=\"sec-1\">\u4e00\u3001\u6734\u7d20\u54c8\u5e0c\u7684\u6b7b\u7a74\uff1a\u8282\u70b9\u6570\u53d8\u4e86\uff0c\u4e00\u5207\u91cd\u6765<\/h2>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u7f13\u5b58\u96c6\u7fa4\u7528 <code style=\"background:#eef4fb;color:#0b6bcb;padding:2px 7px;border-radius:5px;font-size:14px;font-family:\"JetBrains Mono\",Consolas,Monaco,\"Courier New\",monospace;border:1px solid #dce8f6;white-space:nowrap\">hash(key) % N<\/code> \u5206\u7247\uff0cN=4 \u65f6\u4e00\u5207\u6b63\u5e38\u3002\u6269\u5230 N=5\uff0c\u51e0\u4e4e\u6240\u6709 key \u7684\u6620\u5c04\u5168\u90e8\u6539\u53d8\u2014\u2014\u7f13\u5b58\u77ac\u95f4\u6574\u4f53\u5931\u6548\uff0cDB \u88ab\u6253\u7a7f\u3002\u6570\u5b66\u4e0a\uff1a\u8282\u70b9\u6570\u4ece N \u53d8\u5230 N+1\uff0c<code style=\"background:#eef4fb;color:#0b6bcb;padding:2px 7px;border-radius:5px;font-size:14px;font-family:\"JetBrains Mono\",Consolas,Monaco,\"Courier New\",monospace;border:1px solid #dce8f6;white-space:nowrap\">key % N<\/code> \u4e0e <code style=\"background:#eef4fb;color:#0b6bcb;padding:2px 7px;border-radius:5px;font-size:14px;font-family:\"JetBrains Mono\",Consolas,Monaco,\"Courier New\",monospace;border:1px solid #dce8f6;white-space:nowrap\">key % (N+1)<\/code> \u5927\u6982\u7387\u4e0d\u540c\uff0c<strong style=\"color:#0f1c2e;font-weight:600\">\u5931\u6548\u7387 \u2248 N\/(N+1)<\/strong>\uff0c\u63a5\u8fd1\u5168\u91cf\u3002<\/p>\n<h2 style=\"font-size:23px;line-height:1.45;font-weight:700;color:#0f1c2e;margin:46px 0 18px;padding:0 0 12px 14px;border-left:5px solid #6d8fb8;border-bottom:1px solid #eef2f7;letter-spacing:.3px\" id=\"sec-2\">\u4e8c\u3001\u4e00\u81f4\u6027\u54c8\u5e0c\uff1a\u628a\u300c\u5bf9\u8282\u70b9\u53d6\u6a21\u300d\u6539\u6210\u300c\u5728\u73af\u4e0a\u627e\u4f4d\u7f6e\u300d<\/h2>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u628a\u54c8\u5e0c\u7a7a\u95f4\u7ec4\u7ec7\u6210\u4e00\u4e2a\u73af\uff08\u5982 0 \u2192 2^32-1\uff09\uff0c\u8282\u70b9\u4e0e key \u90fd\u6620\u5c04\u5230\u73af\u4e0a\uff0ckey \u987a\u65f6\u9488\u627e\u5230\u7684\u7b2c\u4e00\u4e2a\u8282\u70b9\u5373\u5f52\u5c5e\u8282\u70b9\u3002\u8fd9\u6837\u8282\u70b9\u589e\u51cf\u65f6\uff0c<strong style=\"color:#0f1c2e;font-weight:600\">\u53ea\u6709\u76f8\u90bb\u533a\u95f4\u7684 key \u9700\u8981\u8fc1\u79fb<\/strong>\uff0c\u5931\u6548\u7387\u4ece ~100% \u964d\u5230 ~1\/N\uff08K \u4e2a key \u4e2d\u53ea\u8fc1\u79fb K\/N \u4e2a\uff09\u3002<\/p>\n<figure style=\"margin:30px 0;text-align:center\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"900\" src=\"https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig.png\" alt=\"\u4ece\u6734\u7d20\u54c8\u5e0c\u5230\u4e00\u81f4\u6027\u54c8\u5e0c\" class=\"wp-image-294\" style=\"max-width:100%;height:auto;border-radius:12px;border:1px solid #e6ecf4;box-shadow:0 8px 24px rgba(15,30,60,.07)\" srcset=\"https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig.png 1200w, https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig-300x225.png 300w, https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig-1024x768.png 1024w, https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig-768x576.png 768w, https:\/\/www.ziliaochi.cn\/wp-content\/uploads\/2026\/09\/consistent-hashing-cap-fig-800x600.png 800w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption style=\"margin-top:12px;font-size:13.5px;color:#7b8aa0;text-align:center;line-height:1.75\"><\/figcaption><\/figure>\n<h2 style=\"font-size:23px;line-height:1.45;font-weight:700;color:#0f1c2e;margin:46px 0 18px;padding:0 0 12px 14px;border-left:5px solid #6d8fb8;border-bottom:1px solid #eef2f7;letter-spacing:.3px\" id=\"sec-3\">\u4e09\u3001\u865a\u62df\u8282\u70b9\uff1a\u89e3\u51b3\u300c\u73af\u4e0d\u5747\u5300\u300d\u4e0e\u300c\u70ed\u70b9\u300d<\/h2>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u7269\u7406\u8282\u70b9\u5c11\u65f6\uff0c\u73af\u4e0a\u7684\u533a\u95f4\u5212\u5206\u5929\u7136\u4e0d\u5747\uff0c\u6570\u636e\u503e\u659c\u4e25\u91cd\u3002\u6807\u51c6\u89e3\u6cd5\u662f\u6bcf\u4e2a\u7269\u7406\u8282\u70b9\u6620\u5c04\u6210 100\u2013200 \u4e2a\u865a\u62df\u8282\u70b9\u6563\u5e03\u5728\u73af\u4e0a\uff1a\u533a\u95f4\u5212\u5206\u8d8b\u8fd1\u5747\u5300\uff0c\u4e14\u65b0\u589e\u8282\u70b9\u4ece\u591a\u4e2a\u65e7\u8282\u70b9\u300c\u5404\u62ff\u4e00\u70b9\u300d\uff0c\u8fc1\u79fb\u538b\u529b\u5206\u6563\u3002\u865a\u62df\u8282\u70b9\u540c\u65f6\u662f<strong style=\"color:#0f1c2e;font-weight:600\">\u5f02\u6784\u5bb9\u91cf<\/strong>\u7684\u8c03\u8282\u65cb\u94ae\u2014\u2014\u6027\u80fd\u5f3a\u7684\u673a\u5668\u591a\u653e\u865a\u62df\u8282\u70b9\uff0c\u81ea\u7136\u591a\u627f\u8f7d\u6570\u636e\u3002<\/p>\n<pre style=\"background:#0f1a2b;color:#dbe6f5;border-radius:12px;padding:18px 20px;overflow-x:auto;font-size:13.6px;line-height:1.78;margin:22px 0;border:1px solid #1e2c42;font-family:\"JetBrains Mono\",Consolas,Monaco,\"Courier New\",monospace\"><code style=\"background:none;border:none;color:inherit;padding:0;font-size:inherit;font-family:inherit\"># \u4f2a\u4ee3\u7801\uff1a\u5e26\u865a\u62df\u8282\u70b9\u7684\u4e00\u81f4\u6027\u54c8\u5e0c\nring = {}                                # hash(vnode) -&gt; node\nfor node in nodes:\n    for i in range(150):                 # \u6bcf\u8282\u70b9 150 \u4e2a\u865a\u62df\u8282\u70b9\n        ring[hash(f\"{node}#v{i}\")] = node\n\ndef locate(key):\n    h = hash(key)\n    return min(k for k in ring if k &gt;= h, default=min(ring))  # \u987a\u65f6\u9488\u7b2c\u4e00\u4e2a<\/code><\/pre>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u503c\u5f97\u5bf9\u7167\u7684\u662f Redis Cluster \u7684\u9009\u62e9\uff1a\u5b83\u653e\u5f03\u54c8\u5e0c\u73af\uff0c\u6539\u7528 <strong style=\"color:#0f1c2e;font-weight:600\">16384 \u4e2a\u56fa\u5b9a\u54c8\u5e0c\u69fd<\/strong>\uff0c\u69fd\u662f\u6570\u636e\u5f52\u5c5e\u7684\u6700\u5c0f\u5355\u4f4d\uff0c\u8282\u70b9\u95f4\u6309\u69fd\u8fc1\u79fb\u2014\u2014\u672c\u8d28\u662f\u628a\u300c\u73af\u300d\u79bb\u6563\u5316\uff0c\u8fc1\u79fb\u63a7\u5236\u66f4\u7cbe\u7ec6\u3002\u4e24\u79cd\u65b9\u6848\u601d\u8def\u540c\u6e90\uff1a<strong style=\"color:#0f1c2e;font-weight:600\">\u8ba9\u6570\u636e\u5f52\u5c5e\u4e0e\u8282\u70b9\u6570\u89e3\u8026<\/strong>\u3002<\/p>\n<h2 style=\"font-size:23px;line-height:1.45;font-weight:700;color:#0f1c2e;margin:46px 0 18px;padding:0 0 12px 14px;border-left:5px solid #6d8fb8;border-bottom:1px solid #eef2f7;letter-spacing:.3px\" id=\"sec-4\">\u56db\u3001\u4e00\u81f4\u6027\u54c8\u5e0c\u4e0e CAP\uff1a\u5206\u533a\u65f6\u7684\u5f52\u5c5e\u6743\u95ee\u9898<\/h2>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u4e00\u81f4\u6027\u54c8\u5e0c\u89e3\u51b3\u300c\u6570\u636e\u653e\u54ea\u300d\uff0cCAP \u8ba8\u8bba\u7684\u662f\u300c\u5206\u533a\u53d1\u751f\u65f6\u4fdd\u4ec0\u4e48\u300d\u3002\u4e24\u8005\u5728\u5de5\u7a0b\u4e0a\u4ea4\u6c47\u4e8e\u540c\u4e00\u4e2a\u95ee\u9898\uff1a\u7f51\u7edc\u5206\u533a\u65f6\uff0c\u73af\u4e0a\u67d0\u6bb5\u7684\u4e24\u7aef\u8282\u70b9\u5404\u81ea\u8ba4\u4e3a\u81ea\u5df1\u62e5\u6709\u8be5\u533a\u95f4\u600e\u4e48\u529e\uff1f\u4e1a\u754c\u7b54\u6848\u901a\u5e38\u662f<strong style=\"color:#0f1c2e;font-weight:600\">\u4e00\u81f4\u6027\u54c8\u5e0c\u8d1f\u8d23\u8def\u7531 + \u5f3a\u4e00\u81f4\u534f\u8bae\uff08Raft \u7c7b\uff09\u8d1f\u8d23\u526f\u672c\u4ef2\u88c1<\/strong>\u2014\u2014\u5373\u54c8\u5e0c\u73af\u5b9a\u5206\u7247\uff0c\u6bcf\u4e2a\u5206\u7247\u5185\u90e8\u7528\u591a\u6570\u6d3e\u51b3\u6588\u6743\u5a01\uff0c\u628a CAP \u7684\u9009\u62e9\u9898\u4ea4\u7ed9\u526f\u672c\u5c42\u800c\u4e0d\u662f\u8def\u7531\u5c42\u3002<\/p>\n<p style=\"font-size:16.2px;line-height:1.95;color:#35435c;margin:16px 0;letter-spacing:.2px\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1a\u4e00\u81f4\u6027\u54c8\u5e0c\u7684\u5168\u90e8\u4ef7\u503c = <strong style=\"color:#0f1c2e;font-weight:600\">\u5f52\u5c5e\u4e0e\u8282\u70b9\u6570\u89e3\u8026\uff08\u73af\uff09+ \u5747\u5300\u4e0e\u5f39\u6027\uff08\u865a\u62df\u8282\u70b9\uff09<\/strong>\u3002\u9762\u8bd5\u7b54\u5230\u8fd9\u4e24\u5c42\uff0c\u518d\u80fd\u5bf9\u6bd4\u54c8\u5e0c\u69fd\u65b9\u6848\uff0c\u5c31\u662f\u4e00\u4efd\u5b8c\u6574\u7684\u56de\u7b54\u3002<\/p>\n<div style=\"margin:42px 0 0;padding:22px 0 0;border-top:1px solid #eaeef4\">\n<div style=\"font-size:13px;color:#93a1b5;letter-spacing:1px;margin-bottom:12px\">\u6807\u7b7e<\/div>\n<p><span style=\"display:inline-block;background:#f3f6fa;border:1px solid #e4eaf2;color:#5b6c85;font-size:13px;padding:5px 12px;border-radius:999px;margin:0 8px 8px 0\">#\u4e00\u81f4\u6027\u54c8\u5e0c<\/span><span style=\"display:inline-block;background:#f3f6fa;border:1px solid #e4eaf2;color:#5b6c85;font-size:13px;padding:5px 12px;border-radius:999px;margin:0 8px 8px 0\">#\u5206\u5e03\u5f0f\u7cfb\u7edf<\/span><span style=\"display:inline-block;background:#f3f6fa;border:1px solid #e4eaf2;color:#5b6c85;font-size:13px;padding:5px 12px;border-radius:999px;margin:0 8px 8px 0\">#\u6570\u636e\u5206\u7247<\/span><span style=\"display:inline-block;background:#f3f6fa;border:1px solid #e4eaf2;color:#5b6c85;font-size:13px;padding:5px 12px;border-radius:999px;margin:0 8px 8px 0\">#CAP<\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4ece\u6734\u7d20\u54c8\u5e0c ~100% \u5931\u6548\u7684\u95ee\u9898\u51fa\u53d1\uff0c\u8bb2\u6e05\u54c8\u5e0c\u73af\u7684\u8fc1\u79fb\u6570\u5b66\u3001\u865a\u62df\u8282\u70b9\u89e3\u51b3\u503e\u659c\u4e0e\u70ed\u70b9\u7684\u673a\u5236\u3001Redis Cluster \u54c8\u5e0c\u69fd\u7684\u5bf9\u7167\u8bbe\u8ba1\uff0c\u4ee5\u53ca\u4e00\u81f4\u6027\u54c8\u5e0c\u4e0e CAP \u5728\u5206\u533a\u65f6\u7684\u4ea4\u6c47\u95ee\u9898\u3002<\/p>\n","protected":false},"author":1,"featured_media":293,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[167,164,165,166],"class_list":["post-295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","category-computer-basics","tag-cap","tag-164","tag-165","tag-166"],"_links":{"self":[{"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":0,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/media\/293"}],"wp:attachment":[{"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ziliaochi.cn\/index.php\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}