{"id":104483,"date":"2026-09-12T15:17:09","date_gmt":"2026-09-12T07:17:09","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/104483.html"},"modified":"2026-09-12T15:17:09","modified_gmt":"2026-09-12T07:17:09","slug":"redis%e9%9d%a2%e8%af%95%e5%9c%ba%e6%99%af%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/104483.html","title":{"rendered":"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218"},"content":{"rendered":"<h2>Redis \u9762\u8bd5\u573a\u666f\u5b9e\u6218&#xff1a;\u50cf\u804a\u5929\u4e00\u6837\u628a\u95ee\u9898\u7b54\u660e\u767d<\/h2>\n<p>&#x1f4cc; \u8fd9\u4efd\u6587\u6863\u662f\u4ec0\u4e48&#xff1a;\u4e0d\u662f\u6559\u79d1\u4e66&#xff0c;\u4e5f\u4e0d\u662f\u9898\u5e93&#xff0c;\u800c\u662f\u4e00\u4efd&#034;\u9762\u8bd5\u73b0\u573a\u6a21\u62df&#034;\u3002\u6bcf\u9053\u9898\u90fd\u8fd8\u539f\u771f\u5b9e\u7684\u9762\u8bd5\u5bf9\u8bdd\u573a\u666f\u2014\u2014\u9762\u8bd5\u5b98\u600e\u4e48\u95ee\u3001\u4f60\u8be5\u600e\u4e48\u5f00\u53e3\u3001\u8bf4\u591a\u4e45\u5408\u9002\u3001\u54ea\u4e9b\u8bdd\u80fd\u52a0\u5206\u3001\u54ea\u4e9b\u8bdd\u4f1a\u7ed9\u81ea\u5df1\u6316\u5751\u3002<\/p>\n<p>\u6bcf\u4e2a\u573a\u666f\u5305\u542b\u4e24\u90e8\u5206&#xff1a;<\/p>\n<ul>\n<li>&#x1f5e3;\ufe0f \u53e3\u8ff0\u56de\u7b54&#xff1a;\u9762\u8bd5\u73b0\u573a&#034;\u8bf4&#034;\u51fa\u6765\u7684\u7248\u672c&#xff0c;\u6309\u9650\u65f6\u8282\u594f\u7ec4\u7ec7&#xff1b;<\/li>\n<li>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801&#xff1a;\u56de\u7b54\u80cc\u540e\u7684\u5b8c\u6574\u6280\u672f\u652f\u6491&#xff08;\u6d41\u7a0b\u56fe\u3001Lua \u811a\u672c\u3001\u4ee3\u7801\u793a\u4f8b\u3001\u5bf9\u6bd4\u8868\u683c&#xff09;&#xff0c;\u9762\u8bd5\u524d\u590d\u4e60\u7528&#xff0c;\u88ab\u8981\u6c42&#034;\u5199\u4e00\u4e0b\/\u753b\u4e00\u4e0b&#034;\u65f6\u76f4\u63a5\u62ff\u5f97\u51fa\u6765\u3002<\/li>\n<\/ul>\n<p>\u4e09\u4e2a\u4f7f\u7528\u539f\u5219&#xff1a;<\/p>\n<li>\u56de\u7b54\u662f&#034;\u8bf4&#034;\u51fa\u6765\u7684&#xff0c;\u4e0d\u662f&#034;\u80cc&#034;\u51fa\u6765\u7684\u3002\u6bcf\u9898\u90fd\u7ed9\u4e86\u5efa\u8bae\u65f6\u957f&#xff08;1~3 \u5206\u949f&#xff09;&#xff0c;\u6309\u8fd9\u4e2a\u8282\u594f\u7ec4\u7ec7\u8bed\u8a00&#xff0c;\u522b\u4e00\u5f00\u53e3\u5c31\u5239\u4e0d\u4f4f\u8f66\u3002<\/li>\n<li>\u6709\u7ecf\u5386\u5c31\u8bb2\u7ecf\u5386\u3002\u9762\u8bd5\u5b98\u6700\u60f3\u542c\u7684\u4e0d\u662f\u6807\u51c6\u7b54\u6848&#xff0c;\u800c\u662f&#034;\u4f60\u771f\u7684\u8e29\u8fc7\u8fd9\u4e2a\u5751\u5417&#034;\u3002\u6587\u6863\u91cc\u7a7f\u63d2\u4e86&#034;\u6211\u7684\u7ecf\u5386&#034;\u8bdd\u672f\u6a21\u677f&#xff0c;\u628a\u4f60\u7684\u771f\u5b9e\u9879\u76ee\u5957\u8fdb\u53bb&#xff0c;\u6bd4\u4efb\u4f55\u516b\u80a1\u6587\u90fd\u6709\u8bf4\u670d\u529b\u3002<\/li>\n<li>\u522b\u7ed9\u81ea\u5df1\u6316\u5751\u3002\u6bcf\u4e2a\u56de\u7b54\u90fd\u4e3b\u52a8\u628a\u8fb9\u754c\u60c5\u51b5\u3001\u5931\u8d25\u8def\u5f84\u8bf4\u6e05\u695a&#xff0c;\u8ba9\u9762\u8bd5\u5b98\u6ca1\u6709&#034;\u8ffd\u95ee\u5230\u4f60\u9732\u9985&#034;\u7684\u673a\u4f1a\u3002<\/li>\n<hr \/>\n<p>\u5ffd\u8a00\u60a0\u8bed&#xff1a;<\/p>\n<p>\u2003\u2003\u9762\u8bd5\u8fd9\u4e8b\u513f\u5427&#xff0c;\u8ddf\u8c08\u604b\u7231\u4e00\u4e2a\u9053\u7406\u3002\u770b\u4e0d\u4e0a\u4f60\u7684&#xff0c;\u8bf4\u5565\u90fd\u662f\u9519&#xff0c;\u8fde\u547c\u5438\u90fd\u4e0d\u5bf9&#xff1b;\u770b\u4e0a\u4f60\u7684&#xff0c;\u4f60\u968f\u4fbf\u804a\u804a\u4ed6\u90fd\u89c9\u5f97\u6361\u5230\u5b9d\u3002\u6240\u4ee5\u522b\u628a\u90a3\u4e9b\u62d2\u7edd\u592a\u5f53\u56de\u4e8b&#xff0c;\u90a3\u662f\u4eba\u5bb6\u7684\u201c\u53e3\u5473\u504f\u597d\u201d&#xff0c;\u4e0d\u662f\u4f60\u7684\u201c\u4ef7\u503c\u5224\u5b9a\u201d\u3002\u591a\u8bd5\u51e0\u5bb6&#xff0c;\u603b\u80fd\u78b0\u5230\u90a3\u4e2a\u201c\u770b\u4f60\u54ea\u54ea\u90fd\u987a\u773c\u201d\u7684\u4e1c\u5bb6\u3002<\/p>\n<h3>&#x1f4d1; \u573a\u666f\u7d22\u5f15<\/h3>\n<table>\n<tr>\u9886\u57df\u573a\u666f\u5efa\u8bae\u65f6\u957f<\/tr>\n<tbody>\n<tr>\n<td>\u5e93\u5b58<\/td>\n<td>\u573a\u666f\u4e00&#xff1a;\u79d2\u6740\u5e93\u5b58\u6263\u51cf&#xff0c;\u600e\u4e48\u9632\u8d85\u5356<\/td>\n<td>3 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u5e93\u5b58<\/td>\n<td>\u573a\u666f\u4e8c&#xff1a;\u8ba2\u5355\u53d6\u6d88\u540e\u5e93\u5b58\u600e\u4e48\u56de\u6eda<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u5e93\u5b58<\/td>\n<td>\u573a\u666f\u4e09&#xff1a;Redis \u5e93\u5b58\u548c\u6570\u636e\u5e93\u600e\u4e48\u4fdd\u6301\u4e00\u81f4<\/td>\n<td>2~3 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u6d41\u6c34<\/td>\n<td>\u573a\u666f\u56db&#xff1a;\u5e93\u5b58\u6d41\u6c34\u600e\u4e48\u8bbe\u8ba1<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u6d41\u6c34<\/td>\n<td>\u573a\u666f\u4e94&#xff1a;\u652f\u4ed8\u56de\u8c03\u91cd\u590d\u5230\u8fbe\u600e\u4e48\u5e42\u7b49<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u8ba2\u5355<\/td>\n<td>\u573a\u666f\u516d&#xff1a;\u8ba2\u5355 30 \u5206\u949f\u672a\u652f\u4ed8\u81ea\u52a8\u53d6\u6d88<\/td>\n<td>2~3 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u8ba2\u5355<\/td>\n<td>\u573a\u666f\u4e03&#xff1a;\u600e\u4e48\u9632\u6b62\u7528\u6237\u91cd\u590d\u4e0b\u5355<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58<\/td>\n<td>\u573a\u666f\u516b&#xff1a;\u7a7f\u900f\u3001\u51fb\u7a7f\u3001\u96ea\u5d29<\/td>\n<td>3 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58<\/td>\n<td>\u573a\u666f\u4e5d&#xff1a;\u7f13\u5b58\u548c\u6570\u636e\u5e93\u7684\u4e00\u81f4\u6027<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u9632\u62a4<\/td>\n<td>\u573a\u666f\u5341&#xff1a;\u79d2\u6740\u63a5\u53e3\u9650\u6d41\u9632\u5237<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u9632\u62a4<\/td>\n<td>\u573a\u666f\u5341\u4e00&#xff1a;\u5206\u5e03\u5f0f\u9501<\/td>\n<td>3 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u9009\u578b<\/td>\n<td>\u573a\u666f\u5341\u4e8c&#xff1a;\u5e93\u5b58\u4e3a\u4ec0\u4e48\u7528 String \u4e0d\u7528 Hash<\/td>\n<td>1~2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u9760\u6027<\/td>\n<td>\u573a\u666f\u5341\u4e09&#xff1a;Redis \u6302\u4e86\u600e\u4e48\u529e<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u9760\u6027<\/td>\n<td>\u573a\u666f\u5341\u56db&#xff1a;\u600e\u4e48\u53d1\u73b0\u548c\u4fee\u590d\u5e93\u5b58\u4e0d\u4e00\u81f4<\/td>\n<td>2 \u5206\u949f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u573a\u666f\u4e00&#xff1a;\u79d2\u6740\u5e93\u5b58\u6263\u51cf&#xff0c;\u600e\u4e48\u9632\u8d85\u5356&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u4f60\u4eec\u7535\u5546\u7cfb\u7edf\u6709\u79d2\u6740\u5427&#xff1f;10 \u4e07\u4eba\u62a2 100 \u4ef6\u5546\u54c1&#xff0c;\u5e93\u5b58\u6263\u51cf\u600e\u4e48\u505a\u7684&#xff1f;\u600e\u4e48\u4fdd\u8bc1\u4e0d\u8d85\u5356&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;3 \u5206\u949f&#xff08;\u8fd9\u662f\u91cd\u5934\u620f&#xff0c;\u503c\u5f97\u5c55\u5f00\u8bb2&#xff09;<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u63a5\u4f4f\u95ee\u9898&#xff0c;\u8868\u660e\u601d\u8def&#xff08;15 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8fd9\u4e2a\u573a\u666f\u6211\u4e4b\u524d\u5728\u9879\u76ee\u91cc\u5b9e\u9645\u505a\u8fc7&#xff0c;\u6211\u6309\u2019\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u6263\u6570\u636e\u5e93 \u2192 \u6211\u4eec\u600e\u4e48\u505a\u7684 \u2192 \u4e2d\u95f4\u8e29\u8fc7\u4ec0\u4e48\u5751\u2019\u8fd9\u4e2a\u987a\u5e8f\u8bb2\u4e00\u4e0b\u3002\u201d<\/p>\n<p>\u7b2c\u4e00\u6bb5&#xff1a;\u4e3a\u4ec0\u4e48\u6570\u636e\u5e93\u625b\u4e0d\u4f4f&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6700\u76f4\u89c9\u7684\u505a\u6cd5\u662f\u6570\u636e\u5e93\u91cc UPDATE stock &#061; stock &#8211; 1 WHERE stock &gt; 0&#xff0c;\u8fd9\u6761 SQL \u672c\u8eab\u662f\u539f\u5b50\u7684&#xff0c;\u4e5f\u80fd\u9632\u8d85\u5356\u3002\u4f46 10 \u4e07\u5e76\u53d1\u6253\u8fc7\u6765&#xff0c;\u6240\u6709\u8bf7\u6c42\u90fd\u5728\u62a2\u540c\u4e00\u884c\u7684\u884c\u9501&#xff0c;\u6570\u636e\u5e93 TPS \u76f4\u63a5\u6389\u5230\u51e0\u767e&#xff0c;\u8fde\u63a5\u6c60\u51e0\u79d2\u949f\u5c31\u8017\u5c3d\u4e86&#xff0c;\u800c\u4e14\u4f1a\u628a\u6574\u4e2a\u4e0b\u5355\u94fe\u8def\u62d6\u57ae\u2014\u2014\u4e0d\u53ea\u662f\u79d2\u6740\u6302&#xff0c;\u6b63\u5e38\u8ba2\u5355\u4e5f\u4e0b\u4e0d\u4e86\u3002\u6240\u4ee5\u6570\u636e\u5e93\u4e0d\u80fd\u5f53\u7b2c\u4e00\u9053\u9632\u7ebf\u3002\u201d<\/p>\n<p>\u7b2c\u4e8c\u6bb5&#xff1a;\u6211\u4eec\u7684\u65b9\u6848\u2014\u2014Redis \u9884\u6263 &#043; Lua \u539f\u5b50\u64cd\u4f5c&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u6211\u4eec\u7684\u505a\u6cd5\u5206\u4e09\u6b65&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u6d3b\u52a8\u5f00\u59cb\u524d\u628a\u5e93\u5b58\u9884\u70ed\u5230 Redis&#xff0c;\u6bd4\u5982 seckill:stock:10086 \u8bbe\u6210 100\u3002\u8fd9\u4e00\u6b65\u662f\u8fd0\u8425\u914d\u7f6e\u6d3b\u52a8\u540e\u7531\u5b9a\u65f6\u4efb\u52a1\u63d0\u524d 5 \u5206\u949f\u5b8c\u6210\u7684\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u8bf7\u6c42\u8fdb\u6765\u7528\u4e00\u6bb5 Lua \u811a\u672c\u505a\u2019\u5224\u65ad &#043; \u6263\u51cf\u2019\u3002\u8fd9\u91cc\u5fc5\u987b\u7528 Lua&#xff0c;\u4e0d\u80fd\u5148 GET \u518d DECR\u2014\u2014\u4e24\u6b65\u4e4b\u95f4\u6709\u5e76\u53d1\u7a97\u53e3&#xff0c;100 \u4e2a\u8bf7\u6c42\u540c\u65f6\u8bfb\u5230 1&#xff0c;\u5c31\u90fd\u4ee5\u4e3a\u6709\u5e93\u5b58&#xff0c;\u8d85\u5356\u5c31\u662f\u8fd9\u4e48\u6765\u7684\u3002Lua \u811a\u672c\u5728 Redis \u91cc\u662f\u5355\u7ebf\u7a0b\u539f\u5b50\u6267\u884c\u7684&#xff0c;\u811a\u672c\u91cc\u5148 GET \u5e93\u5b58&#xff0c;\u5224\u65ad\u591f\u4e0d\u591f&#xff0c;\u591f\u5c31 DECRBY&#xff0c;\u4e0d\u591f\u8fd4\u56de\u552e\u7f44&#xff0c;\u6574\u4e2a\u8fc7\u7a0b\u4e0d\u4f1a\u88ab\u6253\u65ad\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;\u6263\u51cf\u6210\u529f\u540e\u4e0d\u76f4\u63a5\u5efa\u8ba2\u5355&#xff0c;\u800c\u662f\u53d1\u4e00\u6761 MQ \u6d88\u606f&#xff0c;\u8ba2\u5355\u670d\u52a1\u5f02\u6b65\u6d88\u8d39\u5efa\u5355\u3002\u56e0\u4e3a\u53ea\u6709 100 \u4e2a\u4eba\u80fd\u62a2\u5230&#xff0c;\u5efa\u5355\u538b\u529b\u4e0d\u5927&#xff0c;\u5f02\u6b65\u5316\u53ef\u4ee5\u8ba9\u79d2\u6740\u63a5\u53e3\u5feb\u901f\u8fd4\u56de\u2019\u6392\u961f\u4e2d\u2019&#xff0c;\u7528\u6237\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002&#034;<\/p>\n<p>\u7b2c\u4e09\u6bb5&#xff1a;\u4e3b\u52a8\u4ea4\u4ee3\u5751\u548c\u515c\u5e95&#xff08;1 \u5206\u949f&#xff09;\u2014\u2014\u8fd9\u6bb5\u6700\u52a0\u5206&#xff1a;<\/p>\n<p>&#034;\u8fd9\u91cc\u9762\u6709\u51e0\u4e2a\u5751\u662f\u6211\u4eec\u5b9e\u9645\u8e29\u8fc7\u6216\u8005\u63d0\u524d\u9632\u4e86\u7684&#xff1a;<\/p>\n<p>\u4e00\u662f Lua \u6263\u6210\u529f\u4e86\u4f46 MQ \u53d1\u9001\u5931\u8d25&#xff0c;\u8fd9\u4f1a\u5bfc\u81f4\u5e93\u5b58\u6263\u4e86\u4f46\u8ba2\u5355\u6ca1\u5efa&#xff0c;\u8868\u73b0\u4e3a\u5c11\u5356\u3002\u6211\u4eec\u7684\u5904\u7406\u662f MQ \u7528\u540c\u6b65\u53d1\u9001&#xff0c;\u53d1\u9001\u5931\u8d25\u5c31\u7acb\u523b\u628a Redis \u5e93\u5b58 INCRBY \u52a0\u56de\u53bb&#xff0c;\u7136\u540e\u8fd4\u56de\u7528\u6237\u2019\u8bf7\u91cd\u8bd5\u2019\u3002<\/p>\n<p>\u4e8c\u662f Redis \u4e07\u4e00\u6302\u4e86&#xff0c;\u79d2\u6740\u662f\u5f3a\u4f9d\u8d56 Redis \u7684&#xff0c;\u6211\u4eec\u7684\u7b56\u7565\u662f\u76f4\u63a5\u7194\u65ad\u8fd4\u56de\u2019\u6d3b\u52a8\u6682\u505c\u2019&#xff0c;\u7edd\u5bf9\u4e0d\u7a7f\u900f\u5230\u6570\u636e\u5e93\u2014\u2014\u56e0\u4e3a\u6570\u636e\u5e93\u90a3\u4efd\u5e93\u5b58\u6ca1\u6709\u5e76\u53d1\u4fdd\u62a4&#xff0c;\u653e 10 \u4e07\u4eba\u8fdb\u53bb\u5c31\u662f\u8d85\u5356\u4e8b\u6545\u3002\u5b81\u53ef\u6d3b\u52a8\u505c\u4e00\u4f1a\u513f&#xff0c;\u4e0d\u80fd\u8d44\u635f\u3002<\/p>\n<p>\u4e09\u662f\u6570\u636e\u5e93\u90a3\u8fb9\u6211\u4eec\u4ecd\u7136\u7559\u4e86\u6700\u540e\u4e00\u9053\u9632\u7ebf&#xff1a;\u5f02\u6b65\u6263\u51cf DB \u5e93\u5b58\u7684 SQL \u4e5f\u5e26 WHERE stock &gt;&#061; \u6570\u91cf \u6761\u4ef6\u3002\u5c31\u7b97\u4e0a\u6e38\u5168\u51fa\u4e86 bug&#xff0c;\u6570\u636e\u5e93\u5c42\u9762\u4e5f\u4e0d\u4f1a\u51fa\u73b0\u8d1f\u5e93\u5b58\u3002&#034;<\/p>\n<p>\u6536\u5c3e&#xff08;15 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u603b\u7ed3\u4e00\u4e0b\u5c31\u662f\u56db\u5c42&#xff1a;Redis \u625b\u5e76\u53d1\u3001Lua \u4fdd\u539f\u5b50\u3001MQ \u505a\u524a\u5cf0\u3001\u6570\u636e\u5e93\u515c\u5e95\u3002\u6bcf\u4e00\u5c42\u89e3\u51b3\u4e00\u4e2a\u95ee\u9898&#xff0c;\u54ea\u5c42\u6302\u4e86\u90fd\u6709\u4e0b\u4e00\u5c42\u63a5\u4f4f\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u6574\u4f53\u67b6\u6784&#xff08;\u5206\u5c42\u6f0f\u6597&#xff09;&#xff1a;<\/p>\n<p>\u7b2c 1 \u5c42&#xff1a;\u524d\u7aef\u9650\u6d41<br \/>\n  &#8211; \u6309\u94ae\u7f6e\u7070\u9632\u91cd\u590d\u70b9\u51fb<br \/>\n  &#8211; \u9a8c\u8bc1\u7801\/\u7b54\u9898\u62c9\u957f\u8bf7\u6c42\u65f6\u95f4<\/p>\n<p>\u7b2c 2 \u5c42&#xff1a;\u7f51\u5173\u9650\u6d41<br \/>\n  &#8211; \u6309\u7528\u6237 ID \u9650\u6d41&#xff08;\u6bcf\u4eba\u6bcf\u79d2 1 \u6b21&#xff09;<br \/>\n  &#8211; \u6309 IP \u9650\u6d41\u9632\u811a\u672c<\/p>\n<p>\u7b2c 3 \u5c42&#xff1a;\u5e93\u5b58\u9884\u70ed\u5230 Redis<br \/>\n  &#8211; \u6d3b\u52a8\u5f00\u59cb\u524d\u5c06\u5e93\u5b58\u52a0\u8f7d\u5230 Redis<br \/>\n  &#8211; \u7528 Lua \u811a\u672c&#034;\u5224\u65ad &#043; \u6263\u51cf&#034;\u539f\u5b50\u6267\u884c<\/p>\n<p>\u7b2c 4 \u5c42&#xff1a;\u5f02\u6b65\u4e0b\u5355<br \/>\n  &#8211; \u6263\u51cf\u6210\u529f \u2192 \u53d1\u9001 MQ \u6d88\u606f<br \/>\n  &#8211; \u6d88\u8d39\u8005\u5f02\u6b65\u521b\u5efa\u8ba2\u5355\u3001\u6263\u51cf DB \u5e93\u5b58<\/p>\n<p>\u7b2c 5 \u5c42&#xff1a;\u6570\u636e\u5e93\u515c\u5e95<br \/>\n  &#8211; UPDATE stock SET count &#061; count &#8211; 1 WHERE id &#061; ? AND count &gt; 0<\/p>\n<p>\u6838\u5fc3 Lua \u811a\u672c&#xff08;\u9010\u884c\u6ce8\u91ca\u7248&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; KEYS[1] &#061; seckill:stock:{skuId}<\/span><br \/>\n<span class=\"token comment\">&#8212; ARGV[1] &#061; \u6263\u51cf\u6570\u91cf&#xff08;\u901a\u5e38\u4e3a 1&#xff09;<\/span><br \/>\n<span class=\"token keyword\">local<\/span> stock <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;GET&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">if<\/span> stock <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">nil<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span>  <span class=\"token comment\">&#8212; \u5e93\u5b58\u672a\u521d\u59cb\u5316&#xff0c;\u8bf4\u660e\u9884\u70ed\u5931\u8d25&#xff0c;\u8d70\u964d\u7ea7<\/span><br \/>\n<span class=\"token keyword\">end<\/span><br \/>\n<span class=\"token keyword\">if<\/span> stock <span class=\"token operator\">&lt;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span>   <span class=\"token comment\">&#8212; \u5e93\u5b58\u4e0d\u8db3&#xff0c;\u5df2\u552e\u7f44<\/span><br \/>\n<span class=\"token keyword\">end<\/span><br \/>\nredis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;DECRBY&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span>       <span class=\"token comment\">&#8212; \u6263\u51cf\u6210\u529f<\/span><\/p>\n<p>\u8bbe\u8ba1\u610f\u56fe\u9010\u6761\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>stock &#061;&#061; nil \u5224\u65ad&#xff1a;\u9632\u5fa1\u6027\u68c0\u67e5\u3002\u9884\u70ed\u5931\u8d25\u6216 key \u88ab\u8bef\u5220\u65f6\u8fd4\u56de -1 \u8d70\u964d\u7ea7&#xff0c;\u800c\u4e0d\u662f\u628a nil \u5f53 0 \u8bef\u5224\u552e\u7f44\u3002<\/li>\n<li>stock &lt; ARGV[1] \u800c\u4e0d\u662f stock &lt;&#061; 0&#xff1a;\u652f\u6301\u4e00\u6b21\u6263\u591a\u4ef6&#xff08;\u7528\u6237\u4e70 2 \u4ef6&#xff09;&#xff0c;\u66f4\u901a\u7528\u3002<\/li>\n<li>\u7528 DECRBY \u800c\u4e0d\u662f DECR&#xff1a;\u540c\u6837\u4e3a\u4e86\u652f\u6301\u591a\u4ef6\u6263\u51cf\u3002<\/li>\n<li>\u6574\u4e2a\u811a\u672c\u5728 Redis \u5355\u7ebf\u7a0b\u4e2d\u539f\u5b50\u6267\u884c&#xff0c;\u4e0d\u5b58\u5728\u5e76\u53d1\u7a97\u53e3\u3002<\/li>\n<\/ul>\n<p>\u79d2\u6740\u63a5\u53e3\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token class-name\">Result<\/span> <span class=\"token function\">seckill<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Long<\/span> userId<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Long<\/span> skuId<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ 1. \u9650\u6d41&#xff1a;\u6bcf\u4eba\u6bcf\u79d2\u6700\u591a 1 \u6b21<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>rateLimiterService<span class=\"token punctuation\">.<\/span><span class=\"token function\">tryAcquire<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;seckill:limit:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> userId<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">fail<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u8bf7\u6c42\u8fc7\u4e8e\u9891\u7e41&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/ 2. Redis \u539f\u5b50\u6263\u51cf\u5e93\u5b58<\/span><br \/>\n    <span class=\"token class-name\">Long<\/span> result <span class=\"token operator\">&#061;<\/span> redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">execute<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">SECKILL_SCRIPT<\/span><span class=\"token punctuation\">,<\/span><br \/>\n            <span class=\"token class-name\">Collections<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">singletonList<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;seckill:stock:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> skuId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>result <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">||<\/span> result <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">fail<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6d3b\u52a8\u672a\u5f00\u59cb\u6216\u5df2\u6682\u505c&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u9884\u70ed\u5931\u8d25\u964d\u7ea7<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>result <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">fail<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5df2\u552e\u7f44&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/ 3. \u540c\u6b65\u53d1\u9001 MQ \u5f02\u6b65\u521b\u5efa\u8ba2\u5355&#xff0c;\u5931\u8d25\u5219\u56de\u8865\u5e93\u5b58<\/span><br \/>\n    <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        rocketMQProducer<span class=\"token punctuation\">.<\/span><span class=\"token function\">sendSync<\/span><span class=\"token punctuation\">(<\/span><span class=\"token constant\">TOPIC_ORDER<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token constant\">TAG_CREATE<\/span><span class=\"token punctuation\">,<\/span><br \/>\n                <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">SeckillOrder<\/span><span class=\"token punctuation\">(<\/span>userId<span class=\"token punctuation\">,<\/span> skuId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">catch<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Exception<\/span> e<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">opsForValue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">increment<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;seckill:stock:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> skuId<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">fail<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u7cfb\u7edf\u7e41\u5fd9&#xff0c;\u8bf7\u91cd\u8bd5&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">success<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u79d2\u6740\u6210\u529f&#xff0c;\u8ba2\u5355\u521b\u5efa\u4e2d&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>\u5f00\u5934\u4e00\u53e5&#034;\u6211\u5b9e\u9645\u505a\u8fc7&#034;\u76f4\u63a5\u62c9\u6ee1\u53ef\u4fe1\u5ea6&#xff1b;<\/li>\n<li>\u4e3b\u52a8\u8bb2\u5751&#xff08;MQ \u5931\u8d25\u3001Redis \u5b95\u673a&#xff09;\u662f\u5835\u8ffd\u95ee\u2014\u2014\u9762\u8bd5\u5b98\u60f3\u95ee\u7684\u4f60\u90fd\u5148\u8bf4\u4e86&#xff1b;<\/li>\n<li>&#034;\u5b81\u53ef\u505c\u6d3b\u52a8\u4e5f\u4e0d\u7a7f\u900f\u6570\u636e\u5e93&#034;\u8fd9\u79cd\u53d6\u820d\u5224\u65ad&#xff0c;\u662f\u533a\u5206&#034;\u80cc\u9898\u7684&#034;\u548c&#034;\u505a\u8fc7\u7684&#034;\u7684\u5173\u952e\u3002<\/li>\n<\/ul>\n<h4>&#x1f4d6; \u6211\u7684\u7ecf\u5386\u00b7\u8bdd\u672f\u6a21\u677f<\/h4>\n<p>\u201c\u6211\u4eec\u5f53\u65f6\u7b2c\u4e00\u6b21\u538b\u6d4b\u5c31\u53d1\u73b0\u4e86 MQ \u53d1\u9001\u5931\u8d25\u5bfc\u81f4\u5c11\u5356\u7684\u95ee\u9898\u2014\u2014\u538b\u6d4b\u5de5\u5177\u6a21\u62df\u4e86 MQ \u6296\u52a8&#xff0c;\u7ed3\u679c Redis \u5e93\u5b58\u6263\u4e86 30 \u4e2a&#xff0c;\u8ba2\u5355\u53ea\u5efa\u4e86 26 \u4e2a\u3002\u540e\u6765\u52a0\u4e86\u540c\u6b65\u53d1\u9001 &#043; \u5931\u8d25\u56de\u8865&#xff0c;\u518d\u538b\u5c31\u5bf9\u5f97\u4e0a\u4e86\u3002\u201d<\/p>\n<p>&#xff08;\u628a\u6570\u5b57\u6362\u6210\u4f60\u771f\u5b9e\u7ecf\u5386\u7684&#xff0c;\u54ea\u6015\u89c4\u6a21\u5c0f\u4e5f\u6ca1\u5173\u7cfb&#xff0c;\u7ec6\u8282\u771f\u5b9e\u6bd4\u89c4\u6a21\u5b8f\u5927\u66f4\u91cd\u8981\u3002&#xff09;<\/p>\n<hr \/>\n<h3>\u573a\u666f\u4e8c&#xff1a;\u8ba2\u5355\u53d6\u6d88\u540e\u5e93\u5b58\u600e\u4e48\u56de\u6eda&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u7528\u6237\u4e0b\u5355 30 \u5206\u949f\u6ca1\u652f\u4ed8&#xff0c;\u8ba2\u5355\u53d6\u6d88\u4e86&#xff0c;\u5e93\u5b58\u600e\u4e48\u52a0\u56de\u53bb&#xff1f;\u7528\u6237\u4e3b\u52a8\u53d6\u6d88\u5462&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u5f52\u7c7b&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u5e93\u5b58\u56de\u6eda\u5176\u5b9e\u6709\u4e09\u79cd\u89e6\u53d1\u573a\u666f&#xff1a;\u8d85\u65f6\u672a\u652f\u4ed8\u3001\u7528\u6237\u4e3b\u52a8\u53d6\u6d88\u3001\u652f\u4ed8\u5931\u8d25\u6216\u9000\u6b3e\u3002\u4e09\u79cd\u573a\u666f\u5165\u53e3\u4e0d\u540c&#xff0c;\u4f46\u56de\u6eda\u903b\u8f91\u662f\u540c\u4e00\u5957&#xff0c;\u6240\u4ee5\u6211\u4eec\u62bd\u4e86\u4e00\u4e2a\u7edf\u4e00\u7684\u56de\u6eda\u6d88\u8d39\u8005&#xff0c;\u4e0d\u540c\u573a\u666f\u90fd\u53d1\u540c\u4e00\u79cd MQ \u6d88\u606f&#xff0c;\u907f\u514d\u5199\u4e09\u4efd\u903b\u8f91\u51fa\u4e09\u79cd bug\u3002\u201d<\/p>\n<p>\u6838\u5fc3&#xff1a;\u5e42\u7b49&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u56de\u6eda\u6700\u6015\u7684\u662f\u91cd\u590d\u56de\u6eda\u3002\u6bd4\u5982\u5ef6\u8fdf\u6d88\u606f\u91cd\u8bd5\u4e86&#xff0c;\u6216\u8005\u7528\u6237\u70b9\u53d6\u6d88\u548c\u8d85\u65f6\u53d6\u6d88\u6070\u597d\u540c\u65f6\u89e6\u53d1\u2014\u2014\u5982\u679c\u6bcf\u6b21\u90fd\u65e0\u8111 INCRBY&#xff0c;\u5e93\u5b58\u5c31\u4f1a\u591a\u52a0&#xff0c;\u7b49\u4e8e\u51ed\u7a7a\u9020\u51fa\u4e86\u5e93\u5b58&#xff0c;\u6700\u540e\u5c31\u662f\u8d85\u5356\u3002<\/p>\n<p>\u6211\u4eec\u7684\u5e42\u7b49\u505a\u4e86\u4e24\u5c42&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u5c42\u662f\u8ba2\u5355\u72b6\u6001\u673a\u3002\u56de\u6eda\u524d\u5148\u67e5\u8ba2\u5355\u72b6\u6001&#xff0c;\u53ea\u6709\u2019\u5f85\u652f\u4ed8\u2019\u624d\u5904\u7406&#xff1b;\u66f4\u65b0\u72b6\u6001\u7528\u4e50\u89c2\u9501 UPDATE &#8230; WHERE status &#061; &#039;\u5f85\u652f\u4ed8&#039;&#xff0c;\u5f71\u54cd\u884c\u6570\u662f 0 \u5c31\u8bf4\u660e\u522b\u4eba\u5df2\u7ecf\u5904\u7406\u8fc7\u4e86&#xff0c;\u76f4\u63a5\u8fd4\u56de\u3002\u72b6\u6001\u662f\u4e1a\u52a1\u4e8b\u5b9e&#xff0c;\u4e0d\u4f1a\u4e22\u4e5f\u4e0d\u4f1a\u8fc7\u671f&#xff0c;\u8fd9\u662f\u6700\u53ef\u9760\u7684\u5e42\u7b49\u4f9d\u636e\u3002<\/p>\n<p>\u7b2c\u4e8c\u5c42\u662f Redis \u56de\u6eda\u6d41\u6c34 key\u3002\u56e0\u4e3a INCRBY \u672c\u8eab\u4e0d\u5e42\u7b49&#xff0c;\u6267\u884c\u4e24\u6b21\u5c31\u52a0\u4e24\u6b21\u3002\u6240\u4ee5\u6211\u4eec\u7528\u4e00\u6bb5 Lua&#xff1a;\u5148 EXISTS \u68c0\u67e5 rollback:log:{orderId} \u5b58\u4e0d\u5b58\u5728&#xff0c;\u4e0d\u5b58\u5728\u624d\u6267\u884c INCRBY \u5e76\u5199\u5165\u8fd9\u4e2a\u6807\u8bb0&#xff0c;\u6807\u8bb0\u4fdd\u7559 7 \u5929\u3002\u68c0\u67e5\u548c\u5199\u5165\u5728\u4e00\u6bb5\u811a\u672c\u91cc\u539f\u5b50\u5b8c\u6210&#xff0c;\u5e76\u53d1\u4e0b\u4e5f\u53ea\u6709\u4e00\u4e2a\u8bf7\u6c42\u80fd\u6210\u529f\u3002&#034;<\/p>\n<p>\u987a\u5e8f\u95ee\u9898&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8fd8\u6709\u4e2a\u7ec6\u8282\u662f\u5148\u56de\u6eda Redis \u518d\u56de\u6eda\u6570\u636e\u5e93\u3002\u56e0\u4e3a Redis \u662f\u7528\u6237\u5b9e\u9645\u770b\u5230\u7684\u2019\u53ef\u552e\u5e93\u5b58\u2019&#xff0c;\u5148\u6062\u590d\u5b83&#xff0c;\u5546\u54c1\u80fd\u5c3d\u5feb\u91cd\u65b0\u4e0a\u67b6\u53ef\u552e&#xff0c;\u5c11\u5356\u7684\u65f6\u95f4\u7a97\u53e3\u6700\u77ed\u3002\u6570\u636e\u5e93\u90a3\u8fb9\u9760 MQ \u91cd\u8bd5\u4fdd\u8bc1\u6700\u7ec8\u4f1a\u8865\u4e0a\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u4e09\u79cd\u89e6\u53d1\u573a\u666f\u5bf9\u7167&#xff1a;<\/p>\n<table>\n<tr>\u89e6\u53d1\u573a\u666f\u89e6\u53d1\u65f6\u673a\u89e6\u53d1\u65b9<\/tr>\n<tbody>\n<tr>\n<td>\u8d85\u65f6\u672a\u652f\u4ed8<\/td>\n<td>\u4e0b\u5355\u540e 30 \u5206\u949f<\/td>\n<td>\u5ef6\u8fdf\u961f\u5217\u6d88\u8d39\u8005<\/td>\n<\/tr>\n<tr>\n<td>\u7528\u6237\u4e3b\u52a8\u53d6\u6d88<\/td>\n<td>\u7528\u6237\u70b9\u51fb\u53d6\u6d88<\/td>\n<td>\u8ba2\u5355\u670d\u52a1<\/td>\n<\/tr>\n<tr>\n<td>\u652f\u4ed8\u5931\u8d25\/\u9000\u6b3e<\/td>\n<td>\u652f\u4ed8\u56de\u8c03\u901a\u77e5\u5931\u8d25<\/td>\n<td>\u652f\u4ed8\u670d\u52a1\/\u4ea4\u6613\u670d\u52a1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b8c\u6574\u56de\u6eda\u6d41\u7a0b&#xff1a;<\/p>\n<p>\u2460 \u6536\u5230\u56de\u6eda\u6d88\u606f&#xff08;\u542b orderId&#xff09;<br \/>\n\u2461 \u67e5\u8be2\u8ba2\u5355\u72b6\u6001<br \/>\n   &#8211; \u5df2\u662f&#034;\u5df2\u53d6\u6d88&#034; \u2192 \u5df2\u5904\u7406\u8fc7&#xff0c;\u76f4\u63a5\u8fd4\u56de&#xff08;\u5e42\u7b49&#xff09;<br \/>\n   &#8211; \u662f&#034;\u5f85\u652f\u4ed8&#034; \u2192 \u7ee7\u7eed<br \/>\n\u2462 \u66f4\u65b0\u8ba2\u5355\u72b6\u6001\u4e3a&#034;\u5df2\u53d6\u6d88&#034;&#xff08;\u4e50\u89c2\u9501&#xff1a;WHERE status &#061; &#039;\u5f85\u652f\u4ed8&#039;&#xff09;<br \/>\n   &#8211; \u5f71\u54cd\u884c\u6570 &#061; 0 \u2192 \u88ab\u5176\u4ed6\u7ebf\u7a0b\u5148\u5904\u7406\u4e86&#xff0c;\u8fd4\u56de&#xff08;\u5e42\u7b49&#xff09;<br \/>\n   &#8211; \u5f71\u54cd\u884c\u6570 &#061; 1 \u2192 \u7ee7\u7eed<br \/>\n\u2463 \u56de\u6eda Redis \u5e93\u5b58&#xff08;\u5e26\u5e42\u7b49\u68c0\u67e5\u7684 Lua&#xff09;<br \/>\n\u2464 \u56de\u6eda\u6570\u636e\u5e93\u5e93\u5b58&#xff1a;UPDATE inventory SET stock &#061; stock &#043; {\u6570\u91cf} WHERE sku_id &#061; ?<br \/>\n\u2465 \u5199\u5165\u5e93\u5b58\u6d41\u6c34&#xff08;\u7c7b\u578b&#061;\u56de\u6eda&#xff0c;\u5173\u8054 orderId&#xff09;<\/p>\n<p>Redis \u56de\u6eda\u5e42\u7b49 Lua \u811a\u672c&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; KEYS[1] &#061; seckill:stock:{skuId}<\/span><br \/>\n<span class=\"token comment\">&#8212; KEYS[2] &#061; rollback:log:{orderId}<\/span><br \/>\n<span class=\"token comment\">&#8212; ARGV[1] &#061; \u56de\u6eda\u6570\u91cf<\/span><br \/>\n<span class=\"token keyword\">if<\/span> redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;EXISTS&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span>  <span class=\"token comment\">&#8212; \u5df2\u56de\u6eda\u8fc7&#xff0c;\u5e42\u7b49\u8fd4\u56de<\/span><br \/>\n<span class=\"token keyword\">end<\/span><br \/>\nredis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;INCRBY&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\nredis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SET&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;EX&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">86400<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">7<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\">&#8212; \u6d41\u6c34\u6807\u8bb0\u4fdd\u7559 7 \u5929<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><\/p>\n<p>\u4e3a\u4ec0\u4e48\u7528\u6570\u636e\u5e93\u72b6\u6001\u673a\u505a\u5e42\u7b49\u800c\u4e0d\u662f Redis SETNX&#xff1a;<\/p>\n<ul>\n<li>Redis \u7684 SETNX \u6709 TTL&#xff0c;\u8fc7\u671f\u540e\u5e42\u7b49\u5931\u6548&#xff1b;\u8bbe\u5f88\u957f&#xff08;\u5982 7 \u5929&#xff09;\u53c8\u5360\u5185\u5b58\u3002<\/li>\n<li>\u8ba2\u5355\u72b6\u6001\u672c\u8eab\u5c31\u662f\u6700\u53ef\u9760\u7684\u5e42\u7b49\u4f9d\u636e\u2014\u2014\u72b6\u6001\u673a\u662f\u4e1a\u52a1\u4e8b\u5b9e&#xff0c;\u4e0d\u4f1a\u4e22\u3001\u4e0d\u4f1a\u8fc7\u671f\u3002<\/li>\n<li>\u6570\u636e\u5e93\u7684 UPDATE &#8230; WHERE status &#061; &#039;\u5f85\u652f\u4ed8&#039; \u5229\u7528\u884c\u9501\u5929\u7136\u9632\u5e76\u53d1&#xff0c;\u5f71\u54cd\u884c\u6570\u5c31\u662f\u5e42\u7b49\u5224\u65ad\u4f9d\u636e\u3002<\/li>\n<\/ul>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>&#034;\u4e09\u79cd\u573a\u666f\u4e00\u5957\u903b\u8f91&#034;\u4f53\u73b0\u5de5\u7a0b\u62bd\u8c61\u80fd\u529b&#xff1b;<\/li>\n<li>\u4e3b\u52a8\u6307\u51fa&#034;INCRBY \u4e0d\u5e42\u7b49&#034;\u8fd9\u4e2a\u7ec6\u8282&#xff0c;\u8bf4\u660e\u4f60\u771f\u5199\u8fc7\u8fd9\u6bb5\u4ee3\u7801\u2014\u2014\u6ca1\u5199\u8fc7\u7684\u4eba\u6839\u672c\u60f3\u4e0d\u5230\u8fd9\u4e00\u5c42\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u4e09&#xff1a;Redis \u5e93\u5b58\u548c\u6570\u636e\u5e93\u600e\u4e48\u4fdd\u6301\u4e00\u81f4&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u5e93\u5b58\u653e Redis \u91cc&#xff0c;\u90a3\u6570\u636e\u5e93\u7684\u5e93\u5b58\u600e\u4e48\u529e&#xff1f;\u4e24\u8fb9\u4e0d\u4e00\u81f4\u4e86\u600e\u4e48\u5904\u7406&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2~3 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u7ea0\u6b63\u4e00\u4e2a\u524d\u63d0&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u9996\u5148\u6211\u60f3\u6f84\u6e05\u4e00\u4e0b&#xff0c;\u5728\u6211\u4eec\u8bbe\u8ba1\u91cc&#xff0c;Redis \u5e93\u5b58\u548c\u6570\u636e\u5e93\u5e93\u5b58\u4e0d\u662f\u540c\u4e00\u4efd\u6570\u636e\u7684\u4e24\u4e2a\u526f\u672c&#xff0c;\u800c\u662f\u804c\u8d23\u4e0d\u540c\u7684\u4e24\u5c42&#xff1a;Redis \u662f\u2019\u53ef\u552e\u5e93\u5b58\u2019&#xff0c;\u8d1f\u8d23\u5b9e\u65f6\u6263\u51cf&#xff0c;\u625b\u5e76\u53d1&#xff1b;\u6570\u636e\u5e93\u662f\u2019\u8d26\u9762\u5e93\u5b58\u2019&#xff0c;\u8d1f\u8d23\u6301\u4e45\u5316\u8bb0\u5f55&#xff0c;\u4f9b\u5bf9\u8d26\u548c\u62a5\u8868\u7528\u3002\u6240\u4ee5\u76ee\u6807\u4e0d\u662f\u5f3a\u4e00\u81f4\u2014\u2014\u90a3\u662f\u5206\u5e03\u5f0f\u4e8b\u52a1\u7684\u4ee3\u4ef7\u2014\u2014\u800c\u662f\u6700\u7ec8\u4e00\u81f4&#xff0c;\u5141\u8bb8\u79d2\u7ea7\u5ef6\u8fdf&#xff0c;\u4f46\u4e0d\u5141\u8bb8\u6c38\u4e45\u5bf9\u4e0d\u4e0a\u3002\u201d<\/p>\n<p>\u6570\u636e\u6d41\u662f\u5355\u5411\u7684&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u6570\u636e\u6d41\u6211\u4eec\u8bbe\u8ba1\u6210\u5355\u5411&#xff1a;Redis \u2192 \u6570\u636e\u5e93&#xff0c;\u4e0d\u505a\u53cc\u5411\u540c\u6b65\u3002\u4e0b\u5355\u65f6 Redis \u5b9e\u65f6\u6263&#xff0c;\u7136\u540e\u53d1 MQ&#xff0c;\u6d88\u8d39\u8005\u5f02\u6b65\u6263\u6570\u636e\u5e93&#xff1b;\u53d6\u6d88\u65f6\u53cd\u8fc7\u6765\u52a0\u3002\u4e3a\u4ec0\u4e48\u4e0d\u505a\u53cc\u5411&#xff1f;\u56e0\u4e3a\u53cc\u5411\u540c\u6b65\u4f1a\u6709\u5faa\u73af\u66f4\u65b0\u95ee\u9898\u2014\u2014Redis \u6539\u4e86\u540c\u6b65\u5230 DB&#xff0c;DB \u7684\u53d8\u66f4\u53c8\u89e6\u53d1\u540c\u6b65\u56de Redis&#xff0c;\u4e00\u65e6\u6709 bug \u5c31\u662f\u6b7b\u5faa\u73af&#xff0c;\u800c\u4e14\u51b2\u7a81\u6ca1\u6cd5\u89e3\u3002\u5355\u5411\u6d41\u5929\u7136\u6ca1\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u6d88\u8d39\u8005\u6263\u6570\u636e\u5e93\u65f6\u6709\u4e24\u4e2a\u5173\u952e\u70b9&#xff1a;\u4e00\u662f\u5e42\u7b49&#xff0c;\u6d88\u606f\u91cc\u5e26\u6d41\u6c34\u53f7&#xff0c;\u6d88\u8d39\u524d\u5148\u67e5\u6d41\u6c34\u8868&#xff0c;\u5904\u7406\u8fc7\u7684\u76f4\u63a5\u8df3\u8fc7&#xff1b;\u4e8c\u662f SQL \u5e26 WHERE stock &gt;&#061; \u6570\u91cf \u6761\u4ef6&#xff0c;\u9632\u6b62\u6d88\u606f\u4e71\u5e8f&#xff08;\u6bd4\u5982\u56de\u6eda\u6d88\u606f\u5148\u5230\u3001\u6263\u51cf\u6d88\u606f\u540e\u5230&#xff09;\u628a\u5e93\u5b58\u6263\u6210\u8d1f\u6570\u3002&#034;<\/p>\n<p>\u4e0d\u4e00\u81f4\u600e\u4e48\u53d1\u73b0\u3001\u600e\u4e48\u4fee&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>\u201c\u5c31\u7b97\u673a\u5236\u518d\u5b8c\u5584&#xff0c;\u8fd8\u662f\u53ef\u80fd\u4e0d\u4e00\u81f4\u2014\u2014\u6d88\u8d39\u8005\u5b95\u673a\u3001\u6d88\u606f\u4e22\u5931\u90fd\u6709\u53ef\u80fd\u3002\u6240\u4ee5\u6211\u4eec\u6709\u4e2a\u5b9a\u65f6\u5bf9\u8d26\u4efb\u52a1&#xff0c;\u6bcf 5 \u5206\u949f\u8dd1\u4e00\u6b21&#xff1a;\u62ff Redis \u5e93\u5b58\u548c\u2019\u6570\u636e\u5e93\u5e93\u5b58\u51cf\u53bb\u5f85\u652f\u4ed8\u8ba2\u5355\u5360\u7528\u2019\u505a\u6bd4\u5bf9\u3002\u4e3a\u4ec0\u4e48\u8981\u51cf\u5f85\u652f\u4ed8\u5360\u7528&#xff1f;\u56e0\u4e3a\u5f85\u652f\u4ed8\u8ba2\u5355\u5728 Redis \u5df2\u7ecf\u6263\u4e86\u3001\u6570\u636e\u5e93\u8fd8\u6ca1\u6263&#xff0c;\u8fd9\u90e8\u5206\u5dee\u503c\u662f\u6b63\u5e38\u7684\u3002\u5982\u679c\u6bd4\u5bf9\u4e0b\u6765\u5dee\u503c\u8d85\u8fc7\u9608\u503c&#xff0c;\u5148\u544a\u8b66&#xff0c;\u786e\u8ba4\u662f\u5f02\u5e38\u5c31\u4ee5\u6570\u636e\u5e93\u4e3a\u51c6\u4fee\u6b63 Redis\u2014\u2014\u56e0\u4e3a\u6570\u636e\u5e93\u6709\u5b8c\u6574\u6d41\u6c34&#xff0c;\u53ef\u4ee5\u8ffd\u6eaf\u6bcf\u4e00\u7b14\u53d8\u52a8&#xff0c;\u800c Redis \u53ea\u662f\u52a0\u901f\u5c42&#xff0c;\u6570\u636e\u968f\u65f6\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u91cd\u5efa\u3002\u201d<\/p>\n<p>\u4e3b\u52a8\u5835\u4e00\u4e2a\u8ffd\u95ee&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u53ef\u80fd\u6709\u4eba\u4f1a\u60f3\u5230\u7528 Canal \u76d1\u542c binlog \u53cd\u5411\u540c\u6b65&#xff0c;\u8fd9\u4e2a\u65b9\u6848\u5728\u5546\u54c1\u4fe1\u606f\u90a3\u79cd\u2019DB \u4e3a\u4e3b\u3001\u7f13\u5b58\u4e3a\u4ece\u2019\u7684\u573a\u666f\u5f88\u5408\u9002&#xff0c;\u4f46\u5e93\u5b58\u573a\u666f\u4e0d\u884c\u2014\u2014\u5e93\u5b58\u7684\u5199\u5165\u65b9\u5411\u662f Redis \u5728\u524d&#xff0c;\u7528 Canal \u53cd\u5411\u540c\u6b65\u4f1a\u5f62\u6210\u5faa\u73af&#xff0c;\u800c\u4e14 Canal \u53ea\u80fd\u505a\u8d4b\u503c\u4e0d\u80fd\u505a\u539f\u5b50\u6263\u51cf&#xff0c;\u5e76\u53d1\u4e0b\u4f1a\u4e92\u76f8\u8986\u76d6\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u4e24\u5c42\u5e93\u5b58\u7684\u804c\u8d23\u5b9a\u4f4d&#xff1a;<\/p>\n<table>\n<tr>\u5c42\u804c\u8d23\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>Redis \u5e93\u5b58<\/td>\n<td>\u53ef\u552e\u5e93\u5b58&#xff08;\u5b9e\u65f6\u6263\u51cf&#xff09;<\/td>\n<td>\u9ad8\u9891\u8bfb\u5199&#xff0c;\u5141\u8bb8\u77ed\u6682\u4e0d\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td>DB \u5e93\u5b58<\/td>\n<td>\u8d26\u9762\u5e93\u5b58&#xff08;\u6301\u4e45\u5316\u8bb0\u5f55&#xff09;<\/td>\n<td>\u4f4e\u9891\u5199\u5165&#xff0c;\u8981\u6c42\u6700\u7ec8\u4e00\u81f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5355\u5411\u6570\u636e\u6d41&#xff1a;<\/p>\n<p>\u4e0b\u5355\u65f6&#xff1a;Redis \u6263\u51cf&#xff08;\u5b9e\u65f6&#xff09;\u2192 \u53d1 MQ \u2192 \u6d88\u8d39\u8005\u6263\u51cf DB&#xff08;\u5f02\u6b65&#xff09;<br \/>\n\u53d6\u6d88\u65f6&#xff1a;Redis \u56de\u6eda&#xff08;\u5b9e\u65f6&#xff09;\u2192 \u53d1 MQ \u2192 \u6d88\u8d39\u8005\u56de\u6eda DB&#xff08;\u5f02\u6b65&#xff09;<\/p>\n<p>\u5f02\u6b65\u843d\u5e93\u6d88\u8d39\u8005\u903b\u8f91&#xff1a;<\/p>\n<p>\u2460 \u6d88\u8d39 MQ \u6d88\u606f&#xff08;\u542b orderId\u3001skuId\u3001\u6570\u91cf\u3001\u6d41\u6c34\u53f7&#xff09;<br \/>\n\u2461 \u5e42\u7b49\u68c0\u67e5&#xff1a;\u67e5\u8be2\u6d41\u6c34\u8868\u662f\u5426\u5df2\u6709\u8be5\u6d41\u6c34\u53f7<br \/>\n   &#8211; \u6709 \u2192 \u8df3\u8fc7<br \/>\n   &#8211; \u65e0 \u2192 \u7ee7\u7eed<br \/>\n\u2462 \u6267\u884c DB \u6263\u51cf&#xff1a;<br \/>\n   UPDATE inventory SET stock &#061; stock &#8211; #{qty}<br \/>\n   WHERE sku_id &#061; #{skuId} AND stock &gt;&#061; #{qty}<br \/>\n\u2463 \u68c0\u67e5\u5f71\u54cd\u884c\u6570&#xff1a;<br \/>\n   &#8211; &#061; 1 \u2192 \u6263\u51cf\u6210\u529f&#xff0c;\u5199\u5165\u6d41\u6c34<br \/>\n   &#8211; &#061; 0 \u2192 \u5e93\u5b58\u4e0d\u8db3&#xff08;\u5f02\u5e38\u60c5\u51b5&#xff09;&#xff0c;\u89e6\u53d1\u544a\u8b66 &#043; \u8865\u507f<br \/>\n\u2464 \u5199\u5165\u5e93\u5b58\u6d41\u6c34\u8868&#xff08;\u7c7b\u578b&#061;\u6263\u51cf&#xff0c;\u5173\u8054 orderId&#xff09;<\/p>\n<p>\u4e3a\u4ec0\u4e48 DB \u6263\u51cf\u8fd8\u8981\u52a0 stock &gt;&#061; qty \u6761\u4ef6&#xff08;\u9632\u5fa1\u6027\u7f16\u7a0b&#xff09;&#xff1a;<\/p>\n<ul>\n<li>MQ \u6d88\u606f\u4e71\u5e8f&#xff08;\u56de\u6eda\u6d88\u606f\u5148\u5230&#xff0c;\u6263\u51cf\u6d88\u606f\u540e\u5230&#xff09;&#xff0c;\u4e0d\u52a0\u6761\u4ef6\u4f1a\u5bfc\u81f4\u5e93\u5b58\u4e3a\u8d1f\u3002<\/li>\n<li>\u6709 bug \u5bfc\u81f4\u91cd\u590d\u6263\u51cf\u6d88\u606f&#xff08;\u5e42\u7b49\u5931\u6548\u7684\u6781\u7aef\u60c5\u51b5&#xff09;&#xff0c;\u8fd9\u4e2a\u6761\u4ef6\u662f\u6700\u540e\u4e00\u9053\u9632\u7ebf\u3002<\/li>\n<li>\u6570\u636e\u5e93\u662f\u6700\u7ec8\u9632\u7ebf&#xff0c;\u5373\u4f7f\u4e0a\u6e38\u5168\u9519&#xff0c;\u8fd9\u91cc\u4e5f\u4e0d\u80fd\u8d85\u5356\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528 Canal&#xff1a;<\/p>\n<ul>\n<li>\u5e93\u5b58\u7684\u5199\u5165\u65b9\u5411\u662f Redis \u2192 DB&#xff08;\u5148\u6263 Redis&#xff09;&#xff0c;Canal \u53cd\u5411\u540c\u6b65\u56de Redis \u4f1a\u5f62\u6210\u5faa\u73af\u3002<\/li>\n<li>\u5e93\u5b58\u9700\u8981\u539f\u5b50\u6263\u51cf&#xff0c;Canal \u53ea\u80fd\u505a&#034;\u8d4b\u503c&#034;&#xff08;SET stock &#061; 100&#xff09;&#xff0c;\u65e0\u6cd5\u505a&#034;\u6263\u51cf&#034;&#xff08;DECR&#xff09;&#xff0c;\u5e76\u53d1\u4e0b\u4f1a\u8986\u76d6\u3002<\/li>\n<li>Canal \u9002\u5408&#034;DB \u662f\u4e3b&#xff0c;\u7f13\u5b58\u662f\u4ece&#034;\u7684\u573a\u666f&#xff08;\u5982\u5546\u54c1\u4fe1\u606f&#xff09;&#xff0c;\u4e0d\u9002\u5408&#034;Redis \u662f\u4e3b&#xff0c;DB \u662f\u4ece&#034;\u7684\u5e93\u5b58\u573a\u666f\u3002<\/li>\n<\/ul>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>\u5f00\u5934&#034;\u6f84\u6e05\u524d\u63d0&#034;\u662f\u9ad8\u7ea7\u6280\u5de7&#xff1a;\u628a\u95ee\u9898\u4ece&#034;\u600e\u4e48\u505a\u5f3a\u4e00\u81f4&#034;\u91cd\u65b0\u5b9a\u4e49\u4e3a&#034;\u4e24\u5c42\u804c\u8d23 &#043; \u6700\u7ec8\u4e00\u81f4&#034;&#xff0c;\u76f4\u63a5\u638c\u63a7\u4e86\u7b54\u9898\u6846\u67b6&#xff1b;<\/li>\n<li>\u5bf9\u8d26\u7b49\u5f0f&#xff08;Redis &#061; DB &#8211; \u5f85\u652f\u4ed8\u5360\u7528&#xff09;\u8bb2\u5f97\u51fa\u6765&#xff0c;\u8bf4\u660e\u771f\u505a\u8fc7\u5bf9\u8d26\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u56db&#xff1a;\u5e93\u5b58\u6d41\u6c34\u600e\u4e48\u8bbe\u8ba1&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u5e93\u5b58\u6bcf\u6b21\u53d8\u52a8\u90fd\u8981\u6709\u8bb0\u5f55\u5427&#xff1f;\u6d41\u6c34\u600e\u4e48\u8bbe\u8ba1&#xff1f;\u600e\u4e48\u4fdd\u8bc1\u4e0d\u91cd\u4e0d\u6f0f&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u8bf4\u6d41\u6c34\u7684\u4ef7\u503c&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6d41\u6c34\u8868\u6211\u4eec\u53eb inventory_log&#xff0c;\u5b83\u6709\u4e09\u4e2a\u4f5c\u7528&#xff1a;\u5bf9\u8d26\u7684\u4f9d\u636e\u3001\u51fa\u95ee\u9898\u65f6\u8ffd\u6eaf\u7684\u7ebf\u7d22\u3001\u8fd8\u6709\u5e42\u7b49\u7684\u5929\u7136\u94a5\u5319\u3002\u53ef\u4ee5\u8bf4\u6574\u4e2a\u5e93\u5b58\u7cfb\u7edf\u7684\u4e00\u81f4\u6027&#xff0c;\u6700\u540e\u90fd\u538b\u5728\u8fd9\u5f20\u8868\u4e0a\u3002\u201d<\/p>\n<p>\u8868\u8bbe\u8ba1\u8bb2\u91cd\u70b9&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8868\u91cc\u6709\u51e0\u4e2a\u5173\u952e\u5b57\u6bb5&#xff1a;\u6d41\u6c34\u53f7 log_no \u52a0\u4e86\u552f\u4e00\u7d22\u5f15&#xff0c;\u8fd9\u662f\u5e42\u7b49\u7684\u6838\u5fc3&#xff1b;before_stock \u548c after_stock \u8bb0\u5f55\u53d8\u52a8\u524d\u540e\u7684\u5e93\u5b58\u5feb\u7167&#xff0c;\u8fd9\u6837\u5bf9\u8d26\u7684\u65f6\u5019\u53ef\u4ee5\u94fe\u5f0f\u6821\u9a8c\u2014\u2014\u4e0a\u4e00\u6761\u7684 after \u5e94\u8be5\u7b49\u4e8e\u4e0b\u4e00\u6761\u7684 before&#xff0c;\u94fe\u6761\u65ad\u4e86\u5c31\u8bf4\u660e\u6709\u6d41\u6c34\u7f3a\u5931&#xff1b;type \u533a\u5206\u6263\u51cf\u3001\u56de\u6eda\u3001\u5165\u5e93\u3001\u51fa\u5e93&#xff1b;order_id \u5173\u8054\u8ba2\u5355&#xff0c;\u5165\u5e93\u8fd9\u79cd\u6ca1\u6709\u8ba2\u5355\u7684\u5c31\u7559\u7a7a\u3002\u201d<\/p>\n<p>&#034;\u4e0d\u91cd&#034;\u4e09\u5c42\u4fdd\u969c&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u4e0d\u91cd\u9760\u4e09\u5c42&#xff1a;\u7b2c\u4e00\u5c42\u662f\u4e1a\u52a1\u524d\u7f6e\u68c0\u67e5&#xff0c;\u6bd4\u5982\u8ba2\u5355\u72b6\u6001\u673a\u4fdd\u8bc1\u540c\u4e00\u8ba2\u5355\u4e0d\u4f1a\u89e6\u53d1\u4e24\u6b21\u56de\u6eda&#xff0c;\u8fd9\u5c42\u80fd\u6321\u4f4f 99% \u7684\u91cd\u590d&#xff1b;\u7b2c\u4e8c\u5c42\u662f\u6d88\u8d39\u8005\u63d2\u5165\u524d\u5148\u67e5\u6d41\u6c34\u53f7&#xff0c;\u5b58\u5728\u5c31\u8df3\u8fc7&#xff0c;\u8fd9\u5c42\u662f\u6027\u80fd\u4f18\u5316&#xff0c;\u907f\u514d\u8d70\u5230\u5f02\u5e38\u8def\u5f84&#xff1b;\u7b2c\u4e09\u5c42\u662f\u6570\u636e\u5e93\u552f\u4e00\u7d22\u5f15&#xff0c;\u5c31\u7b97\u524d\u4e24\u5c42\u90fd\u5931\u6548&#xff0c;\u63d2\u5165\u91cd\u590d\u6d41\u6c34\u53f7\u4f1a\u76f4\u63a5\u62a5 Duplicate entry&#xff0c;\u8fd9\u662f\u6700\u7ec8\u9632\u7ebf\u3002\u4e09\u5c42\u7684\u5173\u7cfb\u662f\u5c42\u5c42\u515c\u5e95&#xff0c;\u8d8a\u5f80\u540e\u8d8a\u53ef\u9760\u4f46\u8d8a\u5c11\u89e6\u53d1\u3002\u201d<\/p>\n<p>&#034;\u4e0d\u6f0f&#034;\u4e24\u4e2a\u673a\u5236&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u4e0d\u6f0f\u9760\u4e24\u4e2a\u673a\u5236&#xff1a;\u4e00\u662f\u5e93\u5b58\u53d8\u66f4\u548c\u6d41\u6c34\u5199\u5165\u5728\u540c\u4e00\u4e2a\u6570\u636e\u5e93\u4e8b\u52a1\u91cc&#xff0c;\u8981\u4e48\u90fd\u6210\u529f\u8981\u4e48\u90fd\u56de\u6eda&#xff0c;\u7edd\u4e0d\u4f1a\u51fa\u73b0\u2019\u5e93\u5b58\u6263\u4e86\u6d41\u6c34\u6ca1\u8bb0\u2019&#xff1b;\u4e8c\u662f MQ \u6d88\u8d39\u5931\u8d25\u4f1a\u91cd\u8bd5&#xff0c;\u914d\u5408\u5e42\u7b49&#xff0c;\u91cd\u8bd5\u53ea\u4f1a\u8865\u6f0f\u4e0d\u4f1a\u91cd\u590d\u3002\u6781\u7aef\u60c5\u51b5\u6bd4\u5982\u6570\u636e\u5e93\u4e3b\u4ece\u5207\u6362\u4e22\u4e86\u51e0\u6761&#xff0c;\u5bf9\u8d26\u4efb\u52a1\u4f1a\u53d1\u73b0\u5e93\u5b58\u548c\u6d41\u6c34\u91cd\u7b97\u503c\u5bf9\u4e0d\u4e0a&#xff0c;\u89e6\u53d1\u544a\u8b66\u4eba\u5de5\u4ecb\u5165\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u6d41\u6c34\u8868\u7ed3\u6784&#xff1a;<\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> inventory_log <span class=\"token punctuation\">(<\/span><br \/>\n    id          <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    log_no      <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">64<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">UNIQUE<\/span><span class=\"token punctuation\">,<\/span>  <span class=\"token comment\">&#8212; \u6d41\u6c34\u53f7&#xff08;\u5168\u5c40\u552f\u4e00&#xff0c;\u5e42\u7b49\u952e&#xff09;<\/span><br \/>\n    sku_id      <span class=\"token keyword\">BIGINT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>              <span class=\"token comment\">&#8212; \u5546\u54c1 SKU<\/span><br \/>\n    warehouse_id <span class=\"token keyword\">BIGINT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>             <span class=\"token comment\">&#8212; \u4ed3\u5e93<\/span><br \/>\n    <span class=\"token keyword\">type<\/span>        <span class=\"token keyword\">TINYINT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>             <span class=\"token comment\">&#8212; \u7c7b\u578b&#xff1a;1\u6263\u51cf 2\u56de\u6eda 3\u5165\u5e93 4\u51fa\u5e93<\/span><br \/>\n    quantity    <span class=\"token keyword\">INT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">&#8212; \u53d8\u52a8\u6570\u91cf&#xff08;\u6b63\u6570&#xff09;<\/span><br \/>\n    before_stock <span class=\"token keyword\">INT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">&#8212; \u53d8\u52a8\u524d\u5e93\u5b58<\/span><br \/>\n    after_stock  <span class=\"token keyword\">INT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span>                <span class=\"token comment\">&#8212; \u53d8\u52a8\u540e\u5e93\u5b58<\/span><br \/>\n    order_id    <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">64<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>                  <span class=\"token comment\">&#8212; \u5173\u8054\u8ba2\u5355\u53f7&#xff08;\u53ef\u7a7a&#xff0c;\u5165\u5e93\u65e0\u8ba2\u5355&#xff09;<\/span><br \/>\n    operator    <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">64<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>                  <span class=\"token comment\">&#8212; \u64cd\u4f5c\u4eba\/\u7cfb\u7edf\u6807\u8bc6<\/span><br \/>\n    remark      <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">256<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span>                 <span class=\"token comment\">&#8212; \u5907\u6ce8<\/span><br \/>\n    create_time <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">INDEX<\/span> idx_sku_time <span class=\"token punctuation\">(<\/span>sku_id<span class=\"token punctuation\">,<\/span> create_time<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n    <span class=\"token keyword\">INDEX<\/span> idx_order <span class=\"token punctuation\">(<\/span>order_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CHARSET<\/span><span class=\"token operator\">&#061;<\/span>utf8mb4<span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8bbe\u8ba1\u8981\u70b9&#xff1a;<\/p>\n<ul>\n<li>log_no \u552f\u4e00\u7d22\u5f15&#xff1a;\u5e42\u7b49\u7684\u6838\u5fc3\u3002\u6bcf\u6b21\u64cd\u4f5c\u751f\u6210\u5168\u5c40\u552f\u4e00\u6d41\u6c34\u53f7&#xff0c;\u63d2\u5165\u524d\u9760\u552f\u4e00\u7d22\u5f15\u9632\u91cd\u3002<\/li>\n<li>before_stock \/ after_stock&#xff1a;\u53d8\u52a8\u524d\u540e\u5feb\u7167&#xff0c;\u5bf9\u8d26\u65f6\u94fe\u5f0f\u6821\u9a8c&#xff08;\u4e0a\u4e00\u6761\u7684 after \u5e94\u7b49\u4e8e\u4e0b\u4e00\u6761\u7684 before&#xff09;\u3002<\/li>\n<li>type \u679a\u4e3e&#xff1a;\u533a\u5206\u64cd\u4f5c\u7c7b\u578b&#xff0c;\u4fbf\u4e8e\u7edf\u8ba1\u548c\u7b5b\u9009\u3002<\/li>\n<li>order_id \u53ef\u7a7a&#xff1a;\u5165\u5e93\u3001\u76d8\u70b9\u7b49\u64cd\u4f5c\u6ca1\u6709\u8ba2\u5355\u5173\u8054\u3002<\/li>\n<li>\u7d22\u5f15\u8bbe\u8ba1&#xff1a;idx_sku_time \u652f\u6301&#034;\u67e5\u67d0\u5546\u54c1\u67d0\u65f6\u95f4\u6bb5\u7684\u6d41\u6c34&#034;&#xff1b;idx_order \u652f\u6301&#034;\u67e5\u67d0\u8ba2\u5355\u7684\u6240\u6709\u5e93\u5b58\u64cd\u4f5c&#034;\u3002<\/li>\n<\/ul>\n<p>\u6d41\u6c34\u53f7\u751f\u6210\u7b56\u7565&#xff1a;<\/p>\n<p>\u683c\u5f0f&#xff1a;{\u7c7b\u578b\u7801}{\u65e5\u671f}{\u96ea\u82b1ID\u540e12\u4f4d}<br \/>\n\u793a\u4f8b&#xff1a;DJ20240101123456789012<br \/>\n      \u2191\u6263\u51cf  \u2191\u65e5\u671f    \u2191\u552f\u4e00\u5e8f\u5217<\/p>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528\u6570\u636e\u5e93\u81ea\u589e ID&#xff1a;<\/p>\n<ul>\n<li>\u81ea\u589e ID \u5728\u5206\u5e93\u5206\u8868\u540e\u4f1a\u51b2\u7a81&#xff1b;<\/li>\n<li>\u81ea\u589e ID \u9700\u8981\u63d2\u5165\u540e\u624d\u77e5\u9053&#xff0c;\u65e0\u6cd5\u5728\u63d2\u5165\u524d\u505a\u5e42\u7b49\u5224\u65ad&#xff1b;<\/li>\n<li>\u4e1a\u52a1\u6d41\u6c34\u53f7\u53ef\u4ee5\u643a\u5e26\u8bed\u4e49&#xff08;\u7c7b\u578b\u3001\u65e5\u671f&#xff09;&#xff0c;\u4fbf\u4e8e\u4eba\u5de5\u6392\u67e5\u3002<\/li>\n<\/ul>\n<p>\u4e8b\u52a1\u7ed1\u5b9a\u4ee3\u7801&#xff08;\u4e0d\u6f0f\u7684\u5173\u952e&#xff09;&#xff1a;<\/p>\n<p><span class=\"token annotation punctuation\">&#064;Transactional<\/span><span class=\"token punctuation\">(<\/span>rollbackFor <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Exception<\/span><span class=\"token punctuation\">.<\/span><span class=\"token keyword\">class<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">deductStock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Long<\/span> skuId<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> qty<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span> logNo<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span> orderId<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ 1. \u6263\u51cf\u5e93\u5b58<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rows <span class=\"token operator\">&#061;<\/span> inventoryDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">deduct<\/span><span class=\"token punctuation\">(<\/span>skuId<span class=\"token punctuation\">,<\/span> qty<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>rows <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">throw<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">BusinessException<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u5e93\u5b58\u4e0d\u8db3&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ 2. \u5199\u5165\u6d41\u6c34&#xff08;\u540c\u4e00\u4e8b\u52a1&#xff09;<\/span><br \/>\n    <span class=\"token class-name\">InventoryLog<\/span> log <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">buildLog<\/span><span class=\"token punctuation\">(<\/span>skuId<span class=\"token punctuation\">,<\/span> qty<span class=\"token punctuation\">,<\/span> logNo<span class=\"token punctuation\">,<\/span> orderId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    inventoryLogDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">insert<\/span><span class=\"token punctuation\">(<\/span>log<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u6d41\u6c34\u7684\u6027\u80fd\u8003\u91cf&#xff1a;<\/p>\n<ul>\n<li>\u6309\u6708\u5206\u8868&#xff1a;\u6d41\u6c34\u53ea\u589e\u4e0d\u6539\u4e0d\u5220&#xff0c;\u5386\u53f2\u6570\u636e\u67e5\u8be2\u5c11&#xff0c;\u6309\u6708\u5206\u8868&#xff08;inventory_log_202401&#xff09;\u907f\u514d\u5355\u8868\u8fc7\u5927\u3002<\/li>\n<li>\u5f52\u6863\u7b56\u7565&#xff1a;\u8d85\u8fc7 6 \u4e2a\u6708\u7684\u6d41\u6c34\u5f52\u6863\u5230\u51b7\u5b58\u50a8&#xff08;\u5982 Hive&#xff09;&#xff0c;\u4e3b\u8868\u53ea\u4fdd\u7559\u8fd1\u671f\u6570\u636e\u3002<\/li>\n<li>\u5f02\u6b65\u6279\u91cf&#xff08;\u6027\u80fd\u6781\u654f\u611f\u573a\u666f&#xff09;&#xff1a;\u5148\u5199\u5185\u5b58\u961f\u5217&#xff0c;\u5b9a\u65f6\u6279\u91cf INSERT&#xff0c;\u4ee3\u4ef7\u662f\u6781\u7aef\u60c5\u51b5\u53ef\u80fd\u4e22\u51e0\u6761&#xff08;\u9760\u5bf9\u8d26\u8865\u507f&#xff09;\u3002<\/li>\n<\/ul>\n<h4>&#x1f4d6; \u6211\u7684\u7ecf\u5386\u00b7\u8bdd\u672f\u6a21\u677f<\/h4>\n<p>\u201c\u6d41\u6c34\u53f7\u6211\u4eec\u6700\u65e9\u7528\u7684\u662f\u6570\u636e\u5e93\u81ea\u589e ID&#xff0c;\u540e\u6765\u53d1\u73b0\u4e24\u4e2a\u95ee\u9898&#xff1a;\u5206\u5e93\u5206\u8868\u540e\u4f1a\u51b2\u7a81&#xff0c;\u800c\u4e14\u81ea\u589e ID \u8981\u63d2\u5165\u4e4b\u540e\u624d\u77e5\u9053&#xff0c;\u6ca1\u6cd5\u5728\u63d2\u5165\u524d\u505a\u5e42\u7b49\u5224\u65ad\u3002\u540e\u6765\u6539\u6210\u4e86\u2019\u7c7b\u578b\u7801 &#043; \u65e5\u671f &#043; \u96ea\u82b1 ID\u2019\u7684\u683c\u5f0f&#xff0c;\u6bd4\u5982 DJ20240101 \u5f00\u5934\u5c31\u662f\u6263\u51cf\u6d41\u6c34&#xff0c;\u6392\u67e5\u95ee\u9898\u65f6\u8089\u773c\u5c31\u80fd\u5206\u8fa8\u7c7b\u578b&#xff0c;\u633a\u5b9e\u7528\u7684\u3002\u201d<\/p>\n<hr \/>\n<h3>\u573a\u666f\u4e94&#xff1a;\u652f\u4ed8\u56de\u8c03\u91cd\u590d\u5230\u8fbe\u600e\u4e48\u5e42\u7b49&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u652f\u4ed8\u7f51\u5173\u7684\u56de\u8c03\u53ef\u80fd\u91cd\u590d\u901a\u77e5&#xff0c;\u7f51\u7edc\u8d85\u65f6\u540e\u5b83\u4f1a\u91cd\u8bd5\u3002\u4f60\u600e\u4e48\u4fdd\u8bc1\u4e0d\u91cd\u590d\u5904\u7406&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u63cf\u8ff0\u95ee\u9898\u7684\u771f\u5b9e\u6837\u5b50&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8fd9\u4e2a\u95ee\u9898\u5f88\u5178\u578b&#xff1a;\u6211\u4eec\u5904\u7406\u5b8c\u56de\u8c03\u3001\u8fd4\u56de\u4e86 SUCCESS&#xff0c;\u4f46\u54cd\u5e94\u5728\u7f51\u7edc\u4e0a\u4e22\u4e86&#xff0c;\u652f\u4ed8\u7f51\u5173\u6ca1\u6536\u5230\u786e\u8ba4&#xff0c;\u5c31\u4f1a\u91cd\u53d1\u540c\u4e00\u6761\u56de\u8c03\u3002\u66f4\u72e0\u7684\u662f\u6709\u4e9b\u7f51\u5173\u6709\u5bf9\u8d26\u8865\u5355\u673a\u5236&#xff0c;T&#043;1 \u8fd8\u4f1a\u518d\u53d1\u4e00\u904d\u4f60\u65e9\u5c31\u5904\u7406\u8fc7\u7684\u56de\u8c03\u3002\u6240\u4ee5\u5e42\u7b49\u4e0d\u662f\u53ef\u9009\u9879&#xff0c;\u662f\u5fc5\u9009\u9879\u3002\u201d<\/p>\n<p>\u5e42\u7b49\u952e &#043; \u53cc\u91cd\u68c0\u67e5&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u5e42\u7b49\u7684\u524d\u63d0\u662f\u80fd\u8bc6\u522b\u2019\u8fd9\u662f\u540c\u4e00\u6b21\u64cd\u4f5c\u2019&#xff0c;\u652f\u4ed8\u56de\u8c03\u91cc\u7684\u652f\u4ed8\u6d41\u6c34\u53f7\u5c31\u662f\u5929\u7136\u7684\u5e42\u7b49\u952e\u2014\u2014\u540c\u4e00\u6b21\u652f\u4ed8\u4e0d\u7ba1\u56de\u8c03\u591a\u5c11\u6b21&#xff0c;\u6d41\u6c34\u53f7\u4e0d\u53d8\u3002<\/p>\n<p>\u6211\u4eec\u7684\u5904\u7406\u6d41\u7a0b\u662f&#xff1a;\u5148\u9a8c\u7b7e\u9632\u4f2a\u9020&#xff0c;\u7136\u540e\u67e5 Redis \u7684 pay:done:{\u6d41\u6c34\u53f7} \u6807\u8bb0&#xff0c;\u5b58\u5728\u5c31\u76f4\u63a5\u8fd4\u56de SUCCESS\u2014\u2014\u8fd9\u662f\u5feb\u901f\u8def\u5f84&#xff0c;99% \u7684\u91cd\u590d\u56de\u8c03\u5728\u8fd9\u91cc\u5c31\u88ab\u62e6\u6389\u4e86&#xff0c;\u4e0d\u7528\u78b0\u6570\u636e\u5e93\u3002\u6807\u8bb0\u4e0d\u5b58\u5728\u5c31\u8d70\u6b63\u5e38\u6d41\u7a0b&#xff1a;\u6821\u9a8c\u91d1\u989d\u3001\u66f4\u65b0\u8ba2\u5355\u72b6\u6001\u3001\u6267\u884c\u540e\u7eed\u4e1a\u52a1\u3001\u6700\u540e\u5199\u5165\u5e42\u7b49\u6807\u8bb0\u3002<\/p>\n<p>\u4f46 Redis \u6807\u8bb0\u4e0d\u662f\u6700\u7ec8\u4fdd\u969c\u2014\u2014\u4e07\u4e00 Redis \u6302\u4e86\u6216\u8005 key \u8fc7\u671f\u4e86\u5462&#xff1f;\u6240\u4ee5\u8ba2\u5355\u72b6\u6001\u66f4\u65b0\u7528\u7684\u662f\u4e50\u89c2\u9501&#xff1a;UPDATE order SET status&#061;&#039;\u5df2\u652f\u4ed8&#039; WHERE order_id&#061;? AND status&#061;&#039;\u5f85\u652f\u4ed8&#039;&#xff0c;\u5f71\u54cd\u884c\u6570\u4e3a 0 \u5c31\u8bf4\u660e\u5df2\u7ecf\u5904\u7406\u8fc7\u4e86&#xff0c;\u76f4\u63a5\u8fd4\u56de\u6210\u529f\u3002Redis \u7ba1\u6027\u80fd&#xff0c;\u6570\u636e\u5e93\u7ba1\u6b63\u786e\u6027&#xff0c;\u4e24\u5c42\u7f3a\u4e00\u4e0d\u53ef\u3002&#034;<\/p>\n<p>\u4e24\u4e2a\u5bb9\u6613\u88ab\u5ffd\u7565\u7684\u7ec6\u8282&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u6709\u4e24\u4e2a\u7ec6\u8282\u6211\u60f3\u7279\u522b\u8bf4\u4e00\u4e0b&#xff1a;<\/p>\n<p>\u4e00\u662f\u987a\u5e8f&#xff1a;\u5fc5\u987b\u5148\u66f4\u65b0\u8ba2\u5355\u72b6\u6001\u3001\u518d\u5199 Redis \u5e42\u7b49\u6807\u8bb0\u3002\u53cd\u8fc7\u6765\u7684\u8bdd&#xff0c;\u4e07\u4e00\u6807\u8bb0\u5199\u4e86\u4f46\u72b6\u6001\u66f4\u65b0\u5931\u8d25&#xff0c;\u540e\u7eed\u91cd\u8bd5\u4f1a\u88ab\u6807\u8bb0\u62e6\u4f4f&#xff0c;\u8ba2\u5355\u5c31\u6c38\u8fdc\u5361\u5728\u2019\u5f85\u652f\u4ed8\u2019\u4e86\u3002<\/p>\n<p>\u4e8c\u662f\u91d1\u989d\u6821\u9a8c&#xff1a;\u56de\u8c03\u91cc\u7684\u91d1\u989d\u5fc5\u987b\u548c\u8ba2\u5355\u5e94\u4ed8\u91d1\u989d\u6bd4\u5bf9\u3002\u8fd9\u4e0d\u662f\u8d70\u5f62\u5f0f\u2014\u2014\u653b\u51fb\u8005\u53ef\u80fd\u6293\u5230\u56de\u8c03\u683c\u5f0f\u540e\u4f2a\u9020\u4e00\u4e2a 1 \u5206\u94b1\u7684\u56de\u8c03&#xff0c;\u8bd5\u56fe\u2019\u4f4e\u4ef7\u652f\u4ed8\u2019\u3002\u9a8c\u7b7e\u9632\u7684\u662f\u5916\u90e8\u4f2a\u9020&#xff0c;\u91d1\u989d\u6821\u9a8c\u9632\u7684\u662f\u6570\u636e\u5c42\u9762\u7684\u7be1\u6539&#xff0c;\u4e24\u4e2a\u90fd\u8981\u6709\u3002<\/p>\n<p>\u53e6\u5916\u540e\u7eed\u4e1a\u52a1\u6bd4\u5982\u53d1\u79ef\u5206\u3001\u53d1\u901a\u77e5&#xff0c;\u5982\u679c\u5931\u8d25\u4e86\u4e0d\u5f71\u54cd\u7ed9\u7f51\u5173\u8fd4\u56de SUCCESS\u2014\u2014\u56e0\u4e3a\u8ba2\u5355\u72b6\u6001\u5df2\u7ecf\u6539\u4e86&#xff0c;\u8fd4\u56de FAIL \u8ba9\u7f51\u5173\u91cd\u8bd5\u4e5f\u6ca1\u610f\u4e49&#xff0c;\u91cd\u8bd5\u8fdb\u6765\u5c31\u88ab\u5e42\u7b49\u62e6\u6389\u4e86&#xff0c;\u79ef\u5206\u53cd\u800c\u6c38\u8fdc\u53d1\u4e0d\u51fa\u53bb\u3002\u6b63\u786e\u505a\u6cd5\u662f\u540e\u7eed\u4e1a\u52a1\u5355\u72ec\u8d70\u8865\u507f\u961f\u5217\u91cd\u8bd5\u3002&#034;<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u5b8c\u6574\u5904\u7406\u6d41\u7a0b&#xff1a;<\/p>\n<p>\u2460 \u6536\u5230\u652f\u4ed8\u56de\u8c03&#xff08;\u542b paymentNo\u3001orderId\u3001amount&#xff09;<br \/>\n\u2461 \u9a8c\u7b7e&#xff08;\u9632\u6b62\u4f2a\u9020\u56de\u8c03&#xff09;<br \/>\n\u2462 \u5e42\u7b49\u68c0\u67e5&#xff1a;EXISTS pay:done:{paymentNo}<br \/>\n   &#8211; \u5b58\u5728 \u2192 \u76f4\u63a5\u8fd4\u56de SUCCESS&#xff08;\u5e42\u7b49\u5feb\u901f\u8def\u5f84&#xff09;<br \/>\n\u2463 \u6821\u9a8c\u91d1\u989d&#xff1a;\u56de\u8c03\u91d1\u989d \u4e0e \u8ba2\u5355\u5e94\u4ed8\u91d1\u989d \u662f\u5426\u4e00\u81f4<br \/>\n   &#8211; \u4e0d\u4e00\u81f4 \u2192 \u8bb0\u5f55\u5f02\u5e38\u65e5\u5fd7 &#043; \u544a\u8b66&#xff0c;\u8fd4\u56de FAIL<br \/>\n\u2464 \u66f4\u65b0\u8ba2\u5355\u72b6\u6001&#xff08;\u6570\u636e\u5e93\u4e50\u89c2\u9501&#xff09;&#xff1a;<br \/>\n   UPDATE order SET status&#061;&#039;\u5df2\u652f\u4ed8&#039; WHERE order_id&#061;? AND status&#061;&#039;\u5f85\u652f\u4ed8&#039;<br \/>\n   &#8211; \u5f71\u54cd\u884c\u6570&#061;0 \u2192 \u5df2\u88ab\u5904\u7406&#xff0c;\u8fd4\u56de SUCCESS&#xff08;\u5e42\u7b49\u6700\u7ec8\u9632\u7ebf&#xff09;<br \/>\n\u2465 \u6267\u884c\u540e\u7eed\u4e1a\u52a1&#xff1a;\u786e\u8ba4\u5e93\u5b58\u3001\u53d1\u653e\u79ef\u5206\u3001\u53d1\u9001\u901a\u77e5\u7b49<br \/>\n\u2466 \u8bb0\u5f55\u5e42\u7b49\u6807\u8bb0&#xff1a;SET pay:done:{paymentNo} 1 EX 604800&#xff08;7\u5929&#xff09;<br \/>\n\u2467 \u8fd4\u56de SUCCESS<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5e42\u7b49\u68c0\u67e5\u7528 Redis &#043; \u6570\u636e\u5e93\u53cc\u91cd&#xff1a;<\/p>\n<ul>\n<li>Redis \u68c0\u67e5\u662f\u5feb\u901f\u8def\u5f84&#xff1a;99% \u7684\u91cd\u590d\u56de\u8c03\u5728\u8fd9\u91cc\u88ab\u62e6\u622a&#xff0c;\u4e0d\u67e5\u5e93\u3002<\/li>\n<li>\u6570\u636e\u5e93\u4e50\u89c2\u9501\u662f\u6700\u7ec8\u9632\u7ebf&#xff1a;Redis \u4e0d\u53ef\u7528\u6216 key \u8fc7\u671f\u65f6&#xff0c;WHERE status&#061;&#039;\u5f85\u652f\u4ed8&#039; \u4ecd\u80fd\u4fdd\u8bc1\u5e42\u7b49\u3002<\/li>\n<li>\u4e24\u8005\u4e92\u8865&#xff1a;Redis \u4fdd\u8bc1\u6027\u80fd&#xff0c;\u6570\u636e\u5e93\u4fdd\u8bc1\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u6b65\u9aa4\u2464\u5728\u6b65\u9aa4\u2466\u4e4b\u524d&#xff1a;<\/p>\n<p>\u5148\u66f4\u65b0\u8ba2\u5355\u72b6\u6001&#xff0c;\u518d\u8bb0\u5f55\u5e42\u7b49\u6807\u8bb0\u3002\u5982\u679c\u53cd\u8fc7\u6765&#xff08;\u5148\u8bb0\u6807\u8bb0\u540e\u66f4\u65b0\u72b6\u6001&#xff09;&#xff0c;\u4e07\u4e00\u66f4\u65b0\u72b6\u6001\u5931\u8d25&#xff0c;\u6807\u8bb0\u5df2\u5b58\u5728&#xff0c;\u540e\u7eed\u91cd\u8bd5\u4f1a\u88ab\u8bef\u5224\u4e3a&#034;\u5df2\u5904\u7406&#034;\u800c\u8df3\u8fc7&#xff0c;\u5bfc\u81f4\u8ba2\u5355\u6c38\u8fdc\u505c\u5728&#034;\u5f85\u652f\u4ed8&#034;\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u6b65\u9aa4\u2465\u5931\u8d25\u4e0d\u5f71\u54cd\u8fd4\u56de SUCCESS&#xff1a;<\/p>\n<p>\u540e\u7eed\u4e1a\u52a1&#xff08;\u53d1\u79ef\u5206\u3001\u53d1\u901a\u77e5&#xff09;\u662f\u53ef\u8865\u507f\u7684\u5f02\u6b65\u64cd\u4f5c\u3002\u5982\u679c\u56e0\u4e3a\u53d1\u79ef\u5206\u5931\u8d25\u5c31\u8fd4\u56de FAIL&#xff0c;\u652f\u4ed8\u7f51\u5173\u4f1a\u91cd\u8bd5\u6574\u4e2a\u56de\u8c03&#xff0c;\u4f46\u8ba2\u5355\u72b6\u6001\u5df2\u7ecf\u6539\u4e86&#xff0c;\u91cd\u8bd5\u65f6\u5e42\u7b49\u68c0\u67e5\u76f4\u63a5\u8fd4\u56de\u2014\u2014\u79ef\u5206\u5c31\u6c38\u8fdc\u53d1\u4e0d\u51fa\u53bb\u4e86\u3002\u6b63\u786e\u505a\u6cd5\u662f&#xff1a;\u540e\u7eed\u4e1a\u52a1\u5931\u8d25\u65f6\u5355\u72ec\u91cd\u8bd5\u6216\u8865\u507f&#xff0c;\u4e0d\u5f71\u54cd\u56de\u8c03\u4e3b\u6d41\u7a0b\u7684\u786e\u8ba4\u3002<\/p>\n<p>\u5e76\u53d1\u56de\u8c03\u7684\u5904\u7406&#xff08;\u4e24\u6761\u76f8\u540c\u56de\u8c03\u540c\u65f6\u5230\u8fbe&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u4e24\u4e2a\u8bf7\u6c42\u90fd\u901a\u8fc7\u4e86 Redis \u7684 EXISTS \u68c0\u67e5&#xff08;\u6b64\u65f6\u6807\u8bb0\u8fd8\u6ca1\u5199\u5165&#xff09;&#xff1b;<\/li>\n<li>\u4e24\u4e2a\u8bf7\u6c42\u90fd\u6267\u884c\u6b65\u9aa4\u2464\u7684 UPDATE &#8230; WHERE status&#061;&#039;\u5f85\u652f\u4ed8&#039;&#xff1b;<\/li>\n<li>\u6570\u636e\u5e93\u884c\u9501\u4fdd\u8bc1\u53ea\u6709\u4e00\u4e2a\u6210\u529f&#xff08;\u5f71\u54cd\u884c\u6570&#061;1&#xff09;&#xff0c;\u53e6\u4e00\u4e2a\u5f71\u54cd\u884c\u6570&#061;0&#xff0c;\u8d70\u5e42\u7b49\u8fd4\u56de\u3002<\/li>\n<\/ul>\n<p>\u6240\u4ee5\u6570\u636e\u5e93\u4e50\u89c2\u9501\u662f\u5e76\u53d1\u573a\u666f\u7684\u6700\u7ec8\u4fdd\u969c&#xff0c;Redis \u53ea\u662f\u52a0\u901f\u5668\u3002<\/p>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>&#034;Redis \u7ba1\u6027\u80fd&#xff0c;\u6570\u636e\u5e93\u7ba1\u6b63\u786e\u6027&#034;\u8fd9\u79cd\u4e00\u53e5\u8bdd\u603b\u7ed3&#xff0c;\u9762\u8bd5\u5b98\u4f1a\u8bb0\u4f4f&#xff1b;<\/li>\n<li>\u91d1\u989d\u6821\u9a8c\u548c&#034;\u540e\u7eed\u4e1a\u52a1\u5931\u8d25\u4e0d\u8fd4\u56de FAIL&#034;\u8fd9\u4e24\u4e2a\u7ec6\u8282&#xff0c;\u662f\u771f\u5b9e\u505a\u8fc7\u652f\u4ed8\u5bf9\u63a5\u7684\u4eba\u624d\u6709\u7684\u654f\u611f\u5ea6\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u516d&#xff1a;\u8ba2\u5355 30 \u5206\u949f\u672a\u652f\u4ed8\u81ea\u52a8\u53d6\u6d88<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u7528\u6237\u4e0b\u5355 30 \u5206\u949f\u4e0d\u652f\u4ed8\u8981\u81ea\u52a8\u53d6\u6d88&#xff0c;\u8fd9\u4e2a\u600e\u4e48\u5b9e\u73b0&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2~3 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u6446\u65b9\u6848\u5bf9\u6bd4&#xff0c;\u8868\u660e\u9009\u62e9\u6709\u4f9d\u636e&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u8fd9\u4e2a\u9700\u6c42\u5e38\u89c1\u65b9\u6848\u6709\u56db\u79cd&#xff0c;\u6211\u5148\u5feb\u901f\u8fc7\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u9009\u4e86\u6211\u4eec\u7528\u7684\u90a3\u4e2a&#xff1a;<\/p>\n<p>\u6570\u636e\u5e93\u8f6e\u8be2\u2014\u2014\u5b9a\u65f6\u626b\u8d85\u65f6\u8ba2\u5355&#xff0c;\u7b80\u5355\u4f46\u5ef6\u8fdf\u9ad8&#xff0c;\u8ba2\u5355\u8868\u5927\u4e86\u4e4b\u540e\u626b\u63cf\u672c\u8eab\u5c31\u662f\u538b\u529b&#xff0c;pass&#xff1b; JDK \u7684 DelayQueue\u2014\u2014\u7eaf\u5185\u5b58&#xff0c;\u91cd\u542f\u5c31\u4e22&#xff0c;\u5355\u673a\u4e5f\u625b\u4e0d\u4f4f\u96c6\u7fa4\u90e8\u7f72&#xff0c;pass&#xff1b; Redis \u7684 key \u8fc7\u671f\u4e8b\u4ef6\u2014\u2014\u8fd9\u4e2a\u8981\u7279\u522b\u8bf4\u4e00\u4e0b&#xff0c;\u751f\u4ea7\u73af\u5883\u4e0d\u80fd\u7528&#xff0c;\u56e0\u4e3a\u8fc7\u671f\u4e8b\u4ef6\u662f\u2019\u53d1\u5e03\u5373\u5fd8\u2019\u7684&#xff0c;\u6d88\u8d39\u8005\u65ad\u8fde\u90a3\u4e00\u77ac\u95f4\u7684\u4e8b\u4ef6\u5c31\u6c38\u4e45\u4e22\u4e86&#xff0c;\u800c\u4e14\u8fc7\u671f\u5220\u9664\u672c\u8eab\u662f\u60f0\u6027\u52a0\u5b9a\u671f\u7684&#xff0c;\u89e6\u53d1\u65f6\u95f4\u4e0d\u7cbe\u786e&#xff1b; \u6211\u4eec\u6700\u7ec8\u9009\u7684\u662f RocketMQ \u5ef6\u65f6\u6d88\u606f&#xff0c;\u6d88\u606f\u6301\u4e45\u5316\u3001\u6d88\u8d39\u5931\u8d25\u81ea\u52a8\u91cd\u8bd5&#xff0c;\u53ef\u9760\u6027\u6700\u9ad8&#xff0c;\u800c\u4e14\u6211\u4eec\u6280\u672f\u6808\u91cc\u672c\u6765\u5c31\u6709 RocketMQ&#xff0c;\u4e0d\u5f15\u5165\u65b0\u7ec4\u4ef6\u3002&#034;<\/p>\n<p>\u5b9e\u73b0\u6d41\u7a0b&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u5b9e\u73b0\u5f88\u76f4\u63a5&#xff1a;\u4e0b\u5355\u6210\u529f\u540e\u53d1\u4e00\u6761\u5ef6\u65f6 30 \u5206\u949f\u7684\u6d88\u606f&#xff0c;\u6d88\u606f\u4f53\u5c31\u4e00\u4e2a\u8ba2\u5355\u53f7\u300230 \u5206\u949f\u540e\u6d88\u8d39\u8005\u6536\u5230\u6d88\u606f&#xff0c;\u5148\u67e5\u8ba2\u5355\u72b6\u6001\u2014\u2014\u5df2\u652f\u4ed8\u5c31\u5ffd\u7565&#xff0c;\u5df2\u53d6\u6d88\u4e5f\u5ffd\u7565&#xff0c;\u53ea\u6709\u8fd8\u662f\u2019\u5f85\u652f\u4ed8\u2019\u624d\u6267\u884c\u53d6\u6d88\u3002\u53d6\u6d88\u52a8\u4f5c\u662f&#xff1a;\u4e50\u89c2\u9501\u66f4\u65b0\u8ba2\u5355\u72b6\u6001\u3001\u56de\u6eda\u5e93\u5b58\u3001\u91ca\u653e\u4f18\u60e0\u5238\u3002<\/p>\n<p>\u8fd9\u91cc\u72b6\u6001\u68c0\u67e5\u7279\u522b\u91cd\u8981&#xff0c;\u56e0\u4e3a\u5b58\u5728\u4e00\u4e2a\u7ecf\u5178\u5e76\u53d1&#xff1a;\u7528\u6237\u5728\u7b2c 29 \u5206 59 \u79d2\u5b8c\u6210\u4e86\u652f\u4ed8&#xff0c;\u53d6\u6d88\u6d88\u606f\u51e0\u4e4e\u540c\u65f6\u5230\u8fbe\u3002\u8fd9\u65f6\u5019\u8ba2\u5355\u72b6\u6001\u5df2\u7ecf\u662f\u2019\u5df2\u652f\u4ed8\u2019&#xff0c;\u53d6\u6d88\u903b\u8f91\u4e00\u67e5\u72b6\u6001\u5c31\u76f4\u63a5\u9000\u51fa\u4e86&#xff0c;\u4e0d\u4f1a\u628a\u5df2\u4ed8\u6b3e\u7684\u8ba2\u5355\u53d6\u6d88\u6389\u3002\u72b6\u6001\u673a\u5929\u7136\u89e3\u51b3\u4e86\u8fd9\u4e2a\u7ade\u6001&#xff0c;\u4e0d\u9700\u8981\u989d\u5916\u7684\u9501\u3002&#034;<\/p>\n<p>\u8865\u5145\u5ef6\u65f6\u7ea7\u522b\u7684\u9650\u5236&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6709\u4e2a\u5c0f\u9650\u5236\u662f RocketMQ 4.x \u53ea\u652f\u6301 18 \u4e2a\u56fa\u5b9a\u5ef6\u65f6\u7ea7\u522b&#xff0c;30 \u5206\u949f\u6b63\u597d\u662f\u5176\u4e2d\u4e00\u7ea7&#xff0c;\u8fd0\u6c14\u4e0d\u9519\u3002\u5982\u679c\u4e1a\u52a1\u8981 45 \u5206\u949f\u8fd9\u79cd\u975e\u6807\u65f6\u95f4&#xff0c;\u8981\u4e48\u5c31\u8fd1\u53d6\u6574&#xff0c;\u8981\u4e48\u7528 Redis ZSet \u81ea\u5df1\u5b9e\u73b0&#xff1a;ZADD \u7684\u65f6\u5019 score \u5b58\u5230\u671f\u65f6\u95f4\u6233&#xff0c;\u540e\u53f0\u6bcf\u79d2 ZRANGEBYSCORE \u53d6\u5df2\u5230\u671f\u7684&#xff0c;\u53d6\u51fa\u6765\u540e\u5148 ZREM\u3001\u7528 ZREM \u7684\u8fd4\u56de\u503c\u5224\u65ad\u662f\u4e0d\u662f\u81ea\u5df1\u62a2\u5230\u7684\u2014\u2014\u591a\u5b9e\u4f8b\u90e8\u7f72\u65f6\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u80fd\u5220\u6210\u529f&#xff0c;\u5929\u7136\u9632\u91cd\u590d\u6d88\u8d39\u3002\u8fd9\u4e2a ZSet \u65b9\u6848\u6211\u4eec\u540e\u6765\u5728\u53e6\u4e00\u4e2a\u9700\u8981\u4efb\u610f\u5ef6\u65f6\u7cbe\u5ea6\u7684\u573a\u666f\u7528\u8fc7&#xff0c;\u4e5f\u633a\u7a33\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u65b9\u6848\u5bf9\u6bd4\u8868&#xff1a;<\/p>\n<table>\n<tr>\u65b9\u6848\u539f\u7406\u4f18\u70b9\u7f3a\u70b9\u662f\u5426\u9009\u62e9<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u636e\u5e93\u8f6e\u8be2<\/td>\n<td>\u5b9a\u65f6 SQL \u626b\u63cf\u8d85\u65f6\u8ba2\u5355<\/td>\n<td>\u7b80\u5355<\/td>\n<td>\u5ef6\u8fdf\u9ad8\u3001DB \u538b\u529b\u5927\u3001\u9501\u7ade\u4e89<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td>JDK DelayQueue<\/td>\n<td>\u5185\u5b58\u5ef6\u8fdf\u961f\u5217<\/td>\n<td>\u7cbe\u5ea6\u9ad8<\/td>\n<td>\u5355\u673a\u3001\u91cd\u542f\u4e22\u5931<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td>Redis ZSet<\/td>\n<td>score&#061;\u5230\u671f\u65f6\u95f4&#xff0c;\u8f6e\u8be2\u53d6<\/td>\n<td>\u7075\u6d3b\u3001\u7cbe\u5ea6\u9ad8<\/td>\n<td>\u9700\u81ea\u5df1\u4fdd\u8bc1\u53ef\u9760\u6027<\/td>\n<td>\u26a0\ufe0f \u5907\u9009<\/td>\n<\/tr>\n<tr>\n<td>Redis \u8fc7\u671f\u4e8b\u4ef6<\/td>\n<td>key \u8fc7\u671f\u89e6\u53d1\u901a\u77e5<\/td>\n<td>\u7b80\u5355<\/td>\n<td>\u4e0d\u53ef\u9760&#xff08;\u53ef\u80fd\u4e22\u5931&#xff09;<\/td>\n<td>\u274c<\/td>\n<\/tr>\n<tr>\n<td>RocketMQ \u5ef6\u65f6\u6d88\u606f<\/td>\n<td>\u539f\u751f\u5ef6\u65f6\u6295\u9012<\/td>\n<td>\u53ef\u9760\u3001\u6709\u91cd\u8bd5<\/td>\n<td>\u5ef6\u65f6\u7ea7\u522b\u56fa\u5b9a&#xff08;4.x&#xff09;<\/td>\n<td>\u2705 \u9996\u9009<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528 Redis \u8fc7\u671f\u4e8b\u4ef6&#xff08;\u5fc5\u987b\u4e3b\u52a8\u8bf4\u660e\u7684\u5751&#xff09;&#xff1a;<\/p>\n<li>\u8fc7\u671f\u4e8b\u4ef6\u662f&#034;\u53d1\u5e03\u5373\u5fd8&#034;\u7684&#xff0c;\u5982\u679c\u6d88\u8d39\u8005\u65ad\u8fde&#xff0c;\u4e8b\u4ef6\u76f4\u63a5\u4e22\u5931\u3002<\/li>\n<li>\u8fc7\u671f\u5220\u9664\u662f\u60f0\u6027&#043;\u5b9a\u671f\u7684&#xff0c;\u4e8b\u4ef6\u89e6\u53d1\u65f6\u95f4\u4e0d\u7cbe\u786e\u3002<\/li>\n<li>\u96c6\u7fa4\u6a21\u5f0f\u4e0b&#xff0c;\u4e8b\u4ef6\u53ea\u5728 key \u6240\u5728\u8282\u70b9\u53d1\u5e03&#xff0c;\u6d88\u8d39\u8005\u5fc5\u987b\u8fde\u63a5\u6240\u6709\u8282\u70b9\u3002<\/li>\n<p>RocketMQ \u5ef6\u65f6\u6d88\u606f\u5b9e\u73b0&#xff1a;<\/p>\n<p>\u4e0b\u5355\u65f6&#xff1a;<br \/>\n\u2460 \u521b\u5efa\u8ba2\u5355&#xff08;\u72b6\u6001&#061;\u5f85\u652f\u4ed8&#xff09;<br \/>\n\u2461 \u53d1\u9001\u5ef6\u65f6\u6d88\u606f&#xff1a;<br \/>\n   rocketMQProducer.sendDelay(TOPIC_DELAY, TAG_CANCEL, orderId, DELAY_30M)<br \/>\n   \/\/ DELAY_30M &#061; 16&#xff0c;\u5bf9\u5e94 RocketMQ \u7684 30 \u5206\u949f\u5ef6\u65f6\u7ea7\u522b<\/p>\n<p>30 \u5206\u949f\u540e&#xff1a;<br \/>\n\u2462 \u6d88\u8d39\u8005\u6536\u5230\u6d88\u606f<br \/>\n\u2463 \u67e5\u8be2\u8ba2\u5355\u72b6\u6001<br \/>\n   &#8211; \u5df2\u652f\u4ed8 \u2192 \u5ffd\u7565&#xff08;\u7528\u6237\u5df2\u4ed8\u6b3e&#xff0c;\u4e0d\u80fd\u53d6\u6d88&#xff09;<br \/>\n   &#8211; \u5df2\u53d6\u6d88 \u2192 \u5ffd\u7565&#xff08;\u5e42\u7b49&#xff09;<br \/>\n   &#8211; \u5f85\u652f\u4ed8 \u2192 \u6267\u884c\u53d6\u6d88<br \/>\n\u2464 \u53d6\u6d88\u903b\u8f91&#xff1a;<br \/>\n   a. \u66f4\u65b0\u8ba2\u5355\u72b6\u6001\u4e3a&#034;\u5df2\u53d6\u6d88&#034;&#xff08;\u4e50\u89c2\u9501&#xff09;<br \/>\n   b. \u56de\u6eda\u5e93\u5b58&#xff08;\u573a\u666f\u4e8c\u7684\u903b\u8f91&#xff09;<br \/>\n   c. \u91ca\u653e\u4f18\u60e0\u5238&#xff08;\u5982\u6709&#xff09;<\/p>\n<p>Redis ZSet \u5907\u9009\u65b9\u6848\u5b8c\u6574\u5b9e\u73b0&#xff1a;<\/p>\n<p>\u4e0b\u5355\u65f6&#xff1a;<br \/>\nZADD order:delay {\u4e0b\u5355\u65f6\u95f4&#043;30\u5206\u949f\u7684\u6beb\u79d2\u65f6\u95f4\u6233} {orderId}<\/p>\n<p>\u540e\u53f0\u8f6e\u8be2&#xff08;\u6bcf\u79d2\u4e00\u6b21&#xff09;&#xff1a;<br \/>\n\u2460 ZRANGEBYSCORE order:delay 0 {\u5f53\u524d\u65f6\u95f4\u6233} LIMIT 0 100<br \/>\n   \/\/ \u53d6\u51fa\u6240\u6709\u5df2\u5230\u671f\u7684\u8ba2\u5355&#xff0c;\u6bcf\u6b21\u6700\u591a 100 \u6761\u9632\u963b\u585e<br \/>\n\u2461 \u5bf9\u6bcf\u4e2a\u8ba2\u5355&#xff1a;<br \/>\n   a. ZREM order:delay {orderId}  \/\/ \u539f\u5b50\u79fb\u9664&#xff0c;\u8fd4\u56de\u503c&gt;0 \u624d\u5904\u7406&#xff08;\u9632\u91cd\u590d&#xff09;<br \/>\n   b. \u67e5\u8be2\u8ba2\u5355\u72b6\u6001&#xff0c;\u6267\u884c\u53d6\u6d88<\/p>\n<p><span class=\"token comment\">\/\/ 1. \u4e0b\u5355\u65f6\u52a0\u5165\u5ef6\u8fdf\u961f\u5217&#xff08;30 \u5206\u949f\u540e\u8fc7\u671f&#xff09;<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">addDelayTask<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> orderId<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">long<\/span> expireTime <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">currentTimeMillis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">30<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">60<\/span> <span class=\"token operator\">*<\/span> <span class=\"token number\">1000<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">opsForZSet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">add<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;delay:order:cancel&#034;<\/span><span class=\"token punctuation\">,<\/span> orderId<span class=\"token punctuation\">,<\/span> expireTime<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token comment\">\/\/ 2. \u540e\u53f0\u8f6e\u8be2\u6d88\u8d39&#xff08;\u6bcf\u79d2\u6267\u884c\u4e00\u6b21&#xff09;<\/span><br \/>\n<span class=\"token annotation punctuation\">&#064;Scheduled<\/span><span class=\"token punctuation\">(<\/span>fixedRate <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1000<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">pollDelayQueue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">long<\/span> now <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">System<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">currentTimeMillis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">Set<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Object<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> expiredOrders <span class=\"token operator\">&#061;<\/span> redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">opsForZSet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n            <span class=\"token punctuation\">.<\/span><span class=\"token function\">rangeByScore<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;delay:order:cancel&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> now<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>expiredOrders <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">||<\/span> expiredOrders<span class=\"token punctuation\">.<\/span><span class=\"token function\">isEmpty<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Object<\/span> orderId <span class=\"token operator\">:<\/span> expiredOrders<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ ZREM \u539f\u5b50\u79fb\u9664&#xff0c;\u8fd4\u56de\u503c &gt; 0 \u624d\u5904\u7406&#xff08;\u591a\u5b9e\u4f8b\u9632\u91cd\u590d\u6d88\u8d39&#xff09;<\/span><br \/>\n        <span class=\"token class-name\">Long<\/span> removed <span class=\"token operator\">&#061;<\/span> redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">opsForZSet<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n                <span class=\"token punctuation\">.<\/span><span class=\"token function\">remove<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;delay:order:cancel&#034;<\/span><span class=\"token punctuation\">,<\/span> orderId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>removed <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">&amp;&amp;<\/span> removed <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token function\">cancelIfUnpaid<\/span><span class=\"token punctuation\">(<\/span>orderId<span class=\"token punctuation\">.<\/span><span class=\"token function\">toString<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>ZSet \u65b9\u6848\u4e09\u4e2a\u5173\u952e\u70b9&#xff1a;<\/p>\n<ul>\n<li>ZREM \u7684\u8fd4\u56de\u503c\u505a\u5e42\u7b49&#xff1a;\u591a\u5b9e\u4f8b\u90e8\u7f72\u65f6&#xff0c;\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u80fd ZREM \u6210\u529f&#xff0c;\u907f\u514d\u91cd\u590d\u53d6\u6d88\u3002<\/li>\n<li>LIMIT \u5206\u6279&#xff1a;\u9632\u6b62\u4e00\u6b21\u53d6\u51fa\u592a\u591a\u5bfc\u81f4\u5904\u7406\u8d85\u65f6\u3002<\/li>\n<li>\u8f6e\u8be2\u95f4\u9694\u51b3\u5b9a\u7cbe\u5ea6&#xff1a;1 \u79d2\u8f6e\u8be2 &#061; \u6700\u5927 1 \u79d2\u5ef6\u8fdf&#xff0c;\u5bf9\u8ba2\u5355\u53d6\u6d88\u5b8c\u5168\u591f\u7528\u3002<\/li>\n<\/ul>\n<p>\u53d6\u6d88\u64cd\u4f5c\u7684\u5e76\u53d1\u5904\u7406&#xff1a;<\/p>\n<li>\u53d6\u6d88\u4e0e\u652f\u4ed8\u5e76\u53d1&#xff1a;\u7528\u6237\u5728\u7b2c 29 \u5206 59 \u79d2\u652f\u4ed8\u4e86&#xff0c;\u53d6\u6d88\u6d88\u606f\u6070\u597d\u5230\u8fbe\u3002\u6b64\u65f6\u8ba2\u5355\u72b6\u6001\u5df2\u662f&#034;\u5df2\u652f\u4ed8&#034;&#xff0c;\u53d6\u6d88\u903b\u8f91\u67e5\u5230\u72b6\u6001\u4e0d\u662f&#034;\u5f85\u652f\u4ed8&#034;&#xff0c;\u76f4\u63a5\u5ffd\u7565\u3002\u72b6\u6001\u673a\u4fdd\u8bc1\u4e86\u6b63\u786e\u6027\u3002<\/li>\n<li>\u53d6\u6d88\u6d88\u606f\u91cd\u590d\u6295\u9012&#xff1a;MQ \u7684 at-least-once \u8bed\u4e49\u53ef\u80fd\u91cd\u590d\u6295\u9012\u3002\u901a\u8fc7 UPDATE &#8230; WHERE status&#061;&#039;\u5f85\u652f\u4ed8&#039; \u7684\u5f71\u54cd\u884c\u6570\u5224\u65ad&#xff0c;\u7b2c\u4e8c\u6b21\u6267\u884c\u65f6\u5f71\u54cd\u884c\u6570&#061;0&#xff0c;\u76f4\u63a5\u8fd4\u56de\u3002<\/li>\n<h4>&#x1f4d6; \u6211\u7684\u7ecf\u5386\u00b7\u8bdd\u672f\u6a21\u677f<\/h4>\n<p>\u201c\u8bf4\u5230 Redis \u8fc7\u671f\u4e8b\u4ef6&#xff0c;\u6211\u4eec\u56e2\u961f\u65e9\u671f\u771f\u6709\u540c\u4e8b\u7528\u5b83\u505a\u8fc7\u63d0\u9192\u529f\u80fd&#xff0c;\u6d4b\u8bd5\u73af\u5883\u4e00\u5207\u6b63\u5e38&#xff0c;\u4e0a\u7ebf\u540e\u5076\u5c14\u6709\u7528\u6237\u6536\u4e0d\u5230\u63d0\u9192&#xff0c;\u67e5\u4e86\u534a\u5929\u53d1\u73b0\u662f\u670d\u52a1\u53d1\u5e03\u91cd\u542f\u7684\u77ac\u95f4&#xff0c;\u90a3\u51e0\u79d2\u7684\u8fc7\u671f\u4e8b\u4ef6\u5168\u4e22\u4e86\u3002\u4ece\u90a3\u4ee5\u540e\u6211\u4eec\u5185\u90e8\u5b9a\u4e86\u6761\u89c4\u77e9&#xff1a;\u8fc7\u671f\u4e8b\u4ef6\u53ea\u51c6\u7528\u4e8e\u2019\u4e22\u4e86\u4e5f\u65e0\u6240\u8c13\u2019\u7684\u573a\u666f&#xff0c;\u6838\u5fc3\u4e1a\u52a1\u4e00\u5f8b\u8d70\u5ef6\u65f6\u6d88\u606f\u3002\u201d<\/p>\n<hr \/>\n<h3>\u573a\u666f\u4e03&#xff1a;\u600e\u4e48\u9632\u6b62\u7528\u6237\u91cd\u590d\u4e0b\u5355&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u7528\u6237\u624b\u6296\u53cc\u51fb\u4e86\u4e0b\u5355\u6309\u94ae&#xff0c;\u6216\u8005\u7f51\u7edc\u91cd\u8bd5&#xff0c;\u4f1a\u4e0d\u4f1a\u521b\u5efa\u4e24\u4e2a\u8ba2\u5355&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u533a\u5206\u4e24\u79cd&#034;\u91cd\u590d&#034;&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u5148\u8981\u533a\u5206\u4e24\u79cd\u60c5\u51b5&#xff1a;\u4e00\u79cd\u662f\u524d\u7aef\u91cd\u590d\u63d0\u4ea4\u2014\u2014\u53cc\u51fb\u3001\u7f51\u7edc\u5361\u987f\u6d4f\u89c8\u5668\u91cd\u8bd5&#xff0c;\u8fd9\u662f\u8981\u9632\u7684&#xff1b;\u53e6\u4e00\u79cd\u662f\u7528\u6237\u771f\u7684\u60f3\u518d\u4e70\u4e00\u5355&#xff0c;\u8fd9\u662f\u5408\u6cd5\u884c\u4e3a\u4e0d\u80fd\u62e6\u3002\u9632\u91cd\u65b9\u6848\u4e0d\u80fd\u8bef\u4f24\u540e\u8005\u3002\u201d<\/p>\n<p>\u4e3b\u65b9\u6848&#xff1a;\u5e42\u7b49\u4ee4\u724c&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u6211\u4eec\u7528\u7684\u662f\u5e42\u7b49\u4ee4\u724c\u673a\u5236&#xff1a;\u7528\u6237\u8fdb\u5165\u8ba2\u5355\u786e\u8ba4\u9875\u7684\u65f6\u5019&#xff0c;\u524d\u7aef\u5148\u8c03\u4e00\u4e2a\u63a5\u53e3\u9886\u2019\u4e0b\u5355\u4ee4\u724c\u2019&#xff0c;\u670d\u52a1\u7aef\u751f\u6210\u4e00\u4e2a\u968f\u673a token \u5b58\u5230 Redis&#xff0c;\u8bbe 10 \u5206\u949f\u8fc7\u671f\u3002\u7528\u6237\u63d0\u4ea4\u8ba2\u5355\u65f6\u628a\u4ee4\u724c\u5e26\u4e0a&#xff0c;\u670d\u52a1\u7aef\u7528\u4e00\u6bb5 Lua \u505a\u2019\u68c0\u67e5\u5e76\u5220\u9664\u2019\u2014\u2014\u4ee4\u724c\u5b58\u5728\u5c31\u5220\u6389\u5e76\u653e\u884c\u4e0b\u5355&#xff0c;\u4e0d\u5b58\u5728\u5c31\u62d2\u7edd\u3002<\/p>\n<p>\u8fd9\u91cc\u5fc5\u987b\u7528 Lua \u628a\u68c0\u67e5\u548c\u5220\u9664\u5408\u6210\u4e00\u6b65\u539f\u5b50\u64cd\u4f5c\u3002\u5982\u679c\u5206\u6210 EXISTS \u548c DEL \u4e24\u6b65&#xff0c;\u53cc\u51fb\u4ea7\u751f\u7684\u4e24\u4e2a\u8bf7\u6c42\u53ef\u80fd\u90fd\u901a\u8fc7 EXISTS \u68c0\u67e5&#xff0c;\u7136\u540e\u90fd\u53bb\u521b\u5efa\u8ba2\u5355&#xff0c;\u9632\u91cd\u5c31\u5931\u6548\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u65b9\u6848\u7684\u597d\u5904\u662f\u5929\u7136\u533a\u5206\u4e86\u4e24\u79cd\u91cd\u590d&#xff1a;\u540c\u4e00\u4e2a\u4ee4\u724c\u53ea\u80fd\u7528\u4e00\u6b21&#xff0c;\u53cc\u51fb\u7684\u7b2c\u4e8c\u4e2a\u8bf7\u6c42\u5fc5\u7136\u88ab\u62d2&#xff1b;\u4f46\u7528\u6237\u4e0b\u5b8c\u5355\u518d\u8fdb\u786e\u8ba4\u9875&#xff0c;\u4f1a\u9886\u5230\u65b0\u4ee4\u724c&#xff0c;\u6b63\u5e38\u4e0b\u7b2c\u4e8c\u5355\u5b8c\u5168\u4e0d\u53d7\u5f71\u54cd\u3002&#034;<\/p>\n<p>\u515c\u5e95&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u9664\u4e86\u4ee4\u724c&#xff0c;\u8fd8\u6709\u4e24\u5c42\u515c\u5e95&#xff1a;\u4e00\u662f\u524d\u7aef\u63d0\u4ea4\u540e\u7acb\u523b\u7f6e\u7070\u6309\u94ae&#xff0c;\u6321\u4f4f\u5927\u90e8\u5206\u624b\u6296&#xff1b;\u4e8c\u662f\u8ba2\u5355\u53f7\u6709\u552f\u4e00\u7d22\u5f15&#xff0c;\u6781\u7aef\u60c5\u51b5\u4e0b\u91cd\u590d\u63d2\u5165\u4f1a\u76f4\u63a5\u5931\u8d25\u3002\u4e0d\u8fc7\u8981\u8bf4\u6e05\u695a&#xff0c;\u552f\u4e00\u7d22\u5f15\u53ea\u662f\u6700\u540e\u7684\u4fdd\u9669&#xff0c;\u4e0d\u80fd\u4f5c\u4e3a\u4e3b\u8981\u624b\u6bb5\u2014\u2014\u56e0\u4e3a\u4e24\u6b21\u53cc\u51fb\u751f\u6210\u7684\u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u8ba2\u5355\u53f7&#xff0c;\u552f\u4e00\u7d22\u5f15\u6839\u672c\u62e6\u4e0d\u4f4f&#xff0c;\u771f\u6b63\u8d77\u4f5c\u7528\u7684\u8fd8\u662f\u4ee4\u724c\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u5e42\u7b49\u4ee4\u724c\u5b8c\u6574\u6d41\u7a0b&#xff1a;<\/p>\n<p>\u2460 \u7528\u6237\u8fdb\u5165\u4e0b\u5355\u786e\u8ba4\u9875\u65f6&#xff0c;\u524d\u7aef\u8bf7\u6c42\u4e00\u4e2a&#034;\u4e0b\u5355\u4ee4\u724c&#034;&#xff1a;<br \/>\n   GET \/order\/token \u2192 \u8fd4\u56de token<br \/>\n   &#xff08;Redis: SET order:token:{userId}:{token} 1 EX 600&#xff09;<\/p>\n<p>\u2461 \u7528\u6237\u63d0\u4ea4\u8ba2\u5355\u65f6&#xff0c;\u643a\u5e26\u4ee4\u724c&#xff1a;<br \/>\n   POST \/order\/create {token, skuId, qty, &#8230;}<\/p>\n<p>\u2462 \u670d\u52a1\u7aef\u5904\u7406&#xff1a;<br \/>\n   a. \u7528 Lua \u539f\u5b50&#034;\u68c0\u67e5\u5e76\u5220\u9664&#034;\u4ee4\u724c&#xff1a;<br \/>\n      \u4ee4\u724c\u5b58\u5728 \u2192 \u5220\u9664&#xff0c;\u7ee7\u7eed\u4e0b\u5355<br \/>\n      \u4ee4\u724c\u4e0d\u5b58\u5728 \u2192 \u91cd\u590d\u63d0\u4ea4&#xff0c;\u62d2\u7edd<br \/>\n   b. \u4ee4\u724c\u6821\u9a8c\u901a\u8fc7\u540e&#xff0c;\u6b63\u5e38\u521b\u5efa\u8ba2\u5355<\/p>\n<p>\u4ee4\u724c\u6d88\u8d39 Lua \u811a\u672c&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; KEYS[1] &#061; order:token:{userId}:{token}<\/span><br \/>\n<span class=\"token keyword\">if<\/span> redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;EXISTS&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;DEL&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span>  <span class=\"token comment\">&#8212; \u4ee4\u724c\u6709\u6548&#xff0c;\u5df2\u6d88\u8d39<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span>  <span class=\"token comment\">&#8212; \u4ee4\u724c\u65e0\u6548&#xff08;\u5df2\u7528\u8fc7\u6216\u4e0d\u5b58\u5728&#xff09;<\/span><br \/>\n<span class=\"token keyword\">end<\/span><\/p>\n<p>\u4ee4\u724c\u65b9\u6848\u7684\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5929\u7136\u5e42\u7b49&#xff1a;\u4e00\u4e2a\u4ee4\u724c\u53ea\u80fd\u7528\u4e00\u6b21&#xff0c;\u7b2c\u4e8c\u6b21\u63d0\u4ea4\u5fc5\u7136\u88ab\u62d2\u3002<\/li>\n<li>\u4e0d\u5f71\u54cd\u5408\u6cd5\u91cd\u590d\u4e0b\u5355&#xff1a;\u7528\u6237\u4e0b\u5b8c\u7b2c\u4e00\u5355\u540e&#xff0c;\u518d\u8fdb\u5165\u786e\u8ba4\u9875\u4f1a\u83b7\u53d6\u65b0\u4ee4\u724c&#xff0c;\u53ef\u4ee5\u6b63\u5e38\u4e0b\u7b2c\u4e8c\u5355\u3002<\/li>\n<li>\u524d\u7aef\u4f53\u9a8c\u597d&#xff1a;\u53ef\u4ee5\u5728\u63d0\u4ea4\u540e\u7acb\u5373\u7981\u7528\u6309\u94ae &#043; \u63d0\u793a&#034;\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4&#034;\u3002<\/li>\n<\/ul>\n<p>\u8865\u5145\u5206\u5e03\u5f0f\u9501\u65b9\u6848&#xff08;\u65e0\u4ee4\u724c\u573a\u666f&#xff0c;\u5982\u7b2c\u4e09\u65b9\u8c03\u7528&#xff09;&#xff1a;<\/p>\n<p>\u9501\u7684 key&#xff1a;order:lock:{userId}:{skuId}:{\u5730\u5740\u54c8\u5e0c}<br \/>\n\u9501\u7684\u8d85\u65f6&#xff1a;10 \u79d2<\/p>\n<p>\u2460 \u5c1d\u8bd5\u83b7\u53d6\u9501&#xff08;SETNX &#043; EX&#xff09;<br \/>\n\u2461 \u83b7\u53d6\u6210\u529f \u2192 \u521b\u5efa\u8ba2\u5355 \u2192 \u91ca\u653e\u9501<br \/>\n\u2462 \u83b7\u53d6\u5931\u8d25 \u2192 \u8fd4\u56de&#034;\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4&#034;<\/p>\n<p>\u5c40\u9650&#xff1a;\u9501\u7684\u7c92\u5ea6\u4e0d\u597d\u628a\u63e1\u3002\u592a\u7c97&#xff08;\u53ea\u6309 userId&#xff09;\u4f1a\u8bef\u4f24\u5408\u6cd5\u7684\u591a\u5546\u54c1\u4e0b\u5355&#xff1b;\u592a\u7ec6&#xff08;\u52a0\u5730\u5740\u54c8\u5e0c&#xff09;\u53ef\u80fd\u62e6\u4e0d\u4f4f\u3002\u6240\u4ee5\u4ee4\u724c\u65b9\u6848\u662f\u9996\u9009&#xff0c;\u9501\u65b9\u6848\u662f\u8865\u5145\u3002<\/p>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>\u6700\u540e\u90a3\u53e5&#034;\u552f\u4e00\u7d22\u5f15\u62e6\u4e0d\u4f4f\u53cc\u51fb&#xff0c;\u56e0\u4e3a\u8ba2\u5355\u53f7\u4e0d\u540c&#034;\u662f\u4e2a\u53cd\u76f4\u89c9\u7684\u7ec6\u8282&#xff0c;\u80fd\u8bf4\u51fa\u6765\u8bc1\u660e\u4f60\u771f\u7684\u60f3\u8fc7\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u800c\u4e0d\u662f\u80cc\u4e86\u4e2a&#034;\u52a0\u552f\u4e00\u7d22\u5f15&#034;\u7684\u4e07\u80fd\u7b54\u6848\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u516b&#xff1a;\u7a7f\u900f\u3001\u51fb\u7a7f\u3001\u96ea\u5d29<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u5546\u54c1\u8be6\u60c5\u9875\u7684\u7f13\u5b58\u600e\u4e48\u505a\u7684&#xff1f;\u7a7f\u900f\u3001\u51fb\u7a7f\u3001\u96ea\u5d29\u5206\u522b\u600e\u4e48\u89e3\u51b3&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;3 \u5206\u949f&#xff08;\u4e09\u4e2a\u95ee\u9898\u4e00\u8d77\u95ee&#xff0c;\u503c\u5f97\u5b8c\u6574\u5c55\u5f00&#xff09;<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u7528\u4e00\u53e5\u8bdd\u628a\u4e09\u4e2a\u6982\u5ff5\u9489\u6b7b&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8fd9\u4e09\u4e2a\u95ee\u9898\u6211\u5148\u7528\u4e00\u53e5\u8bdd\u533a\u5206\u4e00\u4e0b&#xff0c;\u56e0\u4e3a\u5f88\u591a\u4eba\u4f1a\u6df7&#xff1a;\u7a7f\u900f\u662f\u67e5\u2019\u6839\u672c\u4e0d\u5b58\u5728\u2019\u7684\u6570\u636e&#xff0c;\u7f13\u5b58\u548c\u6570\u636e\u5e93\u90fd\u6ca1\u6709&#xff0c;\u8bf7\u6c42\u6b21\u6b21\u843d\u5e93&#xff1b;\u51fb\u7a7f\u662f\u2019\u67d0\u4e00\u4e2a\u70ed\u70b9 key\u2019\u6070\u597d\u8fc7\u671f&#xff0c;\u77ac\u95f4\u7684\u5e76\u53d1\u5168\u6253\u5230\u6570\u636e\u5e93&#xff1b;\u96ea\u5d29\u662f\u2019\u4e00\u5927\u6279 key\u2019\u540c\u65f6\u8fc7\u671f\u6216\u8005 Redis \u6574\u4e2a\u6302\u4e86&#xff0c;\u6570\u636e\u5e93\u88ab\u5168\u9762\u51b2\u57ae\u3002\u4e00\u4e2a\u662f\u67e5\u65e0\u6b64\u6570\u636e&#xff0c;\u4e00\u4e2a\u662f\u5355\u70b9\u8fc7\u70ed&#xff0c;\u4e00\u4e2a\u662f\u96c6\u4f53\u5931\u6548\u3002\u201d<\/p>\n<p>\u7a7f\u900f&#xff08;50 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u7a7f\u900f\u6211\u4eec\u7528\u4e86\u4e09\u5c42\u7ec4\u5408&#xff1a;<\/p>\n<p>\u7b2c\u4e00\u5c42\u53c2\u6570\u6821\u9a8c&#xff0c;\u5546\u54c1 ID \u5fc5\u987b\u662f\u6b63\u6574\u6570&#xff0c;\u8d1f\u6570\u3001\u8d85\u957f\u5b57\u7b26\u4e32\u76f4\u63a5\u62d2\u6389&#xff0c;\u6210\u672c\u6700\u4f4e\u7684\u4e00\u9053\u95f8\u3002<\/p>\n<p>\u7b2c\u4e8c\u5c42\u5e03\u9686\u8fc7\u6ee4\u5668&#xff0c;\u7cfb\u7edf\u542f\u52a8\u65f6\u628a\u5168\u91cf\u6709\u6548\u5546\u54c1 ID \u704c\u8fdb\u53bb&#xff0c;\u8bf7\u6c42\u5148\u95ee\u5e03\u9686\u8fc7\u6ee4\u5668\u2019\u8fd9\u4e2a ID \u53ef\u80fd\u5b58\u5728\u5417\u2019&#xff0c;\u8bf4\u4e0d\u53ef\u80fd\u7684\u76f4\u63a5\u8fd4\u56de&#xff0c;\u8fde Redis \u90fd\u4e0d\u7528\u67e5\u3002\u6211\u4eec\u7684\u53c2\u6570\u662f\u9884\u4f30 100 \u4e07\u5546\u54c1\u3001\u8bef\u5224\u7387\u4e07\u5206\u4e4b\u4e00&#xff0c;\u7b97\u4e0b\u6765\u4f4d\u56fe\u5927\u6982 19MB&#xff0c;\u5185\u5b58\u5f00\u9500\u5b8c\u5168\u53ef\u4ee5\u63a5\u53d7\u3002<\/p>\n<p>\u7b2c\u4e09\u5c42\u7f13\u5b58\u7a7a\u503c&#xff0c;\u5e03\u9686\u8fc7\u6ee4\u5668\u6709\u8bef\u5224\u7387&#xff0c;\u8bf4\u2019\u53ef\u80fd\u5b58\u5728\u2019\u4f46\u6570\u636e\u5e93\u67e5\u4e0d\u5230\u7684&#xff0c;\u6211\u4eec\u7f13\u5b58\u4e00\u4e2a\u7a7a\u6807\u8bb0&#xff0c;TTL \u53ea\u8bbe 60 \u79d2\u2014\u2014\u8bbe\u77ed\u662f\u56e0\u4e3a\u4e07\u4e00\u8fd9\u4e2a\u5546\u54c1\u540e\u6765\u771f\u7684\u4e0a\u67b6\u4e86&#xff0c;60 \u79d2\u540e\u7f13\u5b58\u8fc7\u671f\u5c31\u80fd\u67e5\u5230&#xff0c;\u4e0d\u4f1a\u957f\u65f6\u95f4\u8bef\u4f24\u3002<\/p>\n<p>\u4e09\u5c42\u662f\u4e92\u8865\u5173\u7cfb&#xff1a;\u6821\u9a8c\u6321\u683c\u5f0f\u975e\u6cd5\u7684&#xff0c;\u5e03\u9686\u6321\u683c\u5f0f\u5408\u6cd5\u4f46\u4e0d\u5b58\u5728\u7684&#xff0c;\u7a7a\u503c\u6321\u5e03\u9686\u8bef\u5224\u7684\u3002&#034;<\/p>\n<p>\u51fb\u7a7f&#xff08;50 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u51fb\u7a7f\u6211\u4eec\u7528\u4e92\u65a5\u9501\u91cd\u5efa&#xff1a;\u7f13\u5b58\u672a\u547d\u4e2d\u65f6&#xff0c;\u5148 SETNX \u62a2\u4e00\u628a\u9501&#xff0c;\u62a2\u5230\u7684\u7ebf\u7a0b\u53bb\u67e5\u5e93\u3001\u91cd\u5efa\u7f13\u5b58\u3001\u91ca\u653e\u9501&#xff1b;\u6ca1\u62a2\u5230\u7684\u7ebf\u7a0b\u7761 50 \u6beb\u79d2\u540e\u91cd\u8bd5\u8bfb\u7f13\u5b58&#xff0c;\u8fd9\u65f6\u5019\u5927\u6982\u7387\u5df2\u7ecf\u91cd\u5efa\u597d\u4e86\u3002<\/p>\n<p>\u4e24\u4e2a\u7ec6\u8282&#xff1a;\u4e00\u662f\u62a2\u5230\u9501\u4e4b\u540e\u8981\u518d\u67e5\u4e00\u6b21\u7f13\u5b58&#xff08;\u53cc\u91cd\u68c0\u67e5&#xff09;&#xff0c;\u56e0\u4e3a\u53ef\u80fd\u5728\u4f60\u62a2\u9501\u7684\u77ac\u95f4\u522b\u4eba\u5df2\u7ecf\u91cd\u5efa\u5b8c\u4e86&#xff0c;\u4e0d\u67e5\u8fd9\u4e00\u4e0b\u5c31\u767d\u8dd1\u4e00\u8d9f\u6570\u636e\u5e93&#xff1b;\u4e8c\u662f\u9501\u5fc5\u987b\u5e26\u8fc7\u671f\u65f6\u95f4&#xff0c;\u6211\u4eec\u8bbe 10 \u79d2&#xff0c;\u9632\u6b62\u6301\u9501\u7ebf\u7a0b\u5b95\u673a\u53d8\u6210\u6b7b\u9501\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e2a\u53d6\u820d\u53ef\u4ee5\u804a&#xff1a;\u53e6\u4e00\u79cd\u65b9\u6848\u662f\u2019\u903b\u8f91\u8fc7\u671f\u2019\u2014\u2014\u7f13\u5b58\u6c38\u4e0d\u771f\u8fc7\u671f&#xff0c;value \u91cc\u5b58\u4e2a\u8fc7\u671f\u65f6\u95f4\u5b57\u6bb5&#xff0c;\u53d1\u73b0\u903b\u8f91\u8fc7\u671f\u5c31\u5f02\u6b65\u91cd\u5efa\u3001\u5f53\u524d\u8bf7\u6c42\u5148\u8fd4\u56de\u65e7\u6570\u636e\u3002\u8fd9\u4e2a\u65b9\u6848\u53ef\u7528\u6027\u66f4\u597d\u4f46\u4f1a\u77ed\u6682\u8fd4\u56de\u65e7\u6570\u636e\u3002\u5546\u54c1\u8be6\u60c5\u91cc\u6709\u4ef7\u683c&#xff0c;\u8fd4\u56de\u65e7\u4ef7\u683c\u53ef\u80fd\u5f15\u53d1\u5ba2\u8bc9&#xff0c;\u6240\u4ee5\u6211\u4eec\u9009\u4e86\u4e92\u65a5\u9501&#xff0c;\u5b81\u53ef\u8ba9\u6781\u5c11\u6570\u8bf7\u6c42\u7b49 50 \u6beb\u79d2&#xff0c;\u4e5f\u4e0d\u8fd4\u56de\u810f\u6570\u636e\u3002&#034;<\/p>\n<p>\u96ea\u5d29&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u96ea\u5d29\u7684\u9632\u5fa1\u662f\u5206\u5c42\u7684&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;TTL \u52a0\u968f\u673a\u503c&#xff0c;\u6211\u4eec\u7684\u57fa\u7840 TTL \u662f 30 \u5206\u949f&#xff0c;\u518d\u52a0 0 \u5230 300 \u79d2\u7684\u968f\u673a\u6570&#xff0c;\u628a\u8fc7\u671f\u65f6\u95f4\u6253\u6563&#xff0c;\u907f\u514d\u540c\u4e00\u6279\u7f13\u5b58\u96c6\u4f53\u9635\u4ea1\u3002\u8fd9\u662f\u6700\u7b80\u5355\u4e5f\u6700\u6709\u6548\u7684\u4e00\u62db\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;Redis \u672c\u8eab\u9ad8\u53ef\u7528&#xff0c;\u54e8\u5175\u90e8\u7f72&#xff0c;\u5355\u8282\u70b9\u6302\u4e86\u81ea\u52a8\u5207\u6362&#xff0c;\u907f\u514d\u2019Redis \u6574\u4f53\u4e0d\u53ef\u7528\u2019\u8fd9\u79cd\u6700\u4e25\u91cd\u7684\u96ea\u5d29\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;\u9650\u6d41\u964d\u7ea7\u515c\u5e95&#xff0c;\u4e07\u4e00\u6570\u636e\u5e93\u538b\u529b\u8fd8\u662f\u8d85\u4e86\u9608\u503c&#xff0c;\u89e6\u53d1\u9650\u6d41&#xff0c;\u90e8\u5206\u8bf7\u6c42\u5feb\u901f\u5931\u8d25\u8fd4\u56de\u2019\u7a0d\u540e\u518d\u8bd5\u2019&#xff0c;\u4fdd\u4f4f\u6570\u636e\u5e93\u4e0d\u88ab\u6253\u6b7b\u2014\u2014\u6570\u636e\u5e93\u6d3b\u7740&#xff0c;\u7cfb\u7edf\u5c31\u8fd8\u80fd\u6162\u6162\u6062\u590d\u3002&#034;<\/p>\n<p>\u6536\u5c3e&#xff08;10 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u8fd9\u4e09\u4e2a\u95ee\u9898\u6211\u4eec\u7cfb\u7edf\u91cc\u662f\u540c\u65f6\u90e8\u7f72\u4e86\u6240\u6709\u65b9\u6848\u7684&#xff0c;\u4e0d\u662f\u4e8c\u9009\u4e00&#xff0c;\u7eb5\u6df1\u9632\u5fa1\u561b\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u4e09\u8005\u672c\u8d28\u5bf9\u7167&#xff1a;<\/p>\n<table>\n<tr>\u95ee\u9898\u672c\u8d28\u8868\u73b0<\/tr>\n<tbody>\n<tr>\n<td>\u7f13\u5b58\u7a7f\u900f<\/td>\n<td>\u67e5\u8be2\u4e0d\u5b58\u5728\u7684\u6570\u636e&#xff0c;\u7f13\u5b58\u548c DB \u90fd\u6ca1\u6709<\/td>\n<td>\u8bf7\u6c42\u5168\u90e8\u6253\u5230 DB<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u51fb\u7a7f<\/td>\n<td>\u70ed\u70b9 key \u8fc7\u671f&#xff0c;\u5927\u91cf\u5e76\u53d1\u540c\u65f6\u67e5<\/td>\n<td>\u77ac\u95f4 DB \u538b\u529b\u66b4\u589e<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u96ea\u5d29<\/td>\n<td>\u5927\u91cf key \u540c\u65f6\u8fc7\u671f\u6216 Redis \u5b95\u673a<\/td>\n<td>DB \u88ab\u538b\u57ae&#xff0c;\u7cfb\u7edf\u5d29\u6e83<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7a7f\u900f\u9632\u5fa1\u5b8c\u6574\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token class-name\">Product<\/span> <span class=\"token function\">getProduct<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Long<\/span> productId<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ 0. \u53c2\u6570\u6821\u9a8c&#xff1a;\u62e6\u622a\u975e\u6cd5 ID<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>productId <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">||<\/span> productId <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 1. \u5e03\u9686\u8fc7\u6ee4\u5668\u62e6\u622a\u4e0d\u5b58\u5728\u7684 ID<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>bloomService<span class=\"token punctuation\">.<\/span><span class=\"token function\">mightContain<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;product:bloom&#034;<\/span><span class=\"token punctuation\">,<\/span> productId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u4e00\u5b9a\u4e0d\u5b58\u5728&#xff0c;\u4e0d\u67e5\u5e93<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 2. \u67e5\u7f13\u5b58<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> key <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;product:detail:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> productId<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">Product<\/span> product <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Product<\/span><span class=\"token punctuation\">)<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>product <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token constant\">NULL_PLACEHOLDER<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">equals<\/span><span class=\"token punctuation\">(<\/span>product<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u7a7a\u503c\u6807\u8bb0<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> product<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ 3. \u67e5\u5e93\u5e76\u56de\u5199\u7f13\u5b58<\/span><br \/>\n    product <span class=\"token operator\">&#061;<\/span> productDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">selectById<\/span><span class=\"token punctuation\">(<\/span>productId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>product <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> product<span class=\"token punctuation\">,<\/span> <span class=\"token number\">30<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">MINUTES<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u7f13\u5b58\u7a7a\u503c\u9632\u7a7f\u900f&#xff08;TTL \u77ed&#xff0c;60 \u79d2&#xff09;<\/span><br \/>\n        redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> <span class=\"token constant\">NULL_PLACEHOLDER<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">60<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">SECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> product<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5e03\u9686\u8fc7\u6ee4\u5668\u53c2\u6570&#xff1a;\u9884\u4f30 100 \u4e07\u5546\u54c1&#xff0c;\u8bef\u5224\u7387 0.01%&#xff0c;\u8ba1\u7b97\u5f97\u4f4d\u56fe\u7ea6 19MB&#xff0c;\u54c8\u5e0c\u51fd\u6570 14 \u4e2a\u3002<\/p>\n<p>\u51fb\u7a7f\u9632\u5fa1\u5b8c\u6574\u4ee3\u7801&#xff08;\u4e92\u65a5\u9501 &#043; \u53cc\u91cd\u68c0\u67e5&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token class-name\">Product<\/span> <span class=\"token function\">getProductWithMutex<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Long<\/span> productId<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> key <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;product:detail:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> productId<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token class-name\">Product<\/span> product <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Product<\/span><span class=\"token punctuation\">)<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>product <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span> product<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7f13\u5b58\u672a\u547d\u4e2d&#xff0c;\u5c1d\u8bd5\u83b7\u53d6\u91cd\u5efa\u9501<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> lockKey <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;lock:product:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> productId<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">boolean<\/span> locked <span class=\"token operator\">&#061;<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">setIfAbsent<\/span><span class=\"token punctuation\">(<\/span>lockKey<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;1&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">SECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>locked<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">try<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u53cc\u91cd\u68c0\u67e5&#xff1a;\u62ff\u5230\u9501\u540e\u518d\u67e5\u4e00\u6b21&#xff08;\u53ef\u80fd\u522b\u4eba\u5df2\u91cd\u5efa&#xff09;<\/span><br \/>\n            product <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Product<\/span><span class=\"token punctuation\">)<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>product <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">return<\/span> product<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u67e5\u5e93\u91cd\u5efa<\/span><br \/>\n            product <span class=\"token operator\">&#061;<\/span> productDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">selectById<\/span><span class=\"token punctuation\">(<\/span>productId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">set<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> product<span class=\"token punctuation\">,<\/span> <span class=\"token number\">30<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">MINUTES<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> product<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">finally<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">delete<\/span><span class=\"token punctuation\">(<\/span>lockKey<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u672a\u83b7\u53d6\u5230\u9501&#xff0c;\u77ed\u6682\u7b49\u5f85\u540e\u91cd\u8bd5<\/span><br \/>\n        <span class=\"token class-name\">Thread<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">sleep<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token function\">getProductWithMutex<\/span><span class=\"token punctuation\">(<\/span>productId<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u96ea\u5d29\u9632\u5fa1\u56db\u5c42&#xff1a;<\/p>\n<li>TTL \u52a0\u968f\u673a\u503c&#xff1a;30\u5206\u949f &#043; random(0, 300\u79d2)&#xff0c;\u8ba9\u8fc7\u671f\u65f6\u95f4\u5206\u6563\u3002<\/li>\n<li>\u591a\u7ea7\u7f13\u5b58&#xff1a;\u672c\u5730\u7f13\u5b58&#xff08;Caffeine&#xff0c;TTL 10 \u79d2&#xff09;&#043; Redis\u3002Redis \u4e0d\u53ef\u7528\u65f6\u672c\u5730\u7f13\u5b58\u8fd8\u80fd\u625b 10 \u79d2&#xff0c;\u7ed9\u964d\u7ea7\u4e89\u53d6\u65f6\u95f4\u3002<\/li>\n<li>Redis \u9ad8\u53ef\u7528&#xff1a;\u54e8\u5175\u6216\u96c6\u7fa4\u6a21\u5f0f&#xff0c;\u5355\u8282\u70b9\u5b95\u673a\u81ea\u52a8\u5207\u6362\u3002<\/li>\n<li>\u9650\u6d41\u964d\u7ea7&#xff1a;DB \u538b\u529b\u8d85\u9608\u503c\u65f6\u89e6\u53d1\u9650\u6d41&#xff0c;\u90e8\u5206\u8bf7\u6c42\u5feb\u901f\u5931\u8d25&#xff0c;\u4fdd\u62a4 DB\u3002<\/li>\n<p>\u4e09\u8005\u65b9\u6848\u603b\u8868&#xff1a;<\/p>\n<table>\n<tr>\u95ee\u9898\u672c\u8d28\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>\u7a7f\u900f<\/td>\n<td>\u67e5\u4e0d\u5b58\u5728\u7684\u6570\u636e<\/td>\n<td>\u53c2\u6570\u6821\u9a8c &#043; \u5e03\u9686\u8fc7\u6ee4\u5668 &#043; \u7f13\u5b58\u7a7a\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u51fb\u7a7f<\/td>\n<td>\u70ed\u70b9 key \u8fc7\u671f<\/td>\n<td>\u4e92\u65a5\u9501\u91cd\u5efa &#043; \u53cc\u91cd\u68c0\u67e5<\/td>\n<\/tr>\n<tr>\n<td>\u96ea\u5d29<\/td>\n<td>\u5927\u91cf key \u540c\u65f6\u5931\u6548<\/td>\n<td>\u968f\u673a TTL &#043; \u591a\u7ea7\u7f13\u5b58 &#043; \u9ad8\u53ef\u7528 &#043; \u9650\u6d41<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>\u5f00\u5934\u4e00\u53e5\u8bdd\u533a\u5206\u4e09\u4e2a\u6982\u5ff5&#xff0c;\u76f4\u63a5\u544a\u8bc9\u9762\u8bd5\u5b98&#034;\u6211\u6ca1\u6df7&#034;&#xff1b;<\/li>\n<li>\u6bcf\u4e2a\u65b9\u6848\u90fd\u5e26\u53c2\u6570\u548c\u53d6\u820d&#xff08;19MB\u300160 \u79d2\u300150 \u6beb\u79d2\u3001\u4e3a\u4ec0\u4e48\u4e0d\u7528\u903b\u8f91\u8fc7\u671f&#xff09;&#xff0c;\u8fd9\u4e9b\u5177\u4f53\u6570\u5b57\u662f&#034;\u505a\u8fc7&#034;\u7684\u6700\u5f3a\u4fe1\u53f7\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u4e5d&#xff1a;\u7f13\u5b58\u548c\u6570\u636e\u5e93\u7684\u4e00\u81f4\u6027<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u8fd0\u8425\u6539\u4e86\u5546\u54c1\u4ef7\u683c&#xff0c;\u7f13\u5b58\u91cc\u8fd8\u662f\u65e7\u7684&#xff0c;\u600e\u4e48\u5904\u7406&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u5b9a\u8c03&#xff08;15 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u5148\u660e\u786e\u76ee\u6807&#xff1a;\u5546\u54c1\u4ef7\u683c\u8fd9\u79cd\u573a\u666f\u6211\u4eec\u8ffd\u6c42\u7684\u662f\u6700\u7ec8\u4e00\u81f4&#xff0c;\u5141\u8bb8\u79d2\u7ea7\u7684\u5ef6\u8fdf\u7a97\u53e3&#xff0c;\u4f46\u4e0d\u80fd\u957f\u65f6\u95f4\u4e0d\u4e00\u81f4\u2014\u2014\u7528\u6237\u770b\u5230\u65e7\u4ef7\u683c\u4e0b\u5355&#xff0c;\u662f\u8981\u5ba2\u8bc9\u7684\u3002\u201d<\/p>\n<p>\u4e3b\u65b9\u6848 &#043; \u4e3a\u4ec0\u4e48&#xff08;45 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u65b9\u6848\u662f\u7ecf\u5178\u7684 Cache-Aside&#xff1a;\u5148\u66f4\u65b0\u6570\u636e\u5e93&#xff0c;\u518d\u5220\u9664\u7f13\u5b58&#xff0c;\u4e0b\u6b21\u8bfb\u7684\u65f6\u5019\u81ea\u52a8\u91cd\u5efa\u3002<\/p>\n<p>\u8fd9\u91cc\u6709\u4e24\u4e2a\u2019\u4e3a\u4ec0\u4e48\u2019\u503c\u5f97\u8bf4&#xff1a;<\/p>\n<p>\u4e3a\u4ec0\u4e48\u662f\u2019\u5220\u7f13\u5b58\u2019\u800c\u4e0d\u662f\u2019\u66f4\u65b0\u7f13\u5b58\u2019&#xff1f;\u56e0\u4e3a\u5e76\u53d1\u5199\u7684\u65f6\u5019&#xff0c;\u66f4\u65b0\u7f13\u5b58\u53ef\u80fd\u4e92\u76f8\u8986\u76d6\u2014\u2014\u4e24\u4e2a\u66f4\u65b0\u8bf7\u6c42&#xff0c;\u540e\u5230\u7684\u6570\u636e\u5e93\u66f4\u65b0\u53ef\u80fd\u5148\u5199\u7f13\u5b58&#xff0c;\u7ed3\u679c\u7f13\u5b58\u91cc\u662f\u65e7\u503c\u3002\u5220\u9664\u5c31\u6ca1\u8fd9\u4e2a\u95ee\u9898&#xff0c;\u53cd\u6b63\u4e0b\u6b21\u8bfb\u7684\u65f6\u5019\u91cd\u5efa\u7684\u4e00\u5b9a\u662f\u6570\u636e\u5e93\u6700\u65b0\u503c\u3002\u800c\u4e14\u5982\u679c\u8fd9\u4e2a\u5546\u54c1\u66f4\u65b0\u540e\u534a\u5929\u6ca1\u4eba\u770b&#xff0c;\u5220\u9664\u6bd4\u66f4\u65b0\u7701\u8d44\u6e90\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5148\u6570\u636e\u5e93\u540e\u7f13\u5b58&#xff1f;\u53cd\u8fc7\u6765\u7684\u8bdd&#xff0c;\u5148\u5220\u7f13\u5b58\u518d\u66f4\u65b0\u6570\u636e\u5e93&#xff0c;\u4e2d\u95f4\u7a97\u53e3\u91cc\u6765\u4e2a\u8bfb\u8bf7\u6c42&#xff0c;\u628a\u6570\u636e\u5e93\u7684\u65e7\u503c\u53c8\u5199\u56de\u7f13\u5b58\u4e86&#xff0c;\u4e0d\u4e00\u81f4\u4f1a\u6301\u7eed\u5230 TTL \u5230\u671f&#xff0c;\u7a97\u53e3\u5927\u5f97\u591a\u3002&#034;<\/p>\n<p>\u627f\u8ba4\u6b8b\u4f59\u95ee\u9898 &#043; \u5ef6\u8fdf\u53cc\u5220&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u4f46 Cache-Aside \u5728\u6781\u7aef\u5e76\u53d1\u4e0b\u8fd8\u662f\u6709\u4e2a\u5c0f\u7a97\u53e3&#xff1a;\u8bfb\u8bf7\u6c42\u67e5\u5b8c\u6570\u636e\u5e93\u8fd8\u6ca1\u5199\u56de\u7f13\u5b58\u7684\u77ac\u95f4&#xff0c;\u5199\u8bf7\u6c42\u5b8c\u6210\u4e86\u66f4\u65b0\u548c\u5220\u9664&#xff0c;\u7136\u540e\u8bfb\u8bf7\u6c42\u628a\u65e7\u503c\u5199\u4e86\u56de\u53bb\u3002\u8fd9\u4e2a\u7a97\u53e3\u662f\u6beb\u79d2\u7ea7\u7684&#xff0c;\u53d1\u751f\u6761\u4ef6\u5f88\u82db\u523b&#xff0c;\u4f46\u7406\u8bba\u4e0a\u5b58\u5728\u3002<\/p>\n<p>\u6211\u4eec\u7684\u52a0\u56fa\u662f\u5ef6\u8fdf\u53cc\u5220&#xff1a;\u66f4\u65b0\u5b8c\u6570\u636e\u5e93\u540e&#xff0c;\u5ef6\u8fdf 500 \u6beb\u79d2\u518d\u5220\u4e00\u6b21\u7f13\u5b58&#xff0c;\u628a\u7a97\u53e3\u671f\u5185\u53ef\u80fd\u88ab\u5199\u56de\u7684\u65e7\u503c\u6e05\u6389\u3002500 \u6beb\u79d2\u8fd9\u4e2a\u6570\u4e0d\u662f\u62cd\u7684\u2014\u2014\u5b83\u8981\u5927\u4e8e\u2019\u4e00\u6b21\u8bfb\u8bf7\u6c42\u4ece\u67e5\u5e93\u5230\u5199\u7f13\u5b58\u2019\u7684\u8017\u65f6&#xff0c;\u6211\u4eec\u5546\u54c1\u67e5\u8be2 P99 \u5728 100 \u6beb\u79d2\u4ee5\u5185&#xff0c;\u7559\u4e86 5 \u500d\u4f59\u91cf\u3002&#034;<\/p>\n<p>\u515c\u5e95&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6700\u540e TTL \u672c\u8eab\u4e5f\u662f\u4fdd\u9669&#xff1a;\u5c31\u7b97\u524d\u9762\u6240\u6709\u673a\u5236\u90fd\u5931\u6548\u4e86&#xff0c;30 \u5206\u949f\u540e\u7f13\u5b58\u81ea\u7136\u8fc7\u671f\u91cd\u5efa\u3002\u4e00\u81f4\u6027\u65b9\u6848\u6c38\u8fdc\u662f\u591a\u5c42\u53e0\u52a0\u7684&#xff0c;\u4e0d\u6307\u671b\u4efb\u4f55\u5355\u5c42\u767e\u5206\u4e4b\u767e\u53ef\u9760\u3002\u5982\u679c\u672a\u6765\u5546\u54c1\u53d8\u66f4\u9891\u7387\u4e0a\u6765\u4e86&#xff0c;\u6bd4\u5982\u8981\u505a\u5b9e\u65f6\u8c03\u4ef7&#xff0c;\u6211\u4eec\u4f1a\u5347\u7ea7\u6210 Canal \u76d1\u542c binlog \u5f02\u6b65\u5220\u7f13\u5b58&#xff0c;\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165&#xff0c;\u53ef\u9760\u6027\u4e5f\u66f4\u9ad8\u2014\u2014\u76ee\u524d\u7684\u53d8\u66f4\u9891\u7387\u7528\u5ef6\u8fdf\u53cc\u5220\u591f\u4e86&#xff0c;\u6ca1\u5fc5\u8981\u4e0a\u91cd\u6b66\u5668\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u56db\u79cd\u7b56\u7565\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u7b56\u7565\u505a\u6cd5\u95ee\u9898<\/tr>\n<tbody>\n<tr>\n<td>\u5148\u66f4\u65b0\u7f13\u5b58&#xff0c;\u518d\u66f4\u65b0 DB<\/td>\n<td>\u274c<\/td>\n<td>DB \u66f4\u65b0\u5931\u8d25&#xff0c;\u7f13\u5b58\u662f\u810f\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>\u5148\u66f4\u65b0 DB&#xff0c;\u518d\u66f4\u65b0\u7f13\u5b58<\/td>\n<td>\u274c<\/td>\n<td>\u5e76\u53d1\u4e0b\u53ef\u80fd\u5199\u5165\u65e7\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u5148\u5220\u7f13\u5b58&#xff0c;\u518d\u66f4\u65b0 DB<\/td>\n<td>\u274c<\/td>\n<td>\u5e76\u53d1\u8bfb\u53ef\u80fd\u628a\u65e7\u503c\u5199\u56de\u7f13\u5b58<\/td>\n<\/tr>\n<tr>\n<td>\u5148\u66f4\u65b0 DB&#xff0c;\u518d\u5220\u7f13\u5b58&#xff08;\u63a8\u8350&#xff09;<\/td>\n<td>\u2705<\/td>\n<td>\u6781\u7aef\u5e76\u53d1\u4ecd\u6709\u77ed\u6682\u7a97\u53e3&#xff0c;\u6982\u7387\u6781\u4f4e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u57fa\u7840\u7248\u4ee3\u7801&#xff08;Cache-Aside&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">updateProduct<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Product<\/span> product<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ 1. \u5148\u66f4\u65b0\u6570\u636e\u5e93<\/span><br \/>\n    productDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">updateById<\/span><span class=\"token punctuation\">(<\/span>product<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ 2. \u5220\u9664\u7f13\u5b58&#xff08;\u4e0b\u6b21\u8bfb\u65f6\u91cd\u5efa&#xff09;<\/span><br \/>\n    redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">delete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;product:detail:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> product<span class=\"token punctuation\">.<\/span><span class=\"token function\">getId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5e76\u53d1\u4e0d\u4e00\u81f4\u7a97\u53e3\u7684\u65f6\u95f4\u7ebf&#xff1a;<\/p>\n<p>T1: \u7ebf\u7a0bA \u8bfb\u7f13\u5b58\u672a\u547d\u4e2d&#xff0c;\u67e5 DB \u5f97\u5230\u65e7\u503c<br \/>\nT2: \u7ebf\u7a0bB \u66f4\u65b0 DB \u4e3a\u65b0\u503c<br \/>\nT3: \u7ebf\u7a0bB \u5220\u9664\u7f13\u5b58<br \/>\nT4: \u7ebf\u7a0bA \u5c06\u65e7\u503c\u5199\u5165\u7f13\u5b58  \u2190 \u4e0d\u4e00\u81f4&#xff01;<\/p>\n<p>\u5ef6\u8fdf\u53cc\u5220\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token keyword\">public<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">updateProductWithDoubleDelete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Product<\/span> product<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">String<\/span> key <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;product:detail:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> product<span class=\"token punctuation\">.<\/span><span class=\"token function\">getId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ 1. \u5148\u5220\u7f13\u5b58<\/span><br \/>\n    redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">delete<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ 2. \u66f4\u65b0\u6570\u636e\u5e93<\/span><br \/>\n    productDao<span class=\"token punctuation\">.<\/span><span class=\"token function\">updateById<\/span><span class=\"token punctuation\">(<\/span>product<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ 3. \u5ef6\u8fdf 500ms \u518d\u5220\u4e00\u6b21&#xff08;\u8986\u76d6\u5e76\u53d1\u8bfb\u5199\u5165\u7684\u65e7\u503c&#xff09;<\/span><br \/>\n    <span class=\"token function\">delayDelete<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> <span class=\"token number\">500<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span> <span class=\"token keyword\">void<\/span> <span class=\"token function\">delayDelete<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> key<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">long<\/span> delayMs<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    scheduledExecutor<span class=\"token punctuation\">.<\/span><span class=\"token function\">schedule<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">delete<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n            delayMs<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">MILLISECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u4e3a\u4ec0\u4e48\u5ef6\u8fdf 500ms&#xff1a;\u8981\u5927\u4e8e&#034;\u4e00\u6b21\u8bfb\u8bf7\u6c42\u4ece\u67e5\u5e93\u5230\u5199\u7f13\u5b58&#034;\u7684\u8017\u65f6\u3002\u901a\u5e38\u5546\u54c1\u67e5\u8be2\u5728 100ms \u4ee5\u5185&#xff0c;500ms \u7559\u4e86\u5145\u8db3\u4f59\u91cf\u3002\u8bbe\u592a\u77ed&#xff0c;\u53ef\u80fd\u65e7\u503c\u8fd8\u6ca1\u5199\u5165\u5c31\u5220\u4e86&#xff0c;\u8d77\u4e0d\u5230\u4f5c\u7528&#xff1b;\u8bbe\u592a\u957f&#xff0c;\u4e0d\u4e00\u81f4\u7a97\u53e3\u53d8\u5927\u3002<\/p>\n<p>Canal \u65b9\u6848&#xff08;\u66f4\u9ad8\u53ef\u9760\u6027&#xff09;&#xff1a;<\/p>\n<p>DB \u66f4\u65b0 \u2192 binlog \u2192 Canal \u2192 MQ \u2192 \u7f13\u5b58\u5220\u9664\u670d\u52a1 \u2192 \u5220\u9664 Redis<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165\u3001\u53ef\u9760\u6027\u9ad8&#xff08;binlog \u4e0d\u4f1a\u4e22&#xff09;\u3001\u89e3\u8026\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u67b6\u6784\u590d\u6742\u5ea6\u589e\u52a0\u3001\u6709 1~2 \u79d2\u5ef6\u8fdf\u3002<\/li>\n<li>\u9009\u62e9\u4f9d\u636e&#xff1a;\u5546\u54c1\u66f4\u65b0\u9891\u7387\u4f4e&#xff08;\u8fd0\u8425\u64cd\u4f5c&#xff09;\u7528\u5ef6\u8fdf\u53cc\u5220\u8db3\u591f&#xff1b;\u53d8\u66f4\u9891\u7e41&#xff08;\u5b9e\u65f6\u8c03\u4ef7&#xff09;\u518d\u5347\u7ea7 Canal\u3002<\/li>\n<\/ul>\n<p>TTL \u515c\u5e95&#xff1a;\u65e0\u8bba\u54ea\u79cd\u65b9\u6848&#xff0c;\u90fd\u8bbe\u7f6e 30 \u5206\u949f TTL\u3002\u5373\u4f7f\u6240\u6709\u4e3b\u52a8\u5931\u6548\u673a\u5236\u90fd\u5931\u8d25&#xff0c;\u7f13\u5b58\u6700\u591a 30 \u5206\u949f\u540e\u81ea\u52a8\u8fc7\u671f\u91cd\u5efa\u3002TTL \u662f\u4e00\u81f4\u6027\u7684\u6700\u540e\u4fdd\u9669\u3002<\/p>\n<hr \/>\n<h3>\u573a\u666f\u5341&#xff1a;\u79d2\u6740\u63a5\u53e3\u9650\u6d41\u9632\u5237<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u79d2\u6740\u63a5\u53e3\u600e\u4e48\u9632\u811a\u672c\u5237&#xff1f;\u600e\u4e48\u9650\u5236\u5355\u4e2a\u7528\u6237\u7684\u9891\u7387&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u7ed9\u5168\u666f&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u9632\u5237\u4e0d\u662f\u5355\u70b9\u505a\u7684&#xff0c;\u662f\u4e00\u4e2a\u591a\u5c42\u6f0f\u6597&#xff1a;\u524d\u7aef\u6309\u94ae\u7f6e\u7070\u52a0\u9a8c\u8bc1\u7801\u62c9\u957f\u8bf7\u6c42\u95f4\u9694&#xff0c;\u7f51\u5173\u5c42\u6309 IP \u9650\u6d41\u6321\u811a\u672c&#xff0c;\u5e94\u7528\u5c42\u6309\u7528\u6237\u7ef4\u5ea6\u9650\u6d41&#xff0c;\u6700\u540e\u4e1a\u52a1\u5c42\u8fd8\u6709\u5e93\u5b58\u539f\u5b50\u6263\u51cf\u515c\u5e95\u3002\u6211\u91cd\u70b9\u8bb2\u5e94\u7528\u5c42&#xff0c;\u8fd9\u662f\u6211\u4eec\u81ea\u5df1\u5199\u7684\u90e8\u5206\u3002\u201d<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9009\u4ee4\u724c\u6876&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u9650\u6d41\u7b97\u6cd5\u6211\u4eec\u5bf9\u6bd4\u8fc7&#xff1a;\u56fa\u5b9a\u7a97\u53e3\u6709\u4e34\u754c\u7a81\u523a\u95ee\u9898\u2014\u2014\u7a97\u53e3\u4ea4\u754c\u5904\u524d\u540e\u5404\u6765\u4e00\u6ce2&#xff0c;\u5b9e\u9645\u77ac\u65f6\u6d41\u91cf\u662f\u9608\u503c\u7684\u4e24\u500d&#xff1b;\u6f0f\u6876\u592a\u6b7b\u677f&#xff0c;\u5b8c\u5168\u4e0d\u5141\u8bb8\u7a81\u53d1&#xff0c;\u7528\u6237\u6b63\u5e38\u624b\u901f\u5feb\u70b9\u4e24\u4e0b\u90fd\u4f1a\u88ab\u62d2\u3002\u6700\u540e\u9009\u7684\u4ee4\u724c\u6876&#xff1a;\u6876\u91cc\u5300\u901f\u653e\u4ee4\u724c&#xff0c;\u8bf7\u6c42\u6765\u4e86\u53d6\u4ee4\u724c&#xff0c;\u53d6\u5230\u5c31\u653e\u884c\u3002\u5b83\u65e2\u80fd\u63a7\u5236\u5e73\u5747\u901f\u7387&#xff0c;\u53c8\u5141\u8bb8\u77ed\u65f6\u95f4\u7a81\u53d1&#xff0c;\u5bf9\u6b63\u5e38\u7528\u6237\u6700\u53cb\u597d\u3002\u201d<\/p>\n<p>\u5b9e\u73b0\u8981\u70b9&#xff08;50 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u5b9e\u73b0\u662f\u4e00\u6bb5 Lua \u811a\u672c&#xff0c;\u6838\u5fc3\u903b\u8f91\u662f&#xff1a;\u8bb0\u5f55\u4e0a\u6b21\u8bbf\u95ee\u65f6\u95f4\u548c\u5f53\u524d\u4ee4\u724c\u6570&#xff0c;\u8bf7\u6c42\u6765\u4e86\u5148\u6309\u2019\u65f6\u95f4\u5dee \u00d7 \u901f\u7387\u2019\u8865\u5145\u4ee4\u724c&#xff08;\u4e0d\u8d85\u8fc7\u6876\u5bb9\u91cf&#xff09;&#xff0c;\u7136\u540e\u770b\u4ee4\u724c\u591f\u4e0d\u591f&#xff0c;\u591f\u5c31\u6263\u4e00\u4e2a\u653e\u884c&#xff0c;\u4e0d\u591f\u5c31\u62d2\u7edd\u3002<\/p>\n<p>\u5fc5\u987b\u7528 Lua \u7684\u539f\u56e0\u548c\u4e4b\u524d\u4e00\u6837&#xff1a;&#039;\u8bfb\u4ee4\u724c\u6570 \u2192 \u8ba1\u7b97 \u2192 \u5199\u56de\u2019\u8fd9\u4e09\u6b65\u5982\u679c\u6709\u5e76\u53d1\u7a97\u53e3&#xff0c;\u4e24\u4e2a\u8bf7\u6c42\u540c\u65f6\u8bfb\u5230\u8fd8\u5269 1 \u4e2a\u4ee4\u724c&#xff0c;\u5c31\u90fd\u653e\u884c\u4e86&#xff0c;\u9650\u6d41\u5f62\u540c\u865a\u8bbe\u3002Lua \u5728 Redis \u91cc\u539f\u5b50\u6267\u884c&#xff0c;\u6ca1\u8fd9\u4e2a\u95ee\u9898\u3002\u800c\u4e14\u9650\u6d41\u72b6\u6001\u653e Redis \u800c\u4e0d\u662f\u5e94\u7528\u5185\u5b58&#xff0c;\u591a\u5b9e\u4f8b\u90e8\u7f72\u5929\u7136\u5171\u4eab\u540c\u4e00\u4e2a\u6876&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u2019\u6bcf\u4e2a\u5b9e\u4f8b\u5404\u9650\u5404\u7684\u3001\u603b\u91cf\u7ffb\u500d\u2019\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u53c2\u6570\u4e0a&#xff0c;\u79d2\u6740\u63a5\u53e3\u6211\u4eec\u914d\u7684\u662f\u6876\u5bb9\u91cf 5\u3001\u6bcf\u79d2\u8865 2 \u4e2a\u4ee4\u724c\u2014\u2014\u610f\u601d\u662f\u7528\u6237\u6700\u591a\u8fde\u70b9 5 \u4e0b&#xff0c;\u4e4b\u540e\u6bcf\u534a\u79d2\u624d\u80fd\u518d\u70b9\u4e00\u4e0b\u3002\u6b63\u5e38\u4eba\u65e0\u611f&#xff0c;\u811a\u672c\u5237\u4e0d\u52a8\u3002<\/p>\n<p>\u8fd8\u6709\u4e2a\u5bb9\u6613\u6f0f\u7684\u7ec6\u8282&#xff1a;\u9650\u6d41\u7684 key \u4e5f\u8981\u8bbe TTL&#xff0c;\u6bd4\u5982 1 \u5c0f\u65f6\u3002\u4e0d\u7136\u7528\u6237\u8d70\u4e86\u4e4b\u540e&#xff0c;\u51e0\u767e\u4e07\u4e2a\u9650\u6d41 key \u6c38\u8fdc\u8eba\u5728 Redis \u91cc\u5403\u5185\u5b58\u3002&#034;<\/p>\n<p>\u88ab\u62d2\u7edd\u540e\u7684\u4f53\u9a8c&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u88ab\u9650\u6d41\u7684\u8bf7\u6c42\u6211\u4eec\u8fd4\u56de\u7684\u662f\u2019\u6d3b\u52a8\u592a\u706b\u7206&#xff0c;\u8bf7\u7a0d\u540e\u518d\u8bd5\u2019&#xff0c;HTTP \u72b6\u6001\u7528 429\u3002\u8fd9\u91cc\u6709\u4e2a\u4ea7\u54c1\u7ec6\u8282&#xff1a;\u63d0\u793a\u6587\u6848\u4e0d\u80fd\u5199\u2019\u4f60\u88ab\u9650\u6d41\u4e86\u2019&#xff0c;\u7528\u6237\u4f1a\u89c9\u5f97\u81ea\u5df1\u88ab\u9488\u5bf9\u4e86&#xff0c;\u4f53\u9a8c\u5f88\u5dee\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u591a\u5c42\u9650\u6d41\u6f0f\u6597&#xff1a;<\/p>\n<table>\n<tr>\u5c42\u4f4d\u7f6e\u9650\u6d41\u7ef4\u5ea6\u5de5\u5177<\/tr>\n<tbody>\n<tr>\n<td>\u7b2c 1 \u5c42<\/td>\n<td>\u524d\u7aef<\/td>\n<td>\u6309\u94ae\u9632\u91cd &#043; \u9a8c\u8bc1\u7801<\/td>\n<td>JS<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c 2 \u5c42<\/td>\n<td>\u7f51\u5173<\/td>\n<td>IP &#043; \u7528\u6237\u7ef4\u5ea6<\/td>\n<td>Nginx\/Gateway<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c 3 \u5c42<\/td>\n<td>\u5e94\u7528<\/td>\n<td>\u7528\u6237 &#043; \u63a5\u53e3\u7ef4\u5ea6<\/td>\n<td>Redis \u4ee4\u724c\u6876<\/td>\n<\/tr>\n<tr>\n<td>\u7b2c 4 \u5c42<\/td>\n<td>\u4e1a\u52a1<\/td>\n<td>\u5e93\u5b58\u7ef4\u5ea6<\/td>\n<td>Redis Lua<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9650\u6d41\u7b97\u6cd5\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u7b97\u6cd5\u539f\u7406\u95ee\u9898<\/tr>\n<tbody>\n<tr>\n<td>\u56fa\u5b9a\u7a97\u53e3<\/td>\n<td>\u65f6\u95f4\u7a97\u5185\u8ba1\u6570<\/td>\n<td>\u4e34\u754c\u7a81\u523a&#xff1a;\u7a97\u53e3\u4ea4\u754c\u5904\u53ef\u80fd 2 \u500d\u6d41\u91cf<\/td>\n<\/tr>\n<tr>\n<td>\u6ed1\u52a8\u7a97\u53e3<\/td>\n<td>\u7ec6\u5206\u65f6\u95f4\u7247<\/td>\n<td>\u5b9e\u73b0\u590d\u6742&#xff0c;\u7cbe\u5ea6\u4e0e\u5185\u5b58\u6743\u8861<\/td>\n<\/tr>\n<tr>\n<td>\u4ee4\u724c\u6876<\/td>\n<td>\u5300\u901f\u653e\u4ee4\u724c&#xff0c;\u8bf7\u6c42\u53d6\u4ee4\u724c<\/td>\n<td>\u2705 \u5e73\u6ed1\u9650\u6d41&#xff0c;\u5141\u8bb8\u9002\u5ea6\u7a81\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u6f0f\u6876<\/td>\n<td>\u5300\u901f\u6d41\u51fa<\/td>\n<td>\u65e0\u6cd5\u5e94\u5bf9\u7a81\u53d1&#xff0c;\u8fc7\u4e8e\u6b7b\u677f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4ee4\u724c\u6876 Lua \u811a\u672c&#xff08;\u9010\u884c\u6ce8\u91ca\u7248&#xff09;&#xff1a;<\/p>\n<p><span class=\"token keyword\">local<\/span> key <span class=\"token operator\">&#061;<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span>                    <span class=\"token comment\">&#8212; \u9650\u6d41\u952e&#xff0c;\u5982 rate:seckill:{userId}<\/span><br \/>\n<span class=\"token keyword\">local<\/span> capacity <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>     <span class=\"token comment\">&#8212; \u6876\u5bb9\u91cf&#xff08;\u6700\u5927\u7a81\u53d1\u91cf&#xff09;<\/span><br \/>\n<span class=\"token keyword\">local<\/span> rate <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>         <span class=\"token comment\">&#8212; \u586b\u5145\u901f\u7387&#xff08;\u6bcf\u79d2\u4ee4\u724c\u6570&#xff09;<\/span><br \/>\n<span class=\"token keyword\">local<\/span> now <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">3<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span>          <span class=\"token comment\">&#8212; \u5f53\u524d\u65f6\u95f4\u6233&#xff08;\u6beb\u79d2&#xff09;<\/span><\/p>\n<p><span class=\"token keyword\">local<\/span> tokens_key <span class=\"token operator\">&#061;<\/span> key <span class=\"token operator\">..<\/span> <span class=\"token string\">&#039;:tokens&#039;<\/span>    <span class=\"token comment\">&#8212; \u5f53\u524d\u4ee4\u724c\u6570<\/span><br \/>\n<span class=\"token keyword\">local<\/span> ts_key <span class=\"token operator\">&#061;<\/span> key <span class=\"token operator\">..<\/span> <span class=\"token string\">&#039;:ts&#039;<\/span>            <span class=\"token comment\">&#8212; \u4e0a\u6b21\u8bbf\u95ee\u65f6\u95f4<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u83b7\u53d6\u5f53\u524d\u4ee4\u724c\u6570&#xff0c;\u4e0d\u5b58\u5728\u5219\u521d\u59cb\u5316\u4e3a\u5bb9\u91cf&#xff08;\u9996\u6b21\u8bf7\u6c42\u6ee1\u6876&#xff09;<\/span><br \/>\n<span class=\"token keyword\">local<\/span> tokens <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;GET&#039;<\/span><span class=\"token punctuation\">,<\/span> tokens_key<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">or<\/span> capacity<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">&#8212; \u83b7\u53d6\u4e0a\u6b21\u65f6\u95f4\u6233&#xff0c;\u4e0d\u5b58\u5728\u5219\u521d\u59cb\u5316\u4e3a\u5f53\u524d\u65f6\u95f4<\/span><br \/>\n<span class=\"token keyword\">local<\/span> last_ts <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">tonumber<\/span><span class=\"token punctuation\">(<\/span>redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;GET&#039;<\/span><span class=\"token punctuation\">,<\/span> ts_key<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">or<\/span> now<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u8ba1\u7b97\u5e94\u8865\u5145\u7684\u4ee4\u724c&#xff1a;\u65f6\u95f4\u5dee(\u79d2) \u00d7 \u901f\u7387<\/span><br \/>\n<span class=\"token keyword\">local<\/span> delta <span class=\"token operator\">&#061;<\/span> math<span class=\"token punctuation\">.<\/span><span class=\"token function\">max<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span>now <span class=\"token operator\">&#8211;<\/span> last_ts<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">\/<\/span> <span class=\"token number\">1000.0<\/span> <span class=\"token operator\">*<\/span> rate<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4ee4\u724c\u6570 &#061; min(\u5bb9\u91cf, \u5f53\u524d &#043; \u8865\u5145)&#xff0c;\u4e0d\u8d85\u8fc7\u5bb9\u91cf<\/span><br \/>\ntokens <span class=\"token operator\">&#061;<\/span> math<span class=\"token punctuation\">.<\/span><span class=\"token function\">min<\/span><span class=\"token punctuation\">(<\/span>capacity<span class=\"token punctuation\">,<\/span> tokens <span class=\"token operator\">&#043;<\/span> delta<span class=\"token punctuation\">)<\/span><\/p>\n<p><span class=\"token keyword\">if<\/span> tokens <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u6709\u4ee4\u724c&#xff1a;\u6d88\u8017 1 \u4e2a&#xff0c;\u653e\u884c<\/span><br \/>\n    redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SET&#039;<\/span><span class=\"token punctuation\">,<\/span> tokens_key<span class=\"token punctuation\">,<\/span> tokens <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SET&#039;<\/span><span class=\"token punctuation\">,<\/span> ts_key<span class=\"token punctuation\">,<\/span> now<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n    <span class=\"token comment\">&#8212; \u65e0\u4ee4\u724c&#xff1a;\u62d2\u7edd&#xff08;\u4f46\u4ecd\u66f4\u65b0\u65f6\u95f4\u6233&#xff0c;\u4fdd\u8bc1\u4e0b\u6b21\u80fd\u6b63\u786e\u8865\u5145&#xff09;<\/span><br \/>\n    redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SET&#039;<\/span><span class=\"token punctuation\">,<\/span> tokens_key<span class=\"token punctuation\">,<\/span> tokens<span class=\"token punctuation\">)<\/span><br \/>\n    redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;SET&#039;<\/span><span class=\"token punctuation\">,<\/span> ts_key<span class=\"token punctuation\">,<\/span> now<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><br \/>\n<span class=\"token keyword\">end<\/span><\/p>\n<p>\u5173\u952e\u8bbe\u8ba1\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u7528\u4e24\u4e2a key&#xff08;tokens \u548c ts&#xff09;&#xff1a;\u5206\u79bb&#034;\u4ee4\u724c\u6570&#034;\u548c&#034;\u65f6\u95f4\u6233&#034;&#xff0c;\u903b\u8f91\u6e05\u6670\u3002<\/li>\n<li>\u62d2\u7edd\u65f6\u4e5f\u66f4\u65b0\u65f6\u95f4\u6233&#xff1a;\u5982\u679c\u4e0d\u66f4\u65b0&#xff0c;\u4e0b\u6b21\u8bf7\u6c42\u4f1a\u8ba1\u7b97\u51fa\u8fc7\u5927\u7684\u8865\u5145\u91cf&#xff0c;\u9650\u6d41\u5931\u6548\u3002<\/li>\n<li>\u6574\u4e2a\u811a\u672c\u539f\u5b50\u6267\u884c&#xff1a;Redis \u5355\u7ebf\u7a0b\u4fdd\u8bc1&#034;\u8bfb-\u7b97-\u5199&#034;\u4e0d\u88ab\u5e76\u53d1\u6253\u65ad\u3002<\/li>\n<\/ul>\n<p>\u8c03\u7528\u4ee3\u7801&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u6bcf\u4e2a\u7528\u6237&#xff1a;\u6876\u5bb9\u91cf 5&#xff08;\u5141\u8bb8\u7a81\u53d1 5 \u6b21&#xff09;&#xff0c;\u6bcf\u79d2\u8865\u5145 2 \u4e2a\u4ee4\u724c<\/span><br \/>\n<span class=\"token keyword\">boolean<\/span> allowed <span class=\"token operator\">&#061;<\/span> rateLimiterService<span class=\"token punctuation\">.<\/span><span class=\"token function\">tryAcquire<\/span><span class=\"token punctuation\">(<\/span><br \/>\n    <span class=\"token string\">&#034;rate:seckill:&#034;<\/span> <span class=\"token operator\">&#043;<\/span> userId<span class=\"token punctuation\">,<\/span>  <span class=\"token comment\">\/\/ \u9650\u6d41\u952e<\/span><br \/>\n    <span class=\"token number\">5<\/span><span class=\"token punctuation\">,<\/span>                          <span class=\"token comment\">\/\/ \u5bb9\u91cf<\/span><br \/>\n    <span class=\"token number\">2<\/span>                           <span class=\"token comment\">\/\/ \u901f\u7387<\/span><br \/>\n<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>allowed<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token class-name\">Result<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">fail<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u6d3b\u52a8\u592a\u706b\u7206&#xff0c;\u8bf7\u7a0d\u540e\u518d\u8bd5&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ HTTP 429<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u7b80\u5355\u8ba1\u6570\u9650\u6d41&#xff08;\u56fa\u5b9a\u7a97\u53e3&#xff0c;\u9002\u5408\u77ed\u4fe1\u7c7b\u573a\u666f&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u540c\u4e00\u624b\u673a\u53f7 60 \u79d2\u5185\u6700\u591a\u53d1 1 \u6b21\u77ed\u4fe1<\/span><br \/>\n<span class=\"token keyword\">public<\/span> <span class=\"token keyword\">boolean<\/span> <span class=\"token function\">simpleLimit<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> key<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> maxCount<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">long<\/span> windowSeconds<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token class-name\">Long<\/span> count <span class=\"token operator\">&#061;<\/span> redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">opsForValue<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">increment<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>count <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">&amp;&amp;<\/span> count <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        redisTemplate<span class=\"token punctuation\">.<\/span><span class=\"token function\">expire<\/span><span class=\"token punctuation\">(<\/span>key<span class=\"token punctuation\">,<\/span> windowSeconds<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">SECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> count <span class=\"token operator\">!&#061;<\/span> <span class=\"token keyword\">null<\/span> <span class=\"token operator\">&amp;&amp;<\/span> count <span class=\"token operator\">&lt;&#061;<\/span> maxCount<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u5206\u5e03\u5f0f\u9650\u6d41\u7684\u4e00\u81f4\u6027&#xff1a;\u9650\u6d41\u903b\u8f91\u5728 Redis \u4e2d&#xff08;\u800c\u975e\u5e94\u7528\u5185\u5b58&#xff09;&#xff0c;\u591a\u5b9e\u4f8b\u90e8\u7f72\u5929\u7136\u4e00\u81f4\u2014\u2014\u6240\u6709\u5b9e\u4f8b\u5171\u4eab\u540c\u4e00\u4e2a\u4ee4\u724c\u6876\u3002\u8fd9\u662f Redis \u9650\u6d41\u76f8\u6bd4\u672c\u5730\u9650\u6d41&#xff08;\u5982 Guava RateLimiter&#xff09;\u7684\u6838\u5fc3\u4f18\u52bf\u3002<\/p>\n<hr \/>\n<h3>\u573a\u666f\u5341\u4e00&#xff1a;\u5206\u5e03\u5f0f\u9501<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u4f60\u4eec\u54ea\u91cc\u7528\u5230\u4e86\u5206\u5e03\u5f0f\u9501&#xff1f;\u600e\u4e48\u5b9e\u73b0\u7684&#xff1f;\u6709\u4ec0\u4e48\u5751&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;3 \u5206\u949f&#xff08;\u8fd9\u9898\u8ffd\u95ee\u6700\u591a&#xff0c;\u4e3b\u52a8\u628a\u5751\u5168\u8bf4\u5b8c&#xff09;<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u8bf4\u7528\u5728\u54ea&#xff08;15 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u4e3b\u8981\u4e24\u4e2a\u5730\u65b9&#xff1a;\u7f13\u5b58\u51fb\u7a7f\u65f6\u7684\u91cd\u5efa\u9501&#xff0c;\u8fd8\u6709\u8ba2\u5355\u521b\u5efa\u65f6\u7684\u9632\u91cd\u9501\u3002\u6211\u6309\u2019\u57fa\u7840\u5b9e\u73b0 \u2192 \u4e09\u4e2a\u5751 \u2192 \u6211\u4eec\u6700\u7ec8\u600e\u4e48\u7528\u2019\u6765\u8bb2\u3002\u201d<\/p>\n<p>\u57fa\u7840\u5b9e\u73b0&#xff08;30 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u57fa\u7840\u7248\u5c31\u662f SET key value NX EX 10\u2014\u2014\u4e0d\u5b58\u5728\u624d\u8bbe\u7f6e&#xff0c;\u540c\u65f6\u5e26 10 \u79d2\u8fc7\u671f\u3002\u8fd9\u91cc\u6709\u4e2a\u65b0\u624b\u5e38\u72af\u7684\u9519&#xff1a;\u5206\u6210 SETNX \u548c EXPIRE \u4e24\u6761\u547d\u4ee4\u3002\u8fd9\u4e24\u6b65\u4e0d\u662f\u539f\u5b50\u7684&#xff0c;\u5982\u679c\u7ebf\u7a0b\u52a0\u9501\u6210\u529f\u540e\u3001\u8bbe\u7f6e\u8fc7\u671f\u524d\u5b95\u673a\u4e86&#xff0c;\u8fd9\u628a\u9501\u5c31\u6c38\u4e0d\u8fc7\u671f&#xff0c;\u540e\u9762\u7684\u7ebf\u7a0b\u5168\u5361\u6b7b\u3002\u6240\u4ee5\u5fc5\u987b\u7528 SET \u7684 NX &#043; EX \u53c2\u6570\u4e00\u6761\u547d\u4ee4\u641e\u5b9a\u3002\u201d<\/p>\n<p>\u4e09\u4e2a\u5751&#xff0c;\u9010\u4e2a\u8bb2&#xff08;1 \u5206\u534a&#xff09;&#xff1a;<\/p>\n<p>&#034;\u57fa\u7840\u7248\u6709\u4e09\u4e2a\u5751&#xff0c;\u6211\u4eec\u9010\u4e2a\u89e3\u51b3&#xff1a;<\/p>\n<p>\u5751\u4e00&#xff1a;\u8bef\u5220\u522b\u4eba\u7684\u9501\u3002 \u573a\u666f\u662f&#xff1a;\u7ebf\u7a0b A \u52a0\u9501&#xff0c;\u4e1a\u52a1\u6267\u884c\u4e86 12 \u79d2&#xff0c;\u8d85\u8fc7\u4e86 10 \u79d2\u7684\u9501\u8d85\u65f6&#xff0c;\u9501\u81ea\u52a8\u91ca\u653e&#xff1b;\u7ebf\u7a0b B \u62ff\u5230\u9501\u5f00\u59cb\u5e72\u6d3b&#xff1b;\u8fd9\u65f6 A \u5e72\u5b8c\u4e86&#xff0c;\u6267\u884c DEL\u2014\u2014\u628a B \u7684\u9501\u5220\u4e86&#xff01;\u89e3\u51b3\u65b9\u6848\u662f\u52a0\u9501\u65f6 value \u5b58\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6&#xff08;\u6bd4\u5982 UUID&#xff09;&#xff0c;\u89e3\u9501\u65f6\u5148\u6bd4\u5bf9 value \u662f\u4e0d\u662f\u81ea\u5df1\u7684&#xff0c;\u662f\u624d\u5220\u3002\u800c\u4e14\u2019\u6bd4\u5bf9 &#043; \u5220\u9664\u2019\u8fd9\u4e24\u6b65\u53c8\u5fc5\u987b\u7528 Lua \u539f\u5b50\u6267\u884c&#xff0c;\u4e0d\u7136\u6bd4\u5bf9\u901a\u8fc7\u540e\u9501\u6070\u597d\u8fc7\u671f&#xff0c;\u8fd8\u662f\u4f1a\u8bef\u5220\u3002<\/p>\n<p>\u5751\u4e8c&#xff1a;\u4e1a\u52a1\u6ca1\u6267\u884c\u5b8c\u9501\u5148\u8fc7\u671f\u4e86\u3002 \u8d85\u65f6\u8bbe\u77ed\u4e86\u4e1a\u52a1\u8dd1\u4e0d\u5b8c&#xff0c;\u8bbe\u957f\u4e86\u6301\u9501\u7ebf\u7a0b\u5b95\u673a\u540e\u5176\u4ed6\u4eba\u8981\u5e72\u7b49\u3002\u8fd9\u4e2a\u4e24\u96be\u9760\u770b\u95e8\u72d7\u89e3\u51b3\u2014\u2014\u6211\u4eec\u540e\u6765\u6362\u4e86 Redisson&#xff0c;\u5b83\u7684\u673a\u5236\u662f&#xff1a;\u52a0\u9501\u9ed8\u8ba4 30 \u79d2\u8fc7\u671f&#xff0c;\u540c\u65f6\u8d77\u4e00\u4e2a\u540e\u53f0\u4efb\u52a1\u6bcf 10 \u79d2\u68c0\u67e5\u4e00\u6b21&#xff0c;\u7ebf\u7a0b\u8fd8\u6301\u6709\u9501\u5c31\u81ea\u52a8\u7eed\u671f\u56de 30 \u79d2&#xff1b;\u7ebf\u7a0b\u5b95\u673a\u4e86\u770b\u95e8\u72d7\u4e5f\u505c\u4e86&#xff0c;\u9501 30 \u79d2\u540e\u81ea\u7136\u8fc7\u671f\u3002\u76f8\u5f53\u4e8e\u2019\u53ea\u8981\u4eba\u8fd8\u6d3b\u7740&#xff0c;\u9501\u5c31\u4e00\u76f4\u7eed&#xff1b;\u4eba\u6ca1\u4e86&#xff0c;\u9501\u81ea\u52a8\u91ca\u653e\u2019&#xff0c;\u628a\u8d85\u65f6\u65f6\u95f4\u8fd9\u4e2a\u53c2\u6570\u4ece\u8bbe\u8ba1\u91cc\u6d88\u6389\u4e86\u3002<\/p>\n<p>\u5751\u4e09&#xff1a;\u4e3b\u4ece\u5207\u6362\u4e22\u9501\u3002 \u7ebf\u7a0b A \u5728\u4e3b\u8282\u70b9\u52a0\u9501\u6210\u529f&#xff0c;\u9501\u8fd8\u6ca1\u540c\u6b65\u5230\u4ece\u8282\u70b9&#xff0c;\u4e3b\u8282\u70b9\u6302\u4e86&#xff0c;\u4ece\u8282\u70b9\u664b\u5347\u2014\u2014\u65b0\u4e3b\u4e0a\u6ca1\u6709\u8fd9\u628a\u9501&#xff0c;\u7ebf\u7a0b B \u4e5f\u52a0\u9501\u6210\u529f&#xff0c;\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6301\u9501\u3002\u5b98\u65b9\u6709 RedLock \u65b9\u6848&#xff08;\u5411\u591a\u4e2a\u72ec\u7acb\u8282\u70b9\u52a0\u9501&#xff0c;\u591a\u6570\u6210\u529f\u624d\u7b97\u6210\u529f&#xff09;&#xff0c;\u4f46\u5b83\u4f9d\u8d56\u65f6\u949f\u5047\u8bbe&#xff0c;\u4e1a\u754c\u6709\u4e89\u8bae\u3002\u6211\u4eec\u7684\u6001\u5ea6\u662f&#xff1a;\u4e0d\u6307\u671b\u9501\u4fdd\u8bc1\u6b63\u786e\u6027\u3002&#034;<\/p>\n<p>\u6700\u7ec8\u7684\u4f7f\u7528\u54f2\u5b66&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6700\u540e\u8fd9\u70b9\u662f\u6211\u4eec\u8e29\u5751\u4e4b\u540e\u5f62\u6210\u7684\u5171\u8bc6&#xff1a;\u5206\u5e03\u5f0f\u9501\u662f\u6027\u80fd\u4f18\u5316\u624b\u6bb5&#xff0c;\u4e0d\u662f\u6b63\u786e\u6027\u4fdd\u969c\u3002\u9501\u7684\u4f5c\u7528\u662f\u8ba9 99.9% \u7684\u5e76\u53d1\u51b2\u7a81\u5728 Redis \u5c42\u5c31\u89e3\u51b3\u6389&#xff0c;\u51cf\u5c11\u6570\u636e\u5e93\u538b\u529b&#xff1b;\u4f46\u6b63\u786e\u6027\u5fc5\u987b\u7531\u4e1a\u52a1\u5c42\u515c\u5e95\u2014\u2014\u5e93\u5b58\u6263\u51cf\u9760\u6570\u636e\u5e93\u7684 WHERE stock &gt;&#061; \u6570\u91cf \u4e50\u89c2\u9501&#xff0c;\u8ba2\u5355\u9632\u91cd\u9760\u5e42\u7b49\u4ee4\u724c\u52a0\u72b6\u6001\u673a\u3002\u8fd9\u6837\u5373\u4f7f\u9501\u56e0\u4e3a\u4e3b\u4ece\u5207\u6362\u5931\u6548\u4e86&#xff0c;\u6700\u574f\u7ed3\u679c\u4e5f\u53ea\u662f\u591a\u51e0\u4e2a\u8bf7\u6c42\u6253\u5230\u6570\u636e\u5e93\u88ab\u4e50\u89c2\u9501\u6321\u56de\u6765&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u8d44\u635f\u3002\u628a\u8eab\u5bb6\u6027\u547d\u62bc\u5728\u4e00\u628a\u53ef\u80fd\u5931\u6548\u7684\u9501\u4e0a&#xff0c;\u624d\u662f\u771f\u6b63\u5371\u9669\u7684\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u4f7f\u7528\u573a\u666f&#xff1a;<\/p>\n<li>\u5e93\u5b58\u91cd\u5efa\u9501&#xff08;\u573a\u666f\u516b&#xff09;&#xff1a;\u7f13\u5b58\u51fb\u7a7f\u65f6&#xff0c;\u9632\u6b62\u591a\u7ebf\u7a0b\u540c\u65f6\u67e5\u5e93\u91cd\u5efa\u3002<\/li>\n<li>\u8ba2\u5355\u9632\u91cd\u9501&#xff08;\u573a\u666f\u4e03&#xff09;&#xff1a;\u9632\u6b62\u540c\u4e00\u7528\u6237\u5e76\u53d1\u521b\u5efa\u91cd\u590d\u8ba2\u5355\u3002<\/li>\n<p>\u57fa\u7840\u52a0\u9501&#xff1a;<\/p>\n<p><span class=\"token comment\">\/\/ \u52a0\u9501&#xff1a;\u539f\u5b50\u64cd\u4f5c&#xff0c;10 \u79d2\u8d85\u65f6<\/span><br \/>\n<span class=\"token comment\">\/\/ \u5e95\u5c42\u662f SET key value NX EX 10&#xff0c;\u4e00\u6761\u547d\u4ee4\u5b8c\u6210<\/span><br \/>\n<span class=\"token keyword\">boolean<\/span> locked <span class=\"token operator\">&#061;<\/span> redisService<span class=\"token punctuation\">.<\/span><span class=\"token function\">setIfAbsent<\/span><span class=\"token punctuation\">(<\/span>lockKey<span class=\"token punctuation\">,<\/span> requestId<span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">TimeUnit<\/span><span class=\"token punctuation\">.<\/span><span class=\"token constant\">SECONDS<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4e3a\u4ec0\u4e48\u5fc5\u987b\u4e00\u6761\u547d\u4ee4&#xff1a;\u4e24\u6b65\u64cd\u4f5c&#xff08;\u5148 SETNX \u518d EXPIRE&#xff09;\u4e0d\u662f\u539f\u5b50\u7684\u3002\u5982\u679c\u7ebf\u7a0b A \u6267\u884c\u5b8c SETNX \u540e\u5b95\u673a&#xff0c;EXPIRE \u6ca1\u6267\u884c&#xff0c;\u9501\u5c31\u6c38\u4e0d\u8fc7\u671f\u2014\u2014\u6b7b\u9501\u3002<\/p>\n<p>\u89e3\u9501 Lua \u811a\u672c&#xff08;\u6821\u9a8c\u6301\u6709\u8005&#xff09;&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u53ea\u6709\u6301\u6709\u8005\u624d\u80fd\u5220\u9664\u81ea\u5df1\u7684\u9501<\/span><br \/>\n<span class=\"token keyword\">if<\/span> redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;GET&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> ARGV<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token keyword\">then<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> redis<span class=\"token punctuation\">.<\/span><span class=\"token function\">call<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;DEL&#039;<\/span><span class=\"token punctuation\">,<\/span> KEYS<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">else<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><br \/>\n<span class=\"token keyword\">end<\/span><\/p>\n<p>\u8bef\u5220\u573a\u666f\u8fd8\u539f&#xff1a;<\/p>\n<li>\u7ebf\u7a0b A \u52a0\u9501&#xff0c;\u8d85\u65f6 10 \u79d2&#xff1b;<\/li>\n<li>\u7ebf\u7a0b A \u4e1a\u52a1\u6267\u884c\u4e86 12 \u79d2&#xff08;\u8d85\u65f6&#xff09;&#xff0c;\u9501\u81ea\u52a8\u91ca\u653e&#xff1b;<\/li>\n<li>\u7ebf\u7a0b B \u52a0\u9501\u6210\u529f&#xff1b;<\/li>\n<li>\u7ebf\u7a0b A \u6267\u884c\u5b8c\u6bd5&#xff0c;\u8c03\u7528 DEL\u2014\u2014\u5220\u6389\u4e86\u7ebf\u7a0b B \u7684\u9501&#xff01;<\/li>\n<p>\u6240\u4ee5\u89e3\u9501\u5fc5\u987b\u6bd4\u5bf9 value&#xff08;requestId&#xff09;&#xff0c;\u4e14&#034;\u6bd4\u5bf9 &#043; \u5220\u9664&#034;\u7528 Lua \u539f\u5b50\u6267\u884c\u3002<\/p>\n<p>Redisson \u770b\u95e8\u72d7\u539f\u7406&#xff1a;<\/p>\n<p>1. \u52a0\u9501\u65f6\u5982\u679c\u4e0d\u6307\u5b9a leaseTime&#xff0c;Redisson \u9ed8\u8ba4 30 \u79d2\u8fc7\u671f<br \/>\n2. \u540c\u65f6\u542f\u52a8\u4e00\u4e2a\u540e\u53f0\u5b9a\u65f6\u4efb\u52a1&#xff08;\u770b\u95e8\u72d7&#xff09;<br \/>\n3. \u770b\u95e8\u72d7\u6bcf\u9694 10 \u79d2&#xff08;30\/3&#xff09;\u68c0\u67e5\u4e00\u6b21&#xff1a;<br \/>\n   &#8211; \u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u8fd8\u6301\u6709\u9501 \u2192 \u7eed\u671f\u5230 30 \u79d2<br \/>\n   &#8211; \u5982\u679c\u7ebf\u7a0b\u5df2\u91ca\u653e\u9501\u6216\u5b95\u673a \u2192 \u505c\u6b62\u7eed\u671f<br \/>\n4. \u4e1a\u52a1\u6267\u884c\u5b8c\u6bd5\u8c03\u7528 unlock()&#xff0c;\u9501\u7acb\u5373\u91ca\u653e<br \/>\n5. \u5982\u679c\u7ebf\u7a0b\u5b95\u673a&#xff0c;\u770b\u95e8\u72d7\u968f\u4e4b\u505c\u6b62&#xff0c;\u9501\u5728 30 \u79d2\u540e\u81ea\u7136\u8fc7\u671f<\/p>\n<p>     Redis<\/p>\n<p>     \u770b\u95e8\u72d7\u7ebf\u7a0b<\/p>\n<p>     Redisson<\/p>\n<p>     \u4e1a\u52a1\u7ebf\u7a0b<\/p>\n<p>      Redis<\/p>\n<p>      \u770b\u95e8\u72d7\u7ebf\u7a0b<\/p>\n<p>      Redisson<\/p>\n<p>      \u4e1a\u52a1\u7ebf\u7a0b<\/p>\n<p>  #mermaid-svg-nMUrFe7n5vzqy9Fp{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-nMUrFe7n5vzqy9Fp .error-icon{fill:#552222;}#mermaid-svg-nMUrFe7n5vzqy9Fp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-nMUrFe7n5vzqy9Fp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-nMUrFe7n5vzqy9Fp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-nMUrFe7n5vzqy9Fp .marker.cross{stroke:#333333;}#mermaid-svg-nMUrFe7n5vzqy9Fp svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-nMUrFe7n5vzqy9Fp p{margin:0;}#mermaid-svg-nMUrFe7n5vzqy9Fp .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nMUrFe7n5vzqy9Fp text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-nMUrFe7n5vzqy9Fp .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-nMUrFe7n5vzqy9Fp .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-nMUrFe7n5vzqy9Fp #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-nMUrFe7n5vzqy9Fp .sequenceNumber{fill:white;}#mermaid-svg-nMUrFe7n5vzqy9Fp #sequencenumber{fill:#333;}#mermaid-svg-nMUrFe7n5vzqy9Fp #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-nMUrFe7n5vzqy9Fp .messageText{fill:#333;stroke:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nMUrFe7n5vzqy9Fp .labelText,#mermaid-svg-nMUrFe7n5vzqy9Fp .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .loopText,#mermaid-svg-nMUrFe7n5vzqy9Fp .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-nMUrFe7n5vzqy9Fp .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-nMUrFe7n5vzqy9Fp .noteText,#mermaid-svg-nMUrFe7n5vzqy9Fp .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-nMUrFe7n5vzqy9Fp .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nMUrFe7n5vzqy9Fp .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nMUrFe7n5vzqy9Fp .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-nMUrFe7n5vzqy9Fp .actorPopupMenu{position:absolute;}#mermaid-svg-nMUrFe7n5vzqy9Fp .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-nMUrFe7n5vzqy9Fp .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-nMUrFe7n5vzqy9Fp .actor-man circle,#mermaid-svg-nMUrFe7n5vzqy9Fp line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-nMUrFe7n5vzqy9Fp :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>    alt<\/p>\n<p>     [\u4ecd\u6301\u6709]<\/p>\n<p>    [\u5df2\u91ca\u653e]<\/p>\n<p>    loop<\/p>\n<p>     [\u6bcf 10 \u79d2]<\/p>\n<p>   lock()<\/p>\n<p>   SET lock NX EX 30<\/p>\n<p>   \u542f\u52a8\u770b\u95e8\u72d7(\u6bcf10\u79d2\u6267\u884c)<\/p>\n<p>   \u68c0\u67e5\u9501\u662f\u5426\u8fd8\u88ab\u6301\u6709<\/p>\n<p>   PEXPIRE lock 30000(\u7eed\u671f)<\/p>\n<p>   \u505c\u6b62<\/p>\n<p>   unlock()<\/p>\n<p>   DEL lock<\/p>\n<p>   \u505c\u6b62\u770b\u95e8\u72d7<\/p>\n<p>\u770b\u95e8\u72d7\u7684\u7cbe\u5999\u4e4b\u5904&#xff1a;\u89e3\u51b3\u4e86&#034;\u8d85\u65f6\u65f6\u95f4\u8bbe\u591a\u957f&#034;\u7684\u4e24\u96be\u2014\u2014\u4e0d\u9700\u8981\u9884\u8bbe\u8d85\u65f6&#xff0c;\u53ea\u8981\u7ebf\u7a0b\u6d3b\u7740\u5c31\u81ea\u52a8\u7eed\u671f&#xff0c;\u7ebf\u7a0b\u6b7b\u4e86\u5c31\u81ea\u52a8\u8fc7\u671f\u3002<\/p>\n<p>\u5b9e\u9645\u9009\u62e9\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\u7b80\u5355\u573a\u666f&#xff08;\u5982\u7f13\u5b58\u91cd\u5efa\u9501&#xff09;&#xff1a;\u7528\u57fa\u7840 SETNX &#043; EX&#xff0c;\u4e1a\u52a1\u8017\u65f6\u53ef\u9884\u4f30&#xff08;\u67e5\u5e93 &lt; 1 \u79d2&#xff09;&#xff0c;\u8bbe 10 \u79d2\u8d85\u65f6\u8db3\u591f\u3002<\/li>\n<li>\u590d\u6742\u573a\u666f&#xff08;\u5982\u8ba2\u5355\u521b\u5efa&#xff0c;\u591a\u6b65\u64cd\u4f5c&#xff09;&#xff1a;\u7528 Redisson&#xff0c;\u770b\u95e8\u72d7\u81ea\u52a8\u7eed\u671f\u3002<\/li>\n<\/ul>\n<p>\u4e3b\u4ece\u5207\u6362\u4e22\u9501\u95ee\u9898&#xff1a;<\/p>\n<p>1. \u7ebf\u7a0b A \u5728\u4e3b\u8282\u70b9\u52a0\u9501\u6210\u529f<br \/>\n2. \u4e3b\u8282\u70b9\u5b95\u673a&#xff0c;\u9501\u6570\u636e\u8fd8\u6ca1\u540c\u6b65\u5230\u4ece\u8282\u70b9<br \/>\n3. \u4ece\u8282\u70b9\u664b\u5347\u4e3a\u4e3b<br \/>\n4. \u7ebf\u7a0b B \u5728\u65b0\u4e3b\u8282\u70b9\u52a0\u9501\u6210\u529f  \u2190 \u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6301\u9501&#xff01;<\/p>\n<ul>\n<li>RedLock&#xff1a;\u5411\u591a\u4e2a\u72ec\u7acb Redis \u8282\u70b9\u52a0\u9501&#xff0c;\u591a\u6570\u6210\u529f\u624d\u7b97\u6210\u529f\u3002\u4f46\u6709\u4e89\u8bae&#xff08;Martin Kleppmann \u6307\u51fa\u5176\u4f9d\u8d56\u65f6\u949f\u5047\u8bbe&#xff09;\u3002<\/li>\n<li>\u5b9e\u9645\u505a\u6cd5&#xff1a;\u8d44\u635f\u654f\u611f\u573a\u666f\u4e0d\u4f9d\u8d56\u5206\u5e03\u5f0f\u9501\u505a\u6700\u7ec8\u4fdd\u969c&#xff0c;\u7528\u6570\u636e\u5e93\u4e50\u89c2\u9501&#xff08;WHERE stock &gt;&#061; qty&#xff09;\u515c\u5e95\u3002\u9501\u53ea\u662f\u51cf\u5c11\u5e76\u53d1\u51b2\u7a81\u7684\u4f18\u5316\u624b\u6bb5\u3002<\/li>\n<\/ul>\n<h4>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u7b54<\/h4>\n<ul>\n<li>\u4e09\u4e2a\u5751\u6309&#034;\u573a\u666f \u2192 \u540e\u679c \u2192 \u89e3\u6cd5&#034;\u8bb2&#xff0c;\u6bcf\u4e2a\u90fd\u6709\u753b\u9762\u611f&#xff1b;<\/li>\n<li>\u6700\u540e&#034;\u9501\u662f\u4f18\u5316\u4e0d\u662f\u4fdd\u969c&#034;\u7684\u89c2\u70b9&#xff0c;\u662f\u8fd9\u9053\u9898\u7684\u6700\u9ad8\u5206\u7b54\u6848\u2014\u2014\u5b83\u5c55\u793a\u7684\u662f\u5206\u5e03\u5f0f\u7cfb\u7edf\u601d\u7ef4&#xff0c;\u800c\u4e0d\u53ea\u662f API \u719f\u7ec3\u5ea6\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u573a\u666f\u5341\u4e8c&#xff1a;\u5e93\u5b58\u4e3a\u4ec0\u4e48\u7528 String \u4e0d\u7528 Hash&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u5e93\u5b58\u5b58\u5728 Redis \u91cc\u7528\u7684\u4ec0\u4e48\u7ed3\u6784&#xff1f;\u4e3a\u4ec0\u4e48\u4e0d\u7528 Hash \u628a\u6240\u6709 SKU \u653e\u4e00\u4e2a key \u91cc&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;1~2 \u5206\u949f&#xff08;\u8fd9\u9898\u4e0d\u96be&#xff0c;\u7b54\u51fa\u5bf9\u6bd4\u5373\u53ef&#xff09;<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>&#034;\u6211\u4eec\u7528\u7684\u662f String&#xff0c;\u4e00\u4e2a SKU \u4e00\u4e2a key&#xff0c;\u6bd4\u5982 seckill:stock:{skuId}\u3002Hash \u65b9\u6848\u2014\u2014\u6240\u6709 SKU \u585e\u8fdb\u4e00\u4e2a\u5927 Hash \u7684 field \u91cc\u2014\u2014\u770b\u8d77\u6765\u66f4\u2019\u6574\u9f50\u2019&#xff0c;\u4f46\u6709\u4e09\u4e2a\u786c\u4f24&#xff1a;<\/p>\n<p>\u7b2c\u4e00&#xff0c;\u8fc7\u671f\u7c92\u5ea6\u3002\u79d2\u6740\u6d3b\u52a8\u7ed3\u675f\u540e\u8981\u6e05\u6389\u5bf9\u5e94\u5e93\u5b58 key&#xff0c;String \u65b9\u6848\u6bcf\u4e2a SKU \u72ec\u7acb TTL\u3001\u72ec\u7acb\u5220\u9664&#xff1b;Hash \u662f\u6574\u4e2a key \u4e00\u4e2a TTL&#xff0c;\u6ca1\u6cd5\u8ba9\u5355\u4e2a field \u5355\u72ec\u8fc7\u671f&#xff0c;\u53ea\u80fd\u624b\u52a8 HDEL&#xff0c;\u9ebb\u70e6\u4e14\u5bb9\u6613\u6f0f\u3002<\/p>\n<p>\u7b2c\u4e8c&#xff0c;\u5927 key \u98ce\u9669\u300210 \u4e07\u4e2a SKU \u53c2\u4e0e\u5927\u4fc3&#xff0c;Hash \u65b9\u6848\u5c31\u662f 10 \u4e07\u4e2a field \u6324\u5728\u4e00\u4e2a key \u91cc&#xff0c;\u8bfb\u5199\u8fd9\u4e2a key \u4f1a\u963b\u585e Redis \u5355\u7ebf\u7a0b&#xff0c;\u5f71\u54cd\u7684\u662f\u6574\u4e2a\u5b9e\u4f8b\u4e0a\u6240\u6709\u4e1a\u52a1\u3002String \u65b9\u6848\u6bcf\u4e2a key \u5c31\u5b58\u4e00\u4e2a\u6570\u5b57&#xff0c;\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6210\u4e3a\u5927 key\u3002<\/p>\n<p>\u7b2c\u4e09&#xff0c;\u96c6\u7fa4\u5206\u7247\u3002Cluster \u6a21\u5f0f\u4e0b\u4e0d\u540c\u7684 String key \u4f1a\u6563\u843d\u5230\u4e0d\u540c\u69fd\u4f4d\u3001\u4e0d\u540c\u8282\u70b9&#xff0c;\u538b\u529b\u5929\u7136\u5206\u6563&#xff1b;Hash \u65b9\u6848\u6240\u6709 SKU \u5728\u540c\u4e00\u4e2a key&#xff0c;\u53ea\u80fd\u843d\u5728\u4e00\u4e2a\u8282\u70b9\u4e0a&#xff0c;\u79d2\u6740\u7684\u6d2a\u5cf0\u5168\u7838\u8fd9\u4e00\u4e2a\u8282\u70b9&#xff0c;\u7b49\u4e8e\u767d\u642d\u4e86\u96c6\u7fa4\u3002<\/p>\n<p>\u53cd\u8fc7\u6765\u8bf4&#xff0c;Hash \u9002\u5408\u4ec0\u4e48&#xff1f;\u9002\u5408\u2019\u4e00\u4e2a\u5b9e\u4f53\u7684\u591a\u4e2a\u5c5e\u6027\u2019\u4e14\u9700\u8981\u90e8\u5206\u66f4\u65b0\u7684\u573a\u666f&#xff0c;\u6bd4\u5982\u7528\u6237\u4fe1\u606f\u2014\u2014\u6539\u6635\u79f0\u4e0d\u52a8\u5e74\u9f84&#xff0c;HSET \u5355\u4e2a field \u5c31\u884c\u3002\u5e93\u5b58\u5c31\u4e00\u4e2a\u6570\u503c&#xff0c;\u6ca1\u6709\u2019\u90e8\u5206\u66f4\u65b0\u2019\u7684\u9700\u6c42&#xff0c;\u7528 Hash \u662f\u6740\u9e21\u7528\u725b\u5200\u8fd8\u628a\u81ea\u5df1\u624b\u5272\u4e86\u3002<\/p>\n<p>\u4e00\u53e5\u8bdd\u603b\u7ed3\u6211\u4eec\u7684\u9009\u578b\u539f\u5219&#xff1a;\u6570\u636e\u5f62\u6001\u51b3\u5b9a\u7ed3\u6784\u2014\u2014\u5355\u503c String\u3001\u5bf9\u8c61 Hash\u3001\u961f\u5217 List\u3001\u53bb\u91cd Set\u3001\u6392\u884c ZSet\u3002&#034;<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>String vs Hash \u5168\u7ef4\u5ea6\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u7ef4\u5ea6String&#xff08;stock:{skuId} &#061; 100&#xff09;Hash&#xff08;stock \u7684 field&#061;{skuId}, value&#061;100&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u539f\u5b50\u6263\u51cf<\/td>\n<td>DECRBY stock:{skuId} 1 \u2705<\/td>\n<td>HINCRBY stock {skuId} -1 \u2705<\/td>\n<\/tr>\n<tr>\n<td>\u5355 SKU \u64cd\u4f5c<\/td>\n<td>\u76f4\u63a5\u5b9a\u4f4d&#xff0c;O(1)<\/td>\n<td>\u9700\u6307\u5b9a key&#043;field&#xff0c;O(1)<\/td>\n<\/tr>\n<tr>\n<td>\u6279\u91cf\u64cd\u4f5c<\/td>\n<td>\u9700 MGET \u591a\u4e2a key<\/td>\n<td>HGETALL \u4e00\u6b21\u53d6\u5168\u90e8<\/td>\n<\/tr>\n<tr>\n<td>\u8fc7\u671f\u63a7\u5236<\/td>\n<td>\u6bcf\u4e2a key \u72ec\u7acb TTL \u2705<\/td>\n<td>\u6574\u4e2a Hash \u4e00\u4e2a TTL \u274c<\/td>\n<\/tr>\n<tr>\n<td>\u5927 key \u98ce\u9669<\/td>\n<td>\u65e0&#xff08;\u6bcf\u4e2a key \u5f88\u5c0f&#xff09;<\/td>\n<td>\u6240\u6709 SKU \u6324\u4e00\u4e2a key&#xff0c;\u53ef\u80fd\u6210\u5927 key \u274c<\/td>\n<\/tr>\n<tr>\n<td>\u96c6\u7fa4\u5206\u7247<\/td>\n<td>\u4e0d\u540c SKU \u5206\u6563\u5230\u4e0d\u540c\u69fd\u4f4d \u2705<\/td>\n<td>\u6240\u6709 SKU \u540c\u4e00\u69fd\u4f4d \u274c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6570\u636e\u7ed3\u6784\u9009\u578b\u603b\u539f\u5219&#xff1a;<\/p>\n<table>\n<tr>\u6570\u636e\u5f62\u6001\u9009\u62e9\u4f8b\u5b50<\/tr>\n<tbody>\n<tr>\n<td>\u5355\u503c<\/td>\n<td>String<\/td>\n<td>\u5e93\u5b58\u3001\u8ba1\u6570\u5668\u3001Token<\/td>\n<\/tr>\n<tr>\n<td>\u5bf9\u8c61\u5c5e\u6027\u96c6<\/td>\n<td>Hash<\/td>\n<td>\u7528\u6237\u4fe1\u606f\u3001\u5546\u54c1\u5c5e\u6027<\/td>\n<\/tr>\n<tr>\n<td>\u6709\u5e8f\u961f\u5217<\/td>\n<td>List<\/td>\n<td>\u6d88\u606f\u961f\u5217\u3001\u6700\u65b0\u52a8\u6001<\/td>\n<\/tr>\n<tr>\n<td>\u53bb\u91cd\u96c6\u5408<\/td>\n<td>Set<\/td>\n<td>\u6807\u7b7e\u3001\u62bd\u5956\u6c60<\/td>\n<\/tr>\n<tr>\n<td>\u5e26\u5206\u6570\u6392\u5e8f<\/td>\n<td>ZSet<\/td>\n<td>\u6392\u884c\u699c\u3001\u5ef6\u8fdf\u961f\u5217<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u573a\u666f\u5341\u4e09&#xff1a;Redis \u6302\u4e86\u600e\u4e48\u529e&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201cRedis \u5b95\u673a\u4e86&#xff0c;\u4f60\u4eec\u7cfb\u7edf\u4f1a\u600e\u6837&#xff1f;\u6709\u4ec0\u4e48\u964d\u7ea7\u65b9\u6848&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u5206\u4e1a\u52a1\u8bb2\u5f71\u54cd&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;Redis \u5728\u6211\u4eec\u7cfb\u7edf\u91cc\u627f\u62c5\u4e86\u597d\u51e0\u4e2a\u89d2\u8272&#xff0c;\u5b95\u673a\u7684\u5f71\u54cd\u8981\u5206\u4e1a\u52a1\u8bf4&#xff0c;\u964d\u7ea7\u7b56\u7565\u4e5f\u4e0d\u4e00\u6837&#xff1a;<\/p>\n<p>\u79d2\u6740\u5e93\u5b58\u662f\u5f3a\u4f9d\u8d56&#xff0c;\u6ca1\u6709 Redis \u5c31\u6ca1\u6cd5\u539f\u5b50\u6263\u51cf\u3002\u7b56\u7565\u662f\u5feb\u901f\u5931\u8d25&#xff1a;\u7194\u65ad\u5668\u68c0\u6d4b\u5230 Redis \u4e0d\u53ef\u7528&#xff0c;\u79d2\u6740\u63a5\u53e3\u76f4\u63a5\u8fd4\u56de\u2019\u6d3b\u52a8\u6682\u505c\u2019&#xff0c;\u7edd\u5bf9\u4e0d\u7a7f\u900f\u5230\u6570\u636e\u5e93\u2014\u2014\u6570\u636e\u5e93\u90a3\u4efd\u5e93\u5b58\u6ca1\u6709\u9ad8\u5e76\u53d1\u4fdd\u62a4&#xff0c;\u653e\u6d41\u91cf\u8fdb\u53bb\u5c31\u662f\u8d85\u5356\u3002\u5b81\u53ef\u6d3b\u52a8\u505c\u51e0\u5206\u949f&#xff0c;\u4e0d\u80fd\u51fa\u8d44\u635f\u3002<\/p>\n<p>\u5546\u54c1\u7f13\u5b58\u662f\u5f31\u4f9d\u8d56&#xff0c;\u964d\u7ea7\u8def\u5f84\u662f\u672c\u5730\u7f13\u5b58\u5148\u9876\u4f4f&#xff08;Caffeine \u91cc\u8fd8\u6709\u51e0\u79d2\u7684\u6570\u636e&#xff09;&#xff0c;\u7136\u540e\u9650\u6d41\u653e\u884c\u4e00\u90e8\u5206\u8bf7\u6c42\u76f4\u67e5\u6570\u636e\u5e93&#xff0c;\u8d85\u8fc7\u9608\u503c\u7684\u8fd4\u56de\u515c\u5e95\u9875\u3002\u6838\u5fc3\u662f\u9650\u6d41\u4fdd\u62a4\u6570\u636e\u5e93&#xff0c;\u522b\u8ba9\u7f13\u5b58\u5931\u6548\u6f14\u53d8\u6210\u6570\u636e\u5e93\u96ea\u5d29\u3002<\/p>\n<p>\u767b\u5f55\u6001\u6bd4\u8f83\u7279\u6b8a&#xff1a;JWT \u672c\u8eab\u53ef\u4ee5\u672c\u5730\u9a8c\u7b7e&#xff0c;Redis \u53ea\u7528\u4e8e\u2019\u767b\u51fa\u540a\u9500\u2019\u68c0\u67e5\u3002\u964d\u7ea7\u65f6\u6211\u4eec\u9009\u62e9\u8df3\u8fc7\u540a\u9500\u68c0\u67e5\u653e\u884c\u2014\u2014\u727a\u7272\u4e00\u70b9\u70b9\u2019\u5df2\u767b\u51fa token \u77ed\u6682\u53ef\u7528\u2019\u7684\u5b89\u5168\u6027&#xff0c;\u6362\u5168\u7ad9\u53ef\u767b\u5f55&#xff0c;\u4e8b\u540e\u9760\u5ba1\u8ba1\u65e5\u5fd7\u8865\u67e5\u3002\u8fd9\u662f\u4e2a\u5b89\u5168\u548c\u53ef\u7528\u6027\u7684\u6743\u8861&#xff0c;\u6211\u4eec\u5185\u90e8\u8bc4\u5ba1\u8fc7\u3002<\/p>\n<p>\u5206\u5e03\u5f0f\u9501\u5931\u6548\u5c31\u9000\u5316\u6210\u6570\u636e\u5e93\u4e50\u89c2\u9501\u8def\u5f84&#xff0c;\u6027\u80fd\u5dee\u4e00\u70b9\u4f46\u6b63\u786e\u6027\u8fd8\u5728\u2014\u2014\u8fd9\u5c31\u662f\u521a\u624d\u8bf4\u7684\u2019\u9501\u662f\u4f18\u5316\u4e0d\u662f\u4fdd\u969c\u2019\u7684\u4ef7\u503c\u6240\u5728\u3002&#034;<\/p>\n<p>\u518d\u8bb2\u9884\u9632&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u4e0d\u8fc7\u964d\u7ea7\u662f\u4e8b\u540e\u8865\u6551&#xff0c;\u91cd\u70b9\u5176\u5b9e\u5728\u9884\u9632&#xff1a;\u90e8\u7f72\u4e0a\u6211\u4eec\u662f\u54e8\u5175\u6a21\u5f0f&#xff0c;\u4e3b\u8282\u70b9\u6302\u4e86 15 \u5230 30 \u79d2\u81ea\u52a8\u5207\u6362&#xff0c;\u5927\u90e8\u5206\u6545\u969c\u7528\u6237\u6839\u672c\u65e0\u611f&#xff1b;\u5ba2\u6237\u7aef\u6240\u6709 Redis \u64cd\u4f5c\u90fd\u8bbe\u4e86\u8d85\u65f6\u2014\u2014\u547d\u4ee4 2 \u79d2\u3001\u83b7\u53d6\u8fde\u63a5 1 \u79d2&#xff0c;\u7edd\u5bf9\u4e0d\u5141\u8bb8\u65e0\u9650\u7b49\u5f85&#xff0c;\u4e0d\u7136 Redis \u4e00\u6296\u52a8&#xff0c;\u4e1a\u52a1\u7ebf\u7a0b\u5168\u963b\u585e\u5728\u7b49\u8fde\u63a5\u4e0a&#xff0c;\u6545\u969c\u5c31\u4ece Redis \u8513\u5ef6\u5230\u6574\u4e2a\u5e94\u7528&#xff1b;\u518d\u52a0\u4e0a\u7194\u65ad\u5668&#xff0c;\u8fde\u7eed\u5931\u8d25 5 \u6b21\u5c31\u7194\u65ad 10 \u79d2&#xff0c;\u7ed9 Redis \u5598\u606f\u65f6\u95f4&#xff0c;\u4e5f\u907f\u514d\u96ea\u5d29\u5f0f\u7684\u91cd\u8bd5\u98ce\u66b4\u3002\u201d<\/p>\n<p>\u6062\u590d&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u6062\u590d\u4e4b\u540e&#xff0c;\u5e93\u5b58\u6570\u636e\u4ece\u6570\u636e\u5e93\u5bf9\u8d26\u540e\u91cd\u65b0\u9884\u70ed&#xff0c;\u5546\u54c1\u7f13\u5b58\u9760\u8bfb\u8bf7\u6c42\u81ea\u52a8\u91cd\u5efa\u4e0d\u7528\u7ba1&#xff0c;\u9650\u6d41\u4ee4\u724c\u6876\u81ea\u52a8\u4ece\u6ee1\u6876\u5f00\u59cb\u3002\u6574\u4f53\u539f\u5219\u662f&#xff1a;\u6838\u5fc3\u6570\u636e\u4ee5\u6570\u636e\u5e93\u4e3a\u51c6\u91cd\u5efa&#xff0c;\u7f13\u5b58\u7c7b\u6570\u636e\u8ba9\u5b83\u81ea\u7136\u56de\u6e29\u3002\u201d<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>Redis \u627f\u62c5\u7684\u89d2\u8272\u4e0e\u5b95\u673a\u5f71\u54cd&#xff08;\u6309\u4e25\u91cd\u7a0b\u5ea6\u6392\u5e8f&#xff09;&#xff1a;<\/p>\n<p>\u79d2\u6740\u65e0\u6cd5\u8fdb\u884c &gt; \u4e0b\u5355\u53ef\u80fd\u91cd\u590d &gt; \u7f13\u5b58\u7a7f\u900f\u5230 DB &gt; \u9650\u6d41\u5931\u6548<\/p>\n<p>\u5206\u573a\u666f\u964d\u7ea7\u77e9\u9635&#xff1a;<\/p>\n<table>\n<tr>\u4e1a\u52a1\u4f9d\u8d56\u5f3a\u5ea6\u964d\u7ea7\u7b56\u7565<\/tr>\n<tbody>\n<tr>\n<td>\u79d2\u6740\/\u5e93\u5b58<\/td>\n<td>\u5f3a\u4f9d\u8d56<\/td>\n<td>\u6d3b\u52a8\u7194\u65ad\u4e0b\u7ebf&#xff08;\u7edd\u4e0d\u7a7f\u900f DB&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u767b\u5f55\u6001<\/td>\n<td>\u4e2d\u4f9d\u8d56<\/td>\n<td>Token \u672c\u5730\u9a8c\u7b7e\u4ecd\u53ef\u7528&#xff0c;\u540a\u9500\u68c0\u67e5\u964d\u7ea7\u8df3\u8fc7 &#043; \u4e8b\u540e\u5ba1\u8ba1<\/td>\n<\/tr>\n<tr>\n<td>\u5546\u54c1\u7f13\u5b58<\/td>\n<td>\u5f31\u4f9d\u8d56<\/td>\n<td>\u672c\u5730\u7f13\u5b58\u515c\u5e95 &#043; DB \u9650\u6d41\u76f4\u67e5<\/td>\n<\/tr>\n<tr>\n<td>\u6392\u884c\u699c\/\u8ba1\u6570<\/td>\n<td>\u5f31\u4f9d\u8d56<\/td>\n<td>\u8fd4\u56de\u9759\u6001\u515c\u5e95\u6570\u636e&#xff08;\u5982\u6628\u65e5\u5feb\u7167&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5206\u5e03\u5f0f\u9501<\/td>\n<td>\u4e2d\u4f9d\u8d56<\/td>\n<td>\u9000\u5316\u4e3a\u6570\u636e\u5e93\u4e50\u89c2\u9501\u8def\u5f84<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e3a\u4ec0\u4e48\u79d2\u6740\u4e0d\u80fd\u964d\u7ea7\u5230\u6570\u636e\u5e93&#xff1a;\u6570\u636e\u5e93\u7684 UPDATE &#8230; WHERE stock &gt; 0 \u867d\u7136\u80fd\u9632\u8d85\u5356&#xff0c;\u4f46\u5728 10 \u4e07\u5e76\u53d1\u4e0b\u884c\u9501\u7ade\u4e89\u4f1a\u5bfc\u81f4\u6570\u636e\u5e93\u5d29\u6e83&#xff0c;\u8fdb\u800c\u62d6\u57ae\u6574\u4e2a\u7cfb\u7edf&#xff08;\u5305\u62ec\u6b63\u5e38\u8ba2\u5355&#xff09;\u3002\u5b81\u53ef\u79d2\u6740\u4e0d\u53ef\u7528&#xff0c;\u4e5f\u4e0d\u80fd\u62d6\u57ae\u5168\u5c40\u3002<\/p>\n<p>\u9884\u9632\u63aa\u65bd\u56db\u4ef6\u5957&#xff1a;<\/p>\n<li>Redis \u9ad8\u53ef\u7528\u90e8\u7f72&#xff1a;\u54e8\u5175\u6a21\u5f0f&#xff08;\u81ea\u52a8\u6545\u969c\u8f6c\u79fb&#xff09;\u6216 Cluster \u6a21\u5f0f\u3002\u5355\u8282\u70b9\u5b95\u673a\u65f6&#xff0c;\u54e8\u5175\u5728 10~30 \u79d2\u5185\u5b8c\u6210\u4e3b\u4ece\u5207\u6362&#xff0c;\u4e1a\u52a1\u51e0\u4e4e\u65e0\u611f\u3002<\/li>\n<li>\u8fde\u63a5\u6c60\u8d85\u65f6\u914d\u7f6e&#xff1a;\u8fde\u63a5\u8d85\u65f6 3 \u79d2&#xff0c;\u547d\u4ee4\u8d85\u65f6 2 \u79d2&#xff0c;\u83b7\u53d6\u8fde\u63a5\u7b49\u5f85 1 \u79d2\u2014\u2014\u907f\u514d Redis \u6162\u54cd\u5e94\u65f6\u7ebf\u7a0b\u5806\u79ef\u3002<\/li>\n<li>\u7194\u65ad\u5668&#xff1a;Sentinel\/Resilience4j \u5bf9 Redis \u64cd\u4f5c\u505a\u7194\u65ad\u3002\u8fde\u7eed\u5931\u8d25 5 \u6b21\u540e\u7194\u65ad 10 \u79d2&#xff0c;\u671f\u95f4\u76f4\u63a5\u8d70\u964d\u7ea7\u903b\u8f91\u3002<\/li>\n<li>\u76d1\u63a7\u544a\u8b66&#xff1a;\u5185\u5b58\u3001\u8fde\u63a5\u6570\u3001\u54cd\u5e94\u65f6\u95f4\u3001\u547d\u4e2d\u7387\u3002\u547d\u4e2d\u7387\u4f4e\u4e8e 90% \u6216\u54cd\u5e94\u8d85 10ms \u5c31\u544a\u8b66\u3002<\/li>\n<p>\u6570\u636e\u6062\u590d\u7b56\u7565&#xff1a;<\/p>\n<table>\n<tr>\u6570\u636e\u7c7b\u578b\u6062\u590d\u65b9\u5f0f<\/tr>\n<tbody>\n<tr>\n<td>\u5e93\u5b58\u6570\u636e<\/td>\n<td>\u4ece\u6570\u636e\u5e93\u5bf9\u8d26\u540e\u91cd\u65b0\u9884\u70ed&#xff08;\u4ee5 DB \u4e3a\u51c6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u6570\u636e<\/td>\n<td>\u65e0\u9700\u9884\u70ed&#xff0c;\u8bfb\u8bf7\u6c42\u81ea\u52a8\u91cd\u5efa&#xff08;\u61d2\u52a0\u8f7d&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u9650\u6d41\u6570\u636e<\/td>\n<td>\u81ea\u52a8\u91cd\u5efa&#xff08;\u4ee4\u724c\u6876\u4ece\u6ee1\u5f00\u59cb&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Token \u6570\u636e<\/td>\n<td>\u767d\u540d\u5355\u4e22\u5931 &#061; \u6240\u6709\u7528\u6237\u91cd\u65b0\u767b\u5f55&#xff08;\u5b89\u5168\u89d2\u5ea6\u751a\u81f3\u66f4\u597d&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5173\u952e\u8bbe\u8ba1&#xff1a;\u964d\u7ea7\u5f00\u5173\u5fc5\u987b\u9884\u5148\u5b9e\u73b0\u5e76\u53ef\u52a8\u6001\u63a8\u9001&#xff08;\u914d\u7f6e\u4e2d\u5fc3&#xff09;&#xff0c;\u6545\u969c\u65f6&#034;\u4e00\u952e\u964d\u7ea7&#034;&#xff0c;\u800c\u4e0d\u662f\u73b0\u573a\u6539\u4ee3\u7801\u53d1\u5e03\u3002<\/p>\n<hr \/>\n<h3>\u573a\u666f\u5341\u56db&#xff1a;\u600e\u4e48\u53d1\u73b0\u548c\u4fee\u590d\u5e93\u5b58\u4e0d\u4e00\u81f4&#xff1f;<\/h3>\n<h4>&#x1f3a4; \u9762\u8bd5\u5b98\u8fd9\u4e48\u95ee<\/h4>\n<p>\u201c\u4f60\u8bf4\u6700\u7ec8\u4e00\u81f4&#xff0c;\u90a3\u600e\u4e48\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u4e0d\u4e00\u81f4\u4e86&#xff1f;\u53d1\u73b0\u4e86\u600e\u4e48\u4fee&#xff1f;\u201d<\/p>\n<h4>\u23f1\ufe0f \u5efa\u8bae\u65f6\u957f&#xff1a;2 \u5206\u949f<\/h4>\n<h4>&#x1f5e3;\ufe0f \u4f60\u53ef\u4ee5\u8fd9\u4e48\u7b54<\/h4>\n<p>\u5148\u5766\u767d\u4e0d\u4e00\u81f4\u7684\u6765\u6e90&#xff08;20 \u79d2&#xff09;&#xff1a;<\/p>\n<p>\u201c\u5148\u627f\u8ba4\u73b0\u5b9e&#xff1a;\u4e0d\u7ba1\u673a\u5236\u591a\u5b8c\u5584&#xff0c;\u4e0d\u4e00\u81f4\u5c31\u662f\u4f1a\u53d1\u751f\u2014\u2014MQ \u6d88\u606f\u4e22\u4e86\u3001\u6d88\u8d39\u8005\u6709 bug\u3001\u6709\u4eba\u7ed5\u8fc7\u7cfb\u7edf\u76f4\u63a5\u6539\u4e86\u6570\u636e\u5e93\u3002\u6240\u4ee5\u6211\u4eec\u7684\u601d\u8def\u4e0d\u662f\u2019\u4fdd\u8bc1\u6c38\u8fdc\u4e00\u81f4\u2019&#xff0c;\u800c\u662f\u2019\u5feb\u901f\u53d1\u73b0\u3001\u5b89\u5168\u4fee\u590d\u2019\u3002\u201d<\/p>\n<p>\u5bf9\u8d26\u673a\u5236&#xff08;1 \u5206\u949f&#xff09;&#xff1a;<\/p>\n<p>&#034;\u53d1\u73b0\u9760\u5b9a\u65f6\u5bf9\u8d26&#xff0c;\u6bcf 5 \u5206\u949f\u4e00\u8f6e\u3002\u5bf9\u8d26\u7b49\u5f0f\u662f&#xff1a;Redis \u5e93\u5b58 \u5e94\u8be5\u7b49\u4e8e \u6570\u636e\u5e93\u5e93\u5b58 \u51cf\u53bb \u5f85\u652f\u4ed8\u8ba2\u5355\u5360\u7528\u3002\u4e3a\u4ec0\u4e48\u8981\u51cf\u5f85\u652f\u4ed8\u5360\u7528&#xff1f;\u56e0\u4e3a\u4e0b\u5355\u65f6 Redis \u7acb\u5373\u6263\u4e86\u3001\u6570\u636e\u5e93\u662f\u5f02\u6b65\u6263\u7684&#xff0c;\u5f85\u652f\u4ed8\u8ba2\u5355\u8fd9\u90e8\u5206\u5dee\u503c\u662f\u6b63\u5e38\u7684\u65f6\u95f4\u7a97\u53e3&#xff0c;\u4e0d\u7b97\u5f02\u5e38\u3002<\/p>\n<p>\u5dee\u503c\u8d85\u8fc7\u9608\u503c\u2014\u2014\u6211\u4eec\u8bbe\u7684\u662f 5\u2014\u2014\u5c31\u544a\u8b66\u3002\u4e3a\u4ec0\u4e48\u4e0d\u662f\u4e00\u6709\u5dee\u503c\u5c31\u544a\u8b66&#xff1f;\u56e0\u4e3a\u5bf9\u8d26\u672c\u8eab\u6709\u65f6\u95f4\u7a97\u53e3&#xff0c;\u8dd1\u5bf9\u8d26\u7684\u77ac\u95f4\u53ef\u80fd\u6b63\u597d\u6709\u8ba2\u5355\u5728\u9014&#xff0c;\u5c0f\u5dee\u503c\u662f\u566a\u97f3&#xff0c;\u9608\u503c\u662f\u7528\u6765\u8fc7\u6ee4\u566a\u97f3\u7684\u3002\u8fde\u7eed\u4e24\u8f6e\u5bf9\u8d26\u90fd\u8d85\u9608\u503c&#xff0c;\u624d\u8ba4\u5b9a\u662f\u771f\u5f02\u5e38\u3002<\/p>\n<p>\u9664\u4e86\u5e93\u5b58\u503c\u5bf9\u8d26&#xff0c;\u8fd8\u6709\u66f4\u7cbe\u7ec6\u7684\u6d41\u6c34\u5bf9\u8d26&#xff1a;\u628a\u67d0\u4e2a SKU \u7684\u6d41\u6c34\u6309\u65f6\u95f4\u6392\u5e8f&#xff0c;\u94fe\u5f0f\u6821\u9a8c\u2014\u2014\u6bcf\u6761\u6d41\u6c34\u7684 before_stock \u5e94\u8be5\u7b49\u4e8e\u4e0a\u4e00\u6761\u7684 after_stock&#xff0c;\u6700\u540e\u4e00\u6761\u7684 after_stock \u5e94\u8be5\u7b49\u4e8e\u5f53\u524d\u6570\u636e\u5e93\u5e93\u5b58\u3002\u94fe\u6761\u54ea\u91cc\u65ad\u4e86&#xff0c;\u95ee\u9898\u5c31\u51fa\u5728\u54ea\u4e00\u7b14&#xff0c;\u80fd\u7cbe\u786e\u5b9a\u4f4d\u5230\u5177\u4f53\u64cd\u4f5c&#xff0c;\u800c\u4e0d\u662f\u53ea\u77e5\u9053\u2019\u603b\u6570\u4e0d\u5bf9\u2019\u3002&#034;<\/p>\n<p>\u4fee\u590d\u539f\u5219&#xff08;40 \u79d2&#xff09;&#xff1a;<\/p>\n<p>&#034;\u4fee\u590d\u6709\u4e2a\u94c1\u5f8b&#xff1a;\u4ee5\u6570\u636e\u5e93\u4e3a\u51c6\u3002\u56e0\u4e3a\u6570\u636e\u5e93\u6709\u5b8c\u6574\u6d41\u6c34&#xff0c;\u6bcf\u4e2a\u6570\u5b57\u90fd\u80fd\u8ffd\u6eaf\u6765\u9f99\u53bb\u8109&#xff1b;Redis \u53ea\u662f\u52a0\u901f\u5c42&#xff0c;\u968f\u65f6\u53ef\u4ee5\u4ece\u6570\u636e\u5e93\u91cd\u5efa\u3002\u4fee\u590d\u52a8\u4f5c\u5c31\u662f\u6309\u5bf9\u8d26\u7b49\u5f0f\u91cd\u7b97\u51fa Redis \u5e94\u6709\u7684\u503c&#xff0c;\u76f4\u63a5 SET \u8986\u76d6\u3002<\/p>\n<p>\u4fee\u590d\u65f6\u673a\u6709\u8bb2\u7a76&#xff1a;\u8981\u4e48\u5728\u4f4e\u5cf0\u671f\u505a&#xff0c;\u8981\u4e48\u5148\u628a\u8fd9\u4e2a SKU \u7684\u79d2\u6740\u6682\u505c\u518d\u4fee\u2014\u2014\u4e0d\u7136\u4f60\u521a SET \u5b8c&#xff0c;\u9a6c\u4e0a\u53c8\u6765\u4e00\u6ce2\u6263\u51cf&#xff0c;\u4fee\u4e86\u4e2a\u5bc2\u5bde\u3002<\/p>\n<p>\u9884\u9632\u5c42\u9762&#xff0c;MQ \u5f00\u540c\u6b65\u53d1\u9001\u52a0\u6d88\u8d39\u786e\u8ba4&#xff0c;\u6240\u6709\u6d88\u8d39\u8005\u57fa\u4e8e\u6d41\u6c34\u53f7\u5e42\u7b49&#xff0c;\u8fd8\u6709\u6761\u7ba1\u7406\u7ea2\u7ebf&#xff1a;\u7981\u6b62\u4efb\u4f55\u4eba\u624b\u5de5\u6539\u6570\u636e\u5e93\u5e93\u5b58&#xff0c;\u5fc5\u987b\u8d70\u7cfb\u7edf\u63a5\u53e3&#xff0c;\u4e0d\u7136\u6d41\u6c34\u65ad\u4e86\u94fe&#xff0c;\u5bf9\u8d26\u5c31\u5e9f\u4e86\u3002&#034;<\/p>\n<h4>&#x1f4bb; \u65b9\u6848\u7ec6\u8282\u4e0e\u4ee3\u7801<\/h4>\n<p>\u4e0d\u4e00\u81f4\u7684\u6765\u6e90\u6e05\u5355&#xff1a;<\/p>\n<ul>\n<li>MQ \u6d88\u606f\u4e22\u5931\u6216\u6d88\u8d39\u5931\u8d25&#xff08;Redis \u6263\u4e86&#xff0c;DB \u6ca1\u6263&#xff09;\u3002<\/li>\n<li>\u6d88\u8d39\u8005 bug \u5bfc\u81f4\u91cd\u590d\u6263\u51cf\u6216\u9057\u6f0f\u3002<\/li>\n<li>Redis \u4e0e DB \u4e4b\u95f4\u7684\u7f51\u7edc\u5206\u533a\u3002<\/li>\n<li>\u4eba\u5de5\u64cd\u4f5c\u6570\u636e\u5e93&#xff08;\u7ed5\u8fc7\u7cfb\u7edf&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u5b9a\u65f6\u5bf9\u8d26\u4efb\u52a1\u903b\u8f91&#xff1a;<\/p>\n<p>\u5bf9\u8d26\u903b\u8f91&#xff08;\u6bcf 5 \u5206\u949f&#xff09;&#xff1a;<br \/>\n\u2460 \u83b7\u53d6\u6240\u6709\u53c2\u4e0e\u79d2\u6740\u7684 SKU \u5217\u8868<br \/>\n\u2461 \u5bf9\u6bcf\u4e2a SKU&#xff1a;<br \/>\n   a. \u8bfb\u53d6 Redis \u5e93\u5b58&#xff1a;GET seckill:stock:{skuId}<br \/>\n   b. \u8bfb\u53d6 DB \u5e93\u5b58&#xff1a;SELECT stock FROM inventory WHERE sku_id &#061; ?<br \/>\n   c. \u8ba1\u7b97&#034;\u8fdb\u884c\u4e2d\u8ba2\u5355&#034;\u5360\u7528&#xff1a;<br \/>\n      SELECT SUM(qty) FROM order WHERE sku_id &#061; ? AND status &#061; &#039;\u5f85\u652f\u4ed8&#039;<br \/>\n   d. \u6821\u9a8c\u7b49\u5f0f&#xff1a;<br \/>\n      Redis\u5e93\u5b58 &#061;&#061; DB\u5e93\u5b58 &#8211; \u8fdb\u884c\u4e2d\u8ba2\u5355\u5360\u7528<br \/>\n\u2462 \u7b49\u5f0f\u4e0d\u6210\u7acb\u4e14\u5dee\u503c\u8d85\u8fc7\u9608\u503c&#xff08;\u5982 5&#xff09;&#xff1a;<br \/>\n   a. \u8bb0\u5f55\u5bf9\u8d26\u5dee\u5f02\u65e5\u5fd7<br \/>\n   b. \u89e6\u53d1\u544a\u8b66<br \/>\n   c. \u5dee\u503c\u5c0f\u4e8e\u9608\u503c\u53ef\u80fd\u662f\u65f6\u95f4\u7a97\u53e3\u5bfc\u81f4&#xff0c;\u7b49\u5f85\u4e0b\u4e00\u8f6e<\/p>\n<p>\u6821\u9a8c\u7b49\u5f0f\u63a8\u5bfc&#xff1a;<\/p>\n<ul>\n<li>\u7528\u6237\u4e0b\u5355\u65f6&#xff1a;Redis \u7acb\u5373\u6263\u51cf&#xff0c;DB \u5f02\u6b65\u6263\u51cf\u3002<\/li>\n<li>\u8ba2\u5355\u8fd8\u5728&#034;\u5f85\u652f\u4ed8&#034;\u72b6\u6001&#xff1a;Redis \u5df2\u6263&#xff0c;DB \u53ef\u80fd\u8fd8\u6ca1\u6263&#xff08;\u5f02\u6b65\u5ef6\u8fdf&#xff09;\u3002<\/li>\n<li>\u6240\u4ee5 DB \u5e93\u5b58 &#061; Redis \u5e93\u5b58 &#043; \u5f85\u652f\u4ed8\u8ba2\u5355\u5360\u7528\u3002<\/li>\n<li>\u79fb\u9879\u5f97&#xff1a;Redis \u5e93\u5b58 &#061; DB \u5e93\u5b58 &#8211; \u5f85\u652f\u4ed8\u5360\u7528\u3002<\/li>\n<li>\u5982\u679c\u6240\u6709\u5f02\u6b65\u6d88\u606f\u90fd\u5df2\u6d88\u8d39\u5b8c\u6bd5&#xff08;\u65e0\u79ef\u538b&#xff09;&#xff0c;\u7b49\u5f0f\u7b80\u5316\u4e3a Redis &#061;&#061; DB\u3002<\/li>\n<\/ul>\n<p>\u6d41\u6c34\u94fe\u5f0f\u5bf9\u8d26&#xff08;\u66f4\u7cbe\u7ec6&#xff09;&#xff1a;<\/p>\n<p>\u2460 \u53d6\u67d0 SKU \u67d0\u65f6\u95f4\u6bb5\u7684\u6240\u6709\u6d41\u6c34<br \/>\n\u2461 \u6309\u65f6\u95f4\u6392\u5e8f&#xff0c;\u94fe\u5f0f\u6821\u9a8c&#xff1a;<br \/>\n   \u7b2c 1 \u6761\u7684 before_stock &#061;&#061; \u521d\u59cb\u5e93\u5b58<br \/>\n   \u7b2c N \u6761\u7684 before_stock &#061;&#061; \u7b2c N-1 \u6761\u7684 after_stock<br \/>\n   \u6700\u540e\u4e00\u6761\u7684 after_stock &#061;&#061; \u5f53\u524d DB \u5e93\u5b58<br \/>\n\u2462 \u94fe\u6761\u65ad\u88c2 \u2192 \u8bf4\u660e\u6709\u6d41\u6c34\u7f3a\u5931\u6216\u7be1\u6539&#xff0c;\u5b9a\u4f4d\u5230\u5177\u4f53\u54ea\u4e00\u7b14<\/p>\n<p>\u4fee\u590d\u7b56\u7565&#xff1a;<\/p>\n<ul>\n<li>\u4ee5 DB \u4e3a\u51c6&#xff1a;DB \u6709\u5b8c\u6574\u6d41\u6c34\u53ef\u8ffd\u6eaf&#xff0c;Redis \u53ea\u662f\u52a0\u901f\u5c42&#xff0c;\u6570\u636e\u53ef\u91cd\u5efa\u3002<\/li>\n<li>\u4fee\u590d\u65b9\u5f0f&#xff1a;SET seckill:stock:{skuId} {DB\u5e93\u5b58 &#8211; \u5f85\u652f\u4ed8\u5360\u7528}\u3002<\/li>\n<li>\u4fee\u590d\u65f6\u673a&#xff1a;\u4f4e\u5cf0\u671f\u6267\u884c&#xff0c;\u6216\u5148\u6682\u505c\u8be5 SKU \u7684\u79d2\u6740&#xff0c;\u4fee\u590d\u540e\u6062\u590d\u2014\u2014\u907f\u514d\u4fee\u590d\u8fc7\u7a0b\u4e2d\u65b0\u6263\u51cf\u5bfc\u81f4\u518d\u6b21\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p>\u9884\u9632\u63aa\u65bd&#xff1a;<\/p>\n<ul>\n<li>MQ \u53ef\u9760\u6295\u9012&#xff1a;\u540c\u6b65\u53d1\u9001 &#043; \u6d88\u8d39\u786e\u8ba4&#xff08;ACK&#xff09;&#xff0c;\u51cf\u5c11\u6d88\u606f\u4e22\u5931\u3002<\/li>\n<li>\u6d88\u8d39\u5e42\u7b49&#xff1a;\u6240\u6709\u6d88\u8d39\u8005\u57fa\u4e8e\u6d41\u6c34\u53f7\u5e42\u7b49&#xff0c;\u91cd\u8bd5\u4e0d\u4f1a\u5bfc\u81f4\u91cd\u590d\u3002<\/li>\n<li>\u64cd\u4f5c\u5ba1\u8ba1&#xff1a;\u7981\u6b62\u4eba\u5de5\u76f4\u63a5\u6539\u6570\u636e\u5e93\u5e93\u5b58&#xff0c;\u5fc5\u987b\u901a\u8fc7\u7cfb\u7edf\u63a5\u53e3&#xff08;\u7559\u6d41\u6c34&#xff09;\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u9644&#xff1a;\u9762\u8bd5\u73b0\u573a\u7684\u51e0\u4e2a\u5b9e\u7528\u5fc3\u6cd5<\/h3>\n<h4>\u5fc3\u6cd5\u4e00&#xff1a;\u56de\u7b54\u7684&#034;\u9ec4\u91d1\u7ed3\u6784&#034;<\/h4>\n<p>\u6bcf\u9053\u9898\u90fd\u6309\u8fd9\u4e2a\u8282\u594f\u8bf4&#xff0c;\u7a33\u800c\u4e0d\u4e71&#xff1a;<\/p>\n<p>\u2460 \u63a5\u4f4f\u95ee\u9898&#xff08;10 \u79d2&#xff09;&#xff1a;&#034;\u8fd9\u4e2a\u6211\u5b9e\u9645\u505a\u8fc7&#xff0c;\u6211\u6309 XX \u987a\u5e8f\u8bb2&#034;<br \/>\n\u2461 \u7ed9\u7ed3\u8bba\/\u65b9\u6848&#xff08;30 \u79d2&#xff09;&#xff1a;\u5148\u8bf4\u4f60\u4eec\u600e\u4e48\u505a\u7684<br \/>\n\u2462 \u8bb2\u4e3a\u4ec0\u4e48&#xff08;1 \u5206\u949f&#xff09;&#xff1a;\u4e3a\u4ec0\u4e48\u4e0d\u9009\u522b\u7684\u3001\u5173\u952e\u7ec6\u8282\u7684\u53d6\u820d<br \/>\n\u2463 \u4e3b\u52a8\u4ea4\u4ee3\u5751&#xff08;30~60 \u79d2&#xff09;&#xff1a;\u8fb9\u754c\u60c5\u51b5\u3001\u5931\u8d25\u8def\u5f84\u3001\u515c\u5e95<br \/>\n\u2464 \u4e00\u53e5\u8bdd\u6536\u5c3e&#xff08;10 \u79d2&#xff09;&#xff1a;\u603b\u7ed3\u65b9\u6848\u8981\u70b9<\/p>\n<h4>\u5fc3\u6cd5\u4e8c&#xff1a;\u600e\u4e48\u8bb2&#034;\u6211\u7684\u7ecf\u5386&#034;\u624d\u53ef\u4fe1<\/h4>\n<table>\n<tr>\u4e0d\u8981\u8fd9\u4e48\u8bf4\u8981\u8fd9\u4e48\u8bf4<\/tr>\n<tbody>\n<tr>\n<td>\u201c\u6211\u4eec\u7cfb\u7edf QPS \u767e\u4e07\u201d&#xff08;\u5439\u725b&#xff0c;\u4e00\u8ffd\u95ee\u5c31\u7a7f\u5e2e&#xff09;<\/td>\n<td>\u201c\u6211\u4eec\u5927\u4fc3\u5cf0\u503c\u5927\u6982 2 \u4e07 QPS&#xff0c;\u538b\u6d4b\u65f6\u53d1\u73b0\u7684\u8fd9\u4e2a\u95ee\u9898\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u201c\u6211\u8d1f\u8d23\u6574\u4e2a\u67b6\u6784\u201d&#xff08;\u7ea7\u522b\u4e0d\u7b26&#xff0c;\u5047&#xff09;<\/td>\n<td>\u201c\u8fd9\u5757\u7684\u5e93\u5b58\u56de\u6eda\u662f\u6211\u5199\u7684&#xff0c;\u65b9\u6848\u662f\u8ddf\u4e3b\u7a0b\u4e00\u8d77\u8bc4\u5ba1\u7684\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u53ea\u8bf4\u7ed3\u679c<\/td>\n<td>\u8bf4\u8fc7\u7a0b&#xff1a;\u201c\u7b2c\u4e00\u6b21\u538b\u6d4b\u5bf9\u4e0d\u4e0a\u8d26&#xff0c;\u67e5\u4e86\u4e24\u5929\u53d1\u73b0\u662f MQ \u91cd\u8bd5\u5bfc\u81f4\u7684\u91cd\u590d\u56de\u6eda\u201d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ec6\u8282\u8d8a\u5177\u4f53\u8d8a\u53ef\u4fe1&#xff1a;\u6570\u5b57&#xff08;500 \u6beb\u79d2\u300119MB\u3001\u9608\u503c 5&#xff09;\u3001\u6392\u67e5\u8fc7\u7a0b&#xff08;\u201c\u67e5\u4e86\u4e24\u5929\u201d\u201c\u770b\u6162\u67e5\u8be2\u65e5\u5fd7\u53d1\u73b0\u7684\u201d&#xff09;\u3001\u5f53\u65f6\u7684\u7ea0\u7ed3&#xff08;\u201c\u6211\u4eec\u5185\u90e8\u4e3a\u8fd9\u4e2a\u4e89\u8bba\u8fc7\u201d&#xff09;\u2014\u2014\u8fd9\u4e9b\u662f\u80cc\u4e0d\u51fa\u6765\u3001\u53ea\u6709\u7ecf\u5386\u8fc7\u624d\u6709\u7684\u4e1c\u897f\u3002<\/p>\n<h4>\u5fc3\u6cd5\u4e09&#xff1a;\u88ab\u95ee\u5230\u6ca1\u505a\u8fc7\u7684\u600e\u4e48\u529e<\/h4>\n<p>\u522b\u786c\u7f16\u3002\u53ef\u4ee5\u8fd9\u4e48\u7b54&#xff1a;<\/p>\n<p>\u201c\u8fd9\u4e2a\u573a\u666f\u6211\u4eec\u9879\u76ee\u91cc\u6ca1\u76f4\u63a5\u9047\u5230&#xff0c;\u4f46\u6211\u7406\u89e3\u5b83\u7684\u6838\u5fc3\u77db\u76fe\u662f XX&#xff0c;\u5982\u679c\u8ba9\u6211\u8bbe\u8ba1&#xff0c;\u6211\u4f1a XX&#xff0c;\u53ef\u80fd\u9700\u8981\u6ce8\u610f XX \u7684\u5751\u3002\u201d<\/p>\n<p>\u627f\u8ba4\u6ca1\u505a\u8fc7 &#043; \u5c55\u793a\u63a8\u7406\u80fd\u529b&#xff0c;\u6bd4\u7f16\u4e00\u4e2a\u6f0f\u6d1e\u767e\u51fa\u7684&#034;\u7ecf\u5386&#034;\u597d\u4e00\u767e\u500d\u3002\u9762\u8bd5\u5b98\u9605\u4eba\u65e0\u6570&#xff0c;\u7f16\u7684\u7ecf\u5386\u4e09\u4e2a\u8ffd\u95ee\u5c31\u9732\u9985&#xff0c;\u800c\u9732\u9985\u7684\u4ee3\u4ef7\u662f\u524d\u9762\u6240\u6709\u771f\u8bdd\u90fd\u88ab\u6253\u6298\u3002<\/p>\n<h4>\u5fc3\u6cd5\u56db&#xff1a;\u4e3b\u52a8\u5835\u5751\u6e05\u5355<\/h4>\n<p>\u8bf4\u5230\u8fd9\u4e9b\u8bcd\u7684\u65f6\u5019&#xff0c;\u987a\u624b\u628a\u5751\u5835\u4e86&#xff0c;\u522b\u7b49\u8ffd\u95ee&#xff1a;<\/p>\n<table>\n<tr>\u4f60\u63d0\u5230\u2026\u987a\u624b\u8865\u4e00\u53e5\u2026<\/tr>\n<tbody>\n<tr>\n<td>Lua \u811a\u672c<\/td>\n<td>\u201c\u811a\u672c\u8981\u77ed&#xff0c;\u6267\u884c\u65f6\u662f\u963b\u585e\u5176\u4ed6\u547d\u4ee4\u7684\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u5206\u5e03\u5f0f\u9501<\/td>\n<td>\u201c\u4e3b\u4ece\u5207\u6362\u53ef\u80fd\u4e22\u9501&#xff0c;\u6240\u4ee5\u6b63\u786e\u6027\u9760\u6570\u636e\u5e93\u4e50\u89c2\u9501\u515c\u5e95\u201d<\/td>\n<\/tr>\n<tr>\n<td>MQ \u5f02\u6b65<\/td>\n<td>\u201c\u6d88\u606f\u53ef\u80fd\u4e22\u4e5f\u53ef\u80fd\u91cd&#xff0c;\u6240\u4ee5\u6d88\u8d39\u7aef\u505a\u4e86\u5e42\u7b49&#xff0c;\u518d\u52a0\u5bf9\u8d26\u515c\u5e95\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u7a7a\u503c<\/td>\n<td>\u201cTTL \u8981\u8bbe\u77ed&#xff0c;\u4e0d\u7136\u6570\u636e\u771f\u4e0a\u6765\u4e86\u8fd8\u88ab\u7a7a\u503c\u6321\u7740\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u5ef6\u8fdf\u53cc\u5220<\/td>\n<td>\u201c\u5ef6\u8fdf\u65f6\u95f4\u8981\u5927\u4e8e\u4e00\u6b21\u8bfb\u8bf7\u6c42\u7684\u8017\u65f6&#xff0c;\u6211\u4eec\u662f\u6309 P99 \u7559\u4e86\u4f59\u91cf\u201d<\/td>\n<\/tr>\n<tr>\n<td>\u4ee4\u724c\u6876\u9650\u6d41<\/td>\n<td>\u201c\u9650\u6d41 key \u4e5f\u8981\u8bbe TTL&#xff0c;\u4e0d\u7136\u5168\u662f\u50f5\u5c38 key \u5403\u5185\u5b58\u201d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5fc3\u6cd5\u4e94&#xff1a;\u9ad8\u9891\u8ffd\u95ee\u9884\u5224\u8868<\/h4>\n<table>\n<tr>\u5982\u679c\u4f60\u7684\u56de\u7b54\u4e2d\u63d0\u5230\u2026\u9762\u8bd5\u5b98\u53ef\u80fd\u8ffd\u95ee\u2026\u4f60\u5e94\u9884\u5148\u8bf4\u660e\u2026<\/tr>\n<tbody>\n<tr>\n<td>Lua \u811a\u672c<\/td>\n<td>\u201cLua \u6267\u884c\u65f6\u963b\u585e\u5176\u4ed6\u547d\u4ee4\u5417&#xff1f;\u201d<\/td>\n<td>\u811a\u672c\u8981\u77ed&#xff0c;\u590d\u6742\u903b\u8f91\u62c6\u5206<\/td>\n<\/tr>\n<tr>\n<td>\u5206\u5e03\u5f0f\u9501<\/td>\n<td>\u201c\u4e3b\u4ece\u5207\u6362\u9501\u4e22\u5931\u600e\u4e48\u529e&#xff1f;\u201d<\/td>\n<td>\u6570\u636e\u5e93\u4e50\u89c2\u9501\u515c\u5e95<\/td>\n<\/tr>\n<tr>\n<td>MQ \u5f02\u6b65<\/td>\n<td>\u201c\u6d88\u606f\u4e22\u4e86\u600e\u4e48\u529e&#xff1f;\u201d<\/td>\n<td>\u5bf9\u8d26 &#043; \u91cd\u8bd5 &#043; \u544a\u8b66<\/td>\n<\/tr>\n<tr>\n<td>\u7f13\u5b58\u7a7a\u503c<\/td>\n<td>\u201c\u7a7a\u503c\u5360\u5185\u5b58\u5417&#xff1f;\u201d<\/td>\n<td>TTL \u77ed&#xff08;60\u79d2&#xff09;&#xff0c;\u91cf\u53ef\u63a7<\/td>\n<\/tr>\n<tr>\n<td>\u4ee4\u724c\u6876<\/td>\n<td>\u201c\u4e3a\u4ec0\u4e48\u4e0d\u7528\u6f0f\u6876&#xff1f;\u201d<\/td>\n<td>\u6f0f\u6876\u65e0\u6cd5\u5e94\u5bf9\u7a81\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u5ef6\u8fdf\u53cc\u5220<\/td>\n<td>\u201c\u5ef6\u8fdf\u65f6\u95f4\u600e\u4e48\u5b9a&#xff1f;\u201d<\/td>\n<td>\u5927\u4e8e\u8bfb\u8bf7\u6c42\u8017\u65f6&#xff0c;500ms \u7ecf\u9a8c\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p>\u8fd9 14 \u4e2a\u573a\u666f\u57fa\u672c\u8986\u76d6\u4e86\u7535\u5546\u5e93\u5b58\u4e0e\u6d41\u6c34\u65b9\u5411\u7684 Redis \u9762\u8bd5\u9ad8\u9891\u9898\u3002\u5efa\u8bae\u4e0d\u8981\u80cc&#xff0c;\u800c\u662f\u628a\u6bcf\u4e2a\u573a\u666f\u7684&#034;\u4e3a\u4ec0\u4e48&#034;\u60f3\u901a&#xff0c;\u518d\u628a\u81ea\u5df1\u7684\u9879\u76ee\u7ecf\u5386\u5957\u8fdb&#034;\u6211\u7684\u7ecf\u5386&#034;\u6a21\u677f\u91cc\u2014\u2014\u60f3\u901a\u4e86\u7684\u4e1c\u897f&#xff0c;\u600e\u4e48\u95ee\u90fd\u4e0d\u614c\u3002 &#x1f680;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redis \u9762\u8bd5\u573a\u666f\u5b9e\u6218&#xff1a;\u50cf\u804a\u5929\u4e00\u6837\u628a\u95ee\u9898\u7b54\u660e\u767d &#x1f4cc; \u8fd9\u4efd\u6587\u6863\u662f\u4ec0\u4e48&#xff1a;\u4e0d\u662f\u6559\u79d1\u4e66&#xff0c;\u4e5f\u4e0d\u662f\u9898\u5e93&#xff0c;\u800c\u662f\u4e00\u4efd\\&#8221;\u9762\u8bd5\u73b0\u573a\u6a21\u62df\\&#8221;\u3002\u6bcf\u9053\u9898\u90fd\u8fd8\u539f\u771f\u5b9e\u7684\u9762\u8bd5\u5bf9\u8bdd\u573a\u666f\u2014\u2014\u9762\u8bd5\u5b98\u600e\u4e48\u95ee\u3001\u4f60\u8be5\u600e\u4e48\u5f00\u53e3\u3001\u8bf4\u591a\u4e45\u5408\u9002\u3001\u54ea\u4e9b\u8bdd\u80fd\u52a0\u5206\u3001\u54ea\u4e9b\u8bdd\u4f1a\u7ed9\u81ea\u5df1\u6316\u5751\u3002 \u6bcf\u4e2a\u573a\u666f\u5305\u542b\u4e24\u90e8\u5206&#xff1a; &#x1f5e3;\ufe0f \u53e3\u8ff0\u56de\u7b54&#xff1a;\u9762\u8bd5\u73b0\u573a\\&#8221;\u8bf4\\&#8221;\u51fa\u6765\u7684\u7248\u672c&#xff0c;\u6309\u9650\u65f6\u8282\u594f\u7ec4\u7ec7&#xff1b;&#x1f4bb; \u65b9\u6848\u7ec6<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2661,105,176],"topic":[],"class_list":["post-104483","post","type-post","status-publish","format-standard","hentry","category-server","tag-junit","tag-redis","tag-176"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/104483.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Redis \u9762\u8bd5\u573a\u666f\u5b9e\u6218&#xff1a;\u50cf\u804a\u5929\u4e00\u6837\u628a\u95ee\u9898\u7b54\u660e\u767d &#x1f4cc; \u8fd9\u4efd\u6587\u6863\u662f\u4ec0\u4e48&#xff1a;\u4e0d\u662f\u6559\u79d1\u4e66&#xff0c;\u4e5f\u4e0d\u662f\u9898\u5e93&#xff0c;\u800c\u662f\u4e00\u4efd&quot;\u9762\u8bd5\u73b0\u573a\u6a21\u62df&quot;\u3002\u6bcf\u9053\u9898\u90fd\u8fd8\u539f\u771f\u5b9e\u7684\u9762\u8bd5\u5bf9\u8bdd\u573a\u666f\u2014\u2014\u9762\u8bd5\u5b98\u600e\u4e48\u95ee\u3001\u4f60\u8be5\u600e\u4e48\u5f00\u53e3\u3001\u8bf4\u591a\u4e45\u5408\u9002\u3001\u54ea\u4e9b\u8bdd\u80fd\u52a0\u5206\u3001\u54ea\u4e9b\u8bdd\u4f1a\u7ed9\u81ea\u5df1\u6316\u5751\u3002 \u6bcf\u4e2a\u573a\u666f\u5305\u542b\u4e24\u90e8\u5206&#xff1a; &#x1f5e3;\ufe0f \u53e3\u8ff0\u56de\u7b54&#xff1a;\u9762\u8bd5\u73b0\u573a&quot;\u8bf4&quot;\u51fa\u6765\u7684\u7248\u672c&#xff0c;\u6309\u9650\u65f6\u8282\u594f\u7ec4\u7ec7&#xff1b;&#x1f4bb; \u65b9\u6848\u7ec6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/104483.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-12T07:17:09+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"25 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/104483.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/104483.html\",\"name\":\"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-12T07:17:09+00:00\",\"dateModified\":\"2026-09-12T07:17:09+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/104483.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/104483.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/104483.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/104483.html","og_locale":"zh_CN","og_type":"article","og_title":"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Redis \u9762\u8bd5\u573a\u666f\u5b9e\u6218&#xff1a;\u50cf\u804a\u5929\u4e00\u6837\u628a\u95ee\u9898\u7b54\u660e\u767d &#x1f4cc; \u8fd9\u4efd\u6587\u6863\u662f\u4ec0\u4e48&#xff1a;\u4e0d\u662f\u6559\u79d1\u4e66&#xff0c;\u4e5f\u4e0d\u662f\u9898\u5e93&#xff0c;\u800c\u662f\u4e00\u4efd\"\u9762\u8bd5\u73b0\u573a\u6a21\u62df\"\u3002\u6bcf\u9053\u9898\u90fd\u8fd8\u539f\u771f\u5b9e\u7684\u9762\u8bd5\u5bf9\u8bdd\u573a\u666f\u2014\u2014\u9762\u8bd5\u5b98\u600e\u4e48\u95ee\u3001\u4f60\u8be5\u600e\u4e48\u5f00\u53e3\u3001\u8bf4\u591a\u4e45\u5408\u9002\u3001\u54ea\u4e9b\u8bdd\u80fd\u52a0\u5206\u3001\u54ea\u4e9b\u8bdd\u4f1a\u7ed9\u81ea\u5df1\u6316\u5751\u3002 \u6bcf\u4e2a\u573a\u666f\u5305\u542b\u4e24\u90e8\u5206&#xff1a; &#x1f5e3;\ufe0f \u53e3\u8ff0\u56de\u7b54&#xff1a;\u9762\u8bd5\u73b0\u573a\"\u8bf4\"\u51fa\u6765\u7684\u7248\u672c&#xff0c;\u6309\u9650\u65f6\u8282\u594f\u7ec4\u7ec7&#xff1b;&#x1f4bb; \u65b9\u6848\u7ec6","og_url":"https:\/\/www.wsisp.com\/helps\/104483.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-12T07:17:09+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"25 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/104483.html","url":"https:\/\/www.wsisp.com\/helps\/104483.html","name":"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-12T07:17:09+00:00","dateModified":"2026-09-12T07:17:09+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/104483.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/104483.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/104483.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Redis\u9762\u8bd5\u573a\u666f\u5b9e\u6218"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/104483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=104483"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/104483\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=104483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=104483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=104483"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=104483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}