{"id":13741,"date":"2023-07-17T10:19:57","date_gmt":"2023-07-17T02:19:57","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=13741"},"modified":"2023-07-17T15:50:29","modified_gmt":"2023-07-17T07:50:29","slug":"redis-4","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2023\/07\/17\/redis-4\/","title":{"rendered":"redis\u6027\u80fd\u4f18\u5316"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">1\u3001\u907f\u514d\u6162\u67e5\u8be2\u547d\u4ee4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6162\u67e5\u8be2\u4e5f\u5c31\u662f\u6267\u884c\u547d\u4ee4\u7684\u65f6\u5019\u8f83\u6162\u7684\u547d\u4ee4\uff0credis\u81ea\u8eab\u63d0\u4f9b\u4e86\u5f88\u591a\u547d\u4ee4\uff0c\u6162\u67e5\u8be2\u4e0e\u547d\u4ee4\u7684\u64cd\u4f5c\u590d\u6742\u5ea6\u6709\u5173\u7cfb<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679cvalue\u7684\u7c7b\u578b\u4e3astring\uff0c\u90a3\u4e48\u5728\u6267\u884cset\/get\/append\u547d\u4ee4\u65f6\u4e3b\u8981\u662f\u64cd\u4f5credis\u7684\u54c8\u5e0c\u8868\u7d22\u5f15\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u6ce8\u610f\u662f\u5927\u5199\u5b57\u6bcdO\uff0c\u4e0d\u662f\u96f6(0)\uff0c\u8fd9\u4e2a\u590d\u6742\u5ea6\u5728redis\u6587\u6863\u4e2d\u53ef\u67e5<\/li>\n\n\n\n<li>\u5982\u679cvalue\u7684\u7c7b\u578b\u4e3aZset\u7684\u65f6\u5019\uff0c\u5982\u679c\u6267\u884czrange\/zrevrange\u547d\u4ee4\u65f6\uff0c\u64cd\u4f5c\u590d\u6742\u5ea6\u5c31\u662fO(log(n)+k)\uff0ck\u4e3a\u8981\u83b7\u53d6\u6210\u5458\u4e2a\u6570\uff0cn\u4e3a\u5f53\u524d\u6210\u5458\u4e2a\u6570\uff0c\u8fd9\u4e2a\u590d\u6742\u5ea6\u5c31\u589e\u52a0\u4e86\u5f88\u591a<\/li>\n\n\n\n<li>\u5982\u679cvalue\u7684\u7c7b\u578b\u4e3aset\uff0c\u90a3\u4e48\u5728\u6267\u884csmembers\u547d\u4ee4\u65f6\uff0c\u64cd\u4f5c\u590d\u6742\u5ea6\u4e3aO(N)\uff0cn\u4e3a\u5143\u7d20\u603b\u6570\uff0c\u64cd\u4f5c\u590d\u6742\u5ea6\u589e\u52a0\u4e86\u5f88\u591a<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u67e5\u8be2\u5b58\u5728\u5f88\u591a\u6162\u67e5\u8be2\uff0c\u53ef\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\u4f18\u5316\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7528\u5176\u4ed6\u547d\u4ee4\u66ff\u4ee3\uff0c\u6bd4\u5982\u4e0a\u8ff0\u7684smembers\u83b7\u53d6\u5168\u90e8\u5143\u7d20\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528SSCAN\u904d\u5386\u5143\u7d20\uff0c\u907f\u514d\u4e00\u6b21\u8fd4\u56de\u5927\u91cf\u5143\u7d20<\/li>\n\n\n\n<li>\u5728\u5ba2\u6237\u7aef\u6267\u884c\u4e00\u4e9b\u76f8\u5173\u64cd\u4f5c\uff0c\u6bd4\u5982\u6392\u5e8f\u3001\u4ea4\u96c6\u3001\u5e76\u96c6\u7b49\u64cd\u4f5c<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">2\u3001\u5c3d\u91cf\u907f\u514d\u4f7f\u7528keys*<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u547d\u4ee4\u662f\u963b\u585e\u7684\uff0c\u5728\u6267\u884c\u671f\u95f4\uff0c\u5ba2\u6237\u7aef\u65e0\u6cd5\u6267\u884c\u5199\u5165\u64cd\u4f5c\uff0c\u5982\u679c\u6570\u636e\u91cf\u5f88\u5927\uff0c\u6267\u884ckeys*\u4f1a\u4e25\u91cd\u5f71\u54cd\u6027\u80fd\uff0c\u53ef\u4f7f\u7528SCAN\u6765\u4ee3\u66ff<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">3\u3001\u7ed9key\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">redis\u4e2d\u7684\u6570\u636e\u90fd\u662f\u5728\u5185\u5b58\u4e2d\uff0c\u5185\u5b58\u5360\u7528\u8fc7\u5927\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u8bbe\u7f6e\u5408\u7406\u7684\u8fc7\u671f\u65f6\u95f4\u5b9a\u671f\u5220\u9664\u8fc7\u671f\u6570\u636e<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">4\u3001\u907f\u514d\u7ed9\u8fc7\u591a\u7684key\u8bbe\u7f6e\u76f8\u540c\u7684\u8fc7\u671f\u65f6\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0credis\u6bcf\u8fc7100\u6beb\u79d2\u4f1a\u5220\u9664\u4e00\u4e9b\u8fc7\u671f\u7684key\uff0c\u7b97\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP\uff1a\u6b64\u53c2\u6570\u9ed8\u8ba4\u503c\u4e3a20\uff0c\u8868\u793a100\u6beb\u79d2\u5185\u968f\u673a\u62bd\u53d620\u4e2akey\uff0c\u68c0\u67e5\u662f\u5426\u8fc7\u671f\uff0c\u5982\u679c\u8fc7\u671f\u4e86\u5c31\u5220\u9664<\/li>\n\n\n\n<li>\u5982\u679credis\u4e2d\u6709\u8d85\u8fc725%\u7684key\u8fc7\u671f\u4e86\uff0c\u90a3\u4e48redis\u4f1a\u4e00\u76f4\u5220\u9664\u8fc7\u671fkey\uff0c\u76f4\u5230\u8fc7\u671fkey\u7684\u6bd4\u4f8b\u964d\u4f4e\u523025%\u4ee5\u4e0b<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u4e0a\u8ff0\u7b97\u6cd5\u53ef\u4ee5\u770b\u51fa\uff0credis\u5728100\u6beb\u79d2\u5185\u6700\u591a\u5220\u966420\u4e2akey\uff0c1s\u5185\u5c31\u4f1a\u5220\u9664200\u4e2akey\uff0c\u8fd9\u5bf9\u6027\u80fd\u5f71\u54cd\u53ef\u4ee5\u63a5\u53d7\uff0c\u4f46\u662f\u5982\u679ckey\u8fc7\u591a\uff0c\u8d85\u8fc7\u4e8625%\uff0c\u90a3\u4e48\u5c31\u4f1a\u4e00\u76f4\u4e0d\u505c\u7684\u6267\u884c\u5220\u9664\u4e0b\u53bb\uff0c\u6b64\u65f6\u5c31\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u56e0\u4e3aredis\u5220\u9664Key\u662f\u963b\u585e\u7684\uff0c\u5220\u9664\u671f\u95f4\u4e0d\u53ef\u7528(redis4\u4e4b\u540e\u53ef\u4ee5\u7528\u5f02\u6b65\u7ebf\u7a0b\u673a\u5236\u51cf\u5c11\u963b\u585e\u5f71\u54cd)\uff0c\u56e0\u6b64\u4e00\u5b9a\u8981\u907f\u514d\u7ed9\u5927\u91cf\u7684key\u8bbe\u7f6e\u76f8\u540c\u7684\u8fc7\u671f\u65f6\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">5\u3001\u68c0\u67e5\u6301\u4e45\u5316\u7b56\u7565<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679credis\u4ec5\u4ec5\u7528\u6765\u505a\u7f13\u5b58\uff0c\u5e76\u4e14\u6570\u636e\u4e0d\u9700\u8981\u6301\u4e45\u5316\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u5c06\u6301\u4e45\u5316RDB\u548cAOF\u5173\u95ed\uff0cAOF\u9ed8\u8ba4\u5173\u95ed\u7684\uff0cRDB\u53ef\u5c06save\u5b57\u6bb5\u6ce8\u91ca\u5373\u53ef\uff0c\u56e0\u4e3a\u6301\u4e45\u5316\u8fc7\u7a0b\u4e2dredis\u662f\u963b\u585e\u7684\uff0c\u6b64\u65f6\u4e0d\u53ef\u5199\u5165\uff0c\u5f71\u54cd\u6027\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">6\u3001\u66f4\u6362SSD\u786c\u76d8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u9700\u8981\u5f00\u542f\u6570\u636e\u6301\u4e45\u5316\uff0c\u90a3\u4e48\u6700\u597d\u4f7f\u7528SSD\u786c\u76d8<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">7\u3001\u4f7f\u7528\u7269\u7406\u673a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7269\u7406\u673a\u76f8\u6bd4\u865a\u62df\u673a\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u6027\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">8\u3001\u589e\u52a0\u673a\u5668\u7684\u5185\u5b58\uff0c\u907f\u514d\u4f7f\u7528swap<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u673a\u5668\u5185\u5b58\u4e0d\u8db3\uff0c\u5c31\u4f1a\u4f7f\u7528swap\uff0cswap\u662f\u673a\u5668\u78c1\u76d8\u4e0a\u5212\u5206\u7684\u4e00\u5757\u533a\u57df\u6765\u4f7f\u7528\u7684\u865a\u62df\u5185\u5b58\uff0cswap\u64cd\u4f5c\u7684\u662f\u78c1\u76d8\u6570\u636e\uff0c\u5982\u679c\u6570\u636e\u8fc7\u5927\u5bf9\u6027\u80fd\u975e\u5e38\u5927\uff0c\u56e0\u6b64\u5efa\u8bae\u589e\u52a0\u673a\u5668\u5185\u5b58<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">9\u3001\u4f7f\u7528redis\u96c6\u7fa4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">redis\u96c6\u7fa4\u91c7\u7528\u54c8\u5e0c\u69fd(slot)\u673a\u5236\uff0c\u6bcf\u4e2a\u8282\u70b9\u7684\u69fd\u6570\u91cf\u7ea6\u4e3a16384\/\u8282\u70b9\u6570\u91cf\uff0ckey\u5b58\u50a8\u5728\u54ea\u4e2a\u69fd\u7684\u8ba1\u7b97\u65b9\u5f0f\u4e3a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>slot = CRC16(key) &amp; 16384   #\u6ce8\u610f\u662f\u53d6\u4f59<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u83b7\u5f97\u5230\u69fd(slot)\u7684\u503c\u540e\uff0c\u6839\u636e\u8fd9\u4e2a\u503c\u627e\u5230\u69fd\u6240\u5728\u7684\u8282\u70b9\uff0c\u7136\u540e\u8bbe\u7f6ekey\u5373\u53ef\uff0c\u6b64\u79cd\u65b9\u5f0f\u53ef\u5c06\u8bfb\u5199\u538b\u529b\u4ece\u4e00\u53f0\u673a\u5668\u5206\u6563\u5230\u591a\u53f0\u673a\u5668\u4e0a\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">10\u3001\u6e05\u7406\u5185\u5b58\u788e\u7247(redis 4.0\u4ee5\u4e0a\u7248\u672c\u624d\u6709)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">redis\u957f\u65f6\u95f4\u4f7f\u7528\u4f1a\u5bfc\u81f4\u5185\u5b58\u788e\u7247\u8fc7\u591a\uff0c\u6b64\u65f6\u9700\u8981\u6e05\u7406\u5185\u5b58\u788e\u7247\uff0c\u9996\u5148\u67e5\u770b\u5185\u5b58\u788e\u7247\uff0c\u6267\u884c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>info memory<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"703\" height=\"401\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/5-1.png\" alt=\"\" class=\"wp-image-13769\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/5-1.png 703w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/5-1-300x171.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/5-1-600x342.png 600w\" sizes=\"auto, (max-width: 703px) 100vw, 703px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">mem_fragmentation_ratio\u8868\u793a\u5185\u5b58\u788e\u7247\u7387\uff0c\u8fd9\u4e2a\u503c\u5c31\u662fused_memory_rss\/used_memory\u5f97\u6765\u7684\uff0c\u6b64\u503c\u6709\u4e24\u79cd\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>mem_fragmentation_ratio\uff1a\u5927\u4e8e 1 \u4f46\u5c0f\u4e8e 1.5\uff0c\u8fd9\u79cd\u60c5\u51b5\u662f\u5408\u7406\u7684<\/li>\n\n\n\n<li>mem_fragmentation_ratio\uff1a\u5927\u4e8e 1.5 \u3002\u8fd9\u8868\u660e\u5185\u5b58\u788e\u7247\u7387\u5df2\u7ecf\u8d85\u8fc7\u4e86 50%\uff0c\u6b64\u65f6\u9700\u8981\u6e05\u7406<\/li>\n<\/ul>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background wp-block-paragraph\">\u6ce8\uff1aused_memory_rss\u662f\u7cfb\u7edf\u5206\u914d\u7ed9redis\u7684\u7269\u7406\u5185\u5b58\u7a7a\u95f4\uff0c\u91cc\u9762\u5305\u542b\u788e\u7247\uff0cused_memory\u662fredis\u4e3a\u4e86\u4fdd\u5b58\u6570\u636e\u5b9e\u9645\u7533\u8bf7\u4f7f\u7528\u7684\u7a7a\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u81ea\u52a8\u6e05\u7406\u5185\u5b58\u788e\u7247\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5185\u5b58\u788e\u7247\u662f\u5173\u95ed\u7684\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"83\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/6.png\" alt=\"\" class=\"wp-image-13774\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/6.png 704w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/6-300x35.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2023\/07\/6-600x71.png 600w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fee\u6539redis.conf\uff0c\u5f00\u542f\u788e\u7247\u6e05\u7406\u5f00\u5173\uff0c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5185\u5b58\u788e\u7247\u5f00\u5173\nactivedefrag yes   \n#\u5185\u5b58\u788e\u7247\u8fbe\u5230\u591a\u5c11\u7684\u65f6\u5019\u5f00\u542f\u6574\u7406\nactive-defrag-ignore-bytes 100mb\n# \u788e\u7247\u7387\u8fbe\u5230\u767e\u5206\u4e4b\u591a\u5c11\u5f00\u542f\u6574\u7406\nactive-defrag-threshold-lower 10\n# \u788e\u7247\u7387\u5c0f\u4f59\u591a\u5c11\u767e\u5206\u6bd4\u5f00\u542f\u6574\u7406\nactive-defrag-threshold-upper 100\n# Minimal effort for defrag in CPU percentage\nactive-defrag-cycle-min 25\n# Maximal effort for defrag in CPU percentage\nactive-defrag-cycle-max 75<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u914d\u7f6e\u5b8c\u6210\u540e\u91cd\u542fredis\u5373\u53ef<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u624b\u52a8\u6e05\u7406\u5185\u5b58\u788e\u7247\uff0c\u6267\u884c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>memory purge<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u907f\u514d\u6162\u67e5\u8be2\u547d\u4ee4 \u6162\u67e5\u8be2\u4e5f\u5c31\u662f\u6267\u884c\u547d\u4ee4\u7684\u65f6\u5019\u8f83\u6162\u7684\u547d\u4ee4\uff0credis\u81ea\u8eab\u63d0\u4f9b\u4e86\u5f88\u591a\u547d\u4ee4\uff0c\u6162\u67e5\u8be2\u4e0e\u547d\u4ee4\u7684\u64cd\u4f5c\u590d\u6742 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":13744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,25],"tags":[],"class_list":["post-13741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-redis","category-database"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=13741"}],"version-history":[{"count":34,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13741\/revisions"}],"predecessor-version":[{"id":13820,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/13741\/revisions\/13820"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/13744"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=13741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=13741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=13741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}