{"id":202310,"date":"2025-05-10T15:27:29","date_gmt":"2025-05-10T07:27:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202310\/"},"modified":"2025-05-10T15:27:29","modified_gmt":"2025-05-10T07:27:29","slug":"redis%e4%b8%ba%e4%bb%80%e4%b9%88%e8%a6%81%e9%81%bf%e5%85%8dbig-key","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202310\/","title":{"rendered":"Redis\u4e3a\u4ec0\u4e48\u8981\u907f\u514dbig key"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u4e3a\u4ec0\u4e48\u8981\u907f\u514dbig key<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aRedis\u4e3a\u4ec0\u4e48\u8981\u907f\u514dbig key\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<h3>\u907f\u514d big key<\/h3>\n<p>Redis \u6267\u884c\u547d\u4ee4\u662f\u5355\u7ebf\u7a0b\u7684\uff0c\u8fd9\u610f\u5473\u7740 Redis \u64cd\u4f5c\u300cbig key\u300d\u6709\u963b\u585e\u7684\u98ce\u9669\u3002<\/p>\n<p>big key \u901a\u5e38\u6307\u7684\u662f Redis \u5b58\u50a8\u7684 value \u8fc7\u5927\u3002\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\n<p>\u5355\u4e2a value \u8fc7\u5927\u3002\u5982 200M \u5927\u5c0f\u7684 String\u3002<\/p>\n<\/li>\n<li>\n<p>\u96c6\u5408\u5143\u7d20\u8fc7\u591a\u3002\u5982 List\u3001Hash\u3001Set\u3001ZSet \u4e2d\u6709\u51e0\u767e\u3001\u4e0a\u5343\u4e07\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a 200M \u5927\u5c0f\u7684 String key\uff0c\u540d\u79f0\u4e3a\u300cfoo\u300d\u3002<\/p>\n<p>\u6267\u884c\u5982\u4e0b\u547d\u4ee4<\/p>\n<pre>127.0.0.1:6379&gt;&nbsp;GET&nbsp;foo<\/pre>\n<p>\u5f53\u8fd4\u56de\u7ed3\u679c\u65f6\uff0cRedis \u4f1a\u5206\u914d 200m \u7684\u5185\u5b58\uff0c\u5e76\u6267\u884c memcpy \u62f7\u8d1d\u3002<\/p>\n<pre>void&nbsp;_addReplyProtoToList(client&nbsp;*c,&nbsp;const&nbsp;char&nbsp;*s,&nbsp;size_t&nbsp;len)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;...\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(len)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*&nbsp;Create&nbsp;a&nbsp;new&nbsp;node,&nbsp;make&nbsp;sure&nbsp;it&nbsp;is&nbsp;allocated&nbsp;to&nbsp;at\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;least&nbsp;PROTO_REPLY_CHUNK_BYTES&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;size&nbsp;=&nbsp;len&nbsp;&lt;&nbsp;PROTO_REPLY_CHUNK_BYTES?&nbsp;PROTO_REPLY_CHUNK_BYTES:&nbsp;len;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5206\u914d\u5185\u5b58\uff08\u4f8b\u5b50\u4e2d\u4e3a&nbsp;200m\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail&nbsp;=&nbsp;zmalloc(size&nbsp;+&nbsp;sizeof(clientReplyBlock));\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/*&nbsp;take&nbsp;over&nbsp;the&nbsp;allocation's&nbsp;internal&nbsp;fragmentation&nbsp;*\/\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail-&gt;size&nbsp;=&nbsp;zmalloc_usable_size(tail)&nbsp;-&nbsp;sizeof(clientReplyBlock);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tail-&gt;used&nbsp;=&nbsp;len;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5185\u5b58\u62f7\u8d1d\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(tail-&gt;buf,&nbsp;s,&nbsp;len);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;listAddNodeTail(c-&gt;reply,&nbsp;tail);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c-&gt;reply_bytes&nbsp;+=&nbsp;tail-&gt;size;\n\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;closeClientOnOutputBufferLimitReached(c,&nbsp;1);\n&nbsp;&nbsp;&nbsp;&nbsp;}}<\/pre>\n<p>\u800c Redis \u8f93\u51fa buf \u4e3a 16k<\/p>\n<pre>\/\/&nbsp;server.h#define&nbsp;PROTO_REPLY_CHUNK_BYTES&nbsp;(16*1024)&nbsp;\/*&nbsp;16k&nbsp;output&nbsp;buffer&nbsp;*\/typedef&nbsp;struct&nbsp;client&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;...\n&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[PROTO_REPLY_CHUNK_BYTES];}&nbsp;client;<\/pre>\n<p>\u8fd9\u610f\u5473\u7740 Redis \u65e0\u6cd5\u5355\u6b21\u8fd4\u56de\u54cd\u5e94\u6570\u636e\uff0c\u9700\u8981\u6ce8\u518c\u300c\u53ef\u5199\u4e8b\u4ef6\u300d\uff0c\u4ece\u800c\u89e6\u53d1\u591a\u6b21 write \u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u8017\u65f6\u70b9\uff1a<\/p>\n<ul>\n<li>\n<p>\u5206\u914d\u5927\u5185\u5b58\uff08\u4e5f\u53ef\u80fd\u91ca\u653e\u5185\u5b58\uff0c\u5982 DEL \u547d\u4ee4\uff09<\/p>\n<\/li>\n<li>\n<p>\u89e6\u53d1\u591a\u6b21\u53ef\u5199\u4e8b\u4ef6\uff08\u9891\u7e41\u6267\u884c\u7cfb\u7edf\u8c03\u7528\uff0c\u5982 write\u3001epoll_wait\uff09<\/p>\n<\/li>\n<\/ul>\n<p>\u90a3\u4e48\uff0c\u5982\u4f55\u627e\u51fa big key \u5462\uff1f<\/p>\n<p>\u5982\u679c slow log \u51fa\u73b0\u4e86\u7b80\u5355\u547d\u4ee4\uff0c\u5982 GET\u3001SET\u3001DEL\uff0c\u5927\u6982\u7387\u662f\u51fa\u73b0\u4e86 big key\u3002<\/p>\n<pre>127.0.0.1:6379&gt;&nbsp;SLOWLOG&nbsp;GET\n&nbsp;&nbsp;&nbsp;&nbsp;3)&nbsp;(integer)&nbsp;201323&nbsp;&nbsp;\/\/&nbsp;\u5355\u4f4d\u5fae\u5999\n&nbsp;&nbsp;&nbsp;&nbsp;4)&nbsp;1)&nbsp;\"GET\"\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2)&nbsp;\"foo\"<\/pre>\n<p>\u5176\u6b21\uff0c\u53ef\u4ee5\u901a\u8fc7 Redis \u5206\u6790\u5de5\u5177\u6765\u67e5\u627e big key\u3002<\/p>\n<pre>$&nbsp;redis-cli&nbsp;--bigkeys&nbsp;-i&nbsp;0.1\n...\n[00.00%]&nbsp;Biggest&nbsp;string&nbsp;found&nbsp;so&nbsp;far&nbsp;'\"foo\"'&nbsp;with&nbsp;209715200&nbsp;bytes\n\n--------&nbsp;summary&nbsp;-------\n\nSampled&nbsp;1&nbsp;keys&nbsp;in&nbsp;the&nbsp;keyspace!\nTotal&nbsp;key&nbsp;length&nbsp;in&nbsp;bytes&nbsp;is&nbsp;3&nbsp;(avg&nbsp;len&nbsp;3.00)\n\nBiggest&nbsp;string&nbsp;found&nbsp;'\"foo\"'&nbsp;has&nbsp;209715200&nbsp;bytes\n\n1&nbsp;strings&nbsp;with&nbsp;209715200&nbsp;bytes&nbsp;(100.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;209715200.00)\n0&nbsp;lists&nbsp;with&nbsp;0&nbsp;items&nbsp;(00.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;0.00)\n0&nbsp;hashs&nbsp;with&nbsp;0&nbsp;fields&nbsp;(00.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;0.00)\n0&nbsp;streams&nbsp;with&nbsp;0&nbsp;entries&nbsp;(00.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;0.00)\n0&nbsp;sets&nbsp;with&nbsp;0&nbsp;members&nbsp;(00.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;0.00)\n0&nbsp;zsets&nbsp;with&nbsp;0&nbsp;members&nbsp;(00.00%&nbsp;of&nbsp;keys,&nbsp;avg&nbsp;size&nbsp;0.00)<\/pre>\n<p>\u5bf9\u4e8e big key\uff0c\u6709\u4ee5\u4e0b\u51e0\u70b9\u5efa\u8bae\uff1a<\/p>\n<p>1.\u4e1a\u52a1\u4e2d\u5c3d\u91cf\u907f\u514d big key \u51fa\u73b0\u3002\u5f53\u51fa\u73b0 big key \u65f6\uff0c\u4f60\u8981\u5224\u65ad\u8fd9\u6837\u8bbe\u8ba1\u662f\u5426\u5408\u7406\uff0c\u53c8\u6216\u8005\u662f\u51fa\u73b0\u4e86 bug\u3002<\/p>\n<p>2.\u5c06 big key \u62c6\u5206\u4e3a\u591a\u4e2a\u5c0f key\u3002<\/p>\n<p>3.\u4f7f\u7528\u66ff\u4ee3\u547d\u4ee4\u3002<\/p>\n<ul>\n<li>\n<p>\u5982\u679c Redis \u7248\u672c\u5927\u4e8e 4.0\uff0c\u53ef\u4f7f\u7528 UNLINK \u547d\u4ee4\u66ff\u4ee3 DEL\u3002Redis \u7248\u672c\u5927\u4e8e 6.0\uff0c\u53ef\u5f00\u542f lazy-free \u673a\u5236\u3002\u5c06\u91ca\u653e\u5185\u5b58\u64cd\u4f5c\uff0c\u653e\u5230\u540e\u53f0\u7ebf\u7a0b\u6267\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>LRANGE\u3001HGETALL \u7b49\u66ff\u6362\u4e3a LSCAN\u3001HSCAN \u5206\u6b21\u83b7\u53d6\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u4f46\u6211\u8fd8\u662f\u5efa\u8bae\u5728\u4e1a\u52a1\u4e2d\u907f\u514d big key\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u4e3a\u4ec0\u4e48\u8981\u907f\u514dbig k&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-202310","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202310","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202310"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202310\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}