實現接口冪等性的四種方案!

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"1 前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家好,我是阿沐!”冪等“這個詞語或許小夥伴很少見,基本上中小型公司或者一些大公司都未使用過,但是並不代表小夥伴們沒有接觸到。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲啥我會扯到這個技術話題?緣由就是20年我面試了一些大廠包括身邊朋友的面試經歷,例如騰訊、網易、字節等等大廠,其中大都會遇到”冪等的概念、理解以及實現與應用“,那麼下面就聽我一一道來冪等的相關知識。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"2 什麼是冪等性?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"數學中","attrs":{}},{"type":"text","text":":在一次元運算爲冪等時,其作用在任一元素兩次後會和其作用一次的結果相同;在二次元運算爲冪等時,自己重複運算的結果等於它自己的元素。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"計算機學中","attrs":{}},{"type":"text","text":":冪等指多次操作產生的影響只會跟一次執行的結果相同,通俗的說:某個行爲重複的執行,最終獲取的結果是相同的,不會因爲重複執行對系統造成變化。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3 爲什麼要使用冪等性?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"衆所周知,目前隨着js的發展web端慢慢轉變了前後端分離,通過接口實現;小程序、app同樣都是api實現,基本上接口都是正常的返回信息,並未涉及到重複提交或者是併發提交的情況。但假如我們考慮的細緻一點,比如電商系統,抽獎活動、用戶反饋、訂單支付、消息消費、商品評價、商品點贊等這些都是和冪等息息相關。舉幾個例子給小夥伴們看下:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"① 用戶","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"重複下訂單","attrs":{}},{"type":"text","text":":當用戶下單時,因爲網絡問題或者手速過快,導致重複下單。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"② 消息","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"重複消費","attrs":{}},{"type":"text","text":":當使用MQ消息中間件時候,如果消息中間件發生異常出現錯誤未及時提交消費信息,導致消息被重複消費。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"③ ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"抽獎活動(券)","attrs":{}},{"type":"text","text":":當用戶參加抽獎活動需要消耗抽獎券時,如果出現併發請求導致抽獎券餘額更新錯誤。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"④ ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"重複提交表單","attrs":{}},{"type":"text","text":":當用戶填寫表單提交時,可能會因爲用戶點多次連擊提交或者網絡波動導致服務端未及時響應,會導致用戶重複的提交表單,就出現了同一個表單多次請求。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這些只是我們常見的一些狀況,還需要根據自己的項目的實際情況進行分析,判斷是否需要冪等操作,舉個簡單例子:運營做了一次大型活動,參與人數10w+(每人只能給一個用戶點贊衝榜),活動結束後運營需要覆盤,這個時候發現一些用戶給一個人點贊又多次狀況。這個時候,嘿嘿嘿...... 開發過來背鍋嘍!","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"4 我們如何在業務功能上實現冪等性?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常數據庫實現主要是利用數據庫表中主鍵唯一約束+唯一索引的特性,如果主鍵唯一或者設置了複合唯一索引,在”插入“數據的時候就是冪等性操作。例如還有悲觀鎖、樂觀鎖、redis鎖、token令牌、依賴前後端配合生成請求序列號。ps:基本上面試時,大廠都會被問到的問題,不要問我爲什麼?因爲面試官喜歡問。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"下面是設計一個活動抽獎大轉盤用戶抽獎券餘額:抽獎券來源參加活動獲取,抽獎消耗券場景:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"CREATE TABLE `mumu_test` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主鍵自增id',\n `userid` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '用戶id',\n `act_id` varchar(125) COLLATE utf8mb4_bin NOT NULL DEFAULT '' COMMENT '活動ID',\n `lottery` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '餘額',\n PRIMARY KEY (`id`),\n UNIQUE KEY `uniq_uid_aid` (`userid`,`act_id`) USING BTREE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='沐沐測試春節抽獎券記錄表';","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"唯一主鍵索引實現冪等性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常情況下,我們在做這種用戶活動抽獎券記錄數據時,會先","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"select","attrs":{}},{"type":"text","text":"下看看是否已經有插入的記錄了,如果已存在則","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"update","attrs":{}},{"type":"text","text":",否則","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"insert","attrs":{}},{"type":"text","text":"。那麼我現在先說說不存在添加數據的情況:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"存在用戶在做活動任務時,因爲網絡抖動導致服務端響應超時,這個時候用戶以爲並沒領取獎券成功,就會瘋狂的點擊領取按鈕,那麼就會導致同一個任務獎券出現多次請求,那麼我們第一次insert添加肯定成功了,當併發請求過來時就會重複執行以下sql語句:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"inser into mumu_test('userid','act_id','lottery')values(123,'spring',1)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"由於存在userid+act_id唯一鍵,那麼就會出現只有一條數據插入成功,其他的數據就會插入失敗,保證了數據的冪等。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"推薦使用","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"樂觀鎖實現冪等性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通俗地講:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"它的心態就是很樂觀,每次去拿數據的時候都認爲別人不會修改,所以不會上鎖","attrs":{}},{"type":"text","text":",但是在提交更新的時候會判斷一下在此期間別人有沒有去更新這個數據。它就像單純的孩子一樣,總是認爲不會產生併發衝突的場景,我只是在你提交操作時檢查是否違反數據完整性。所以樂觀鎖適用於讀多寫少的應用場景,這樣可以提高吞吐量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"劃重點:一般使用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"版本號控制version","attrs":{}},{"type":"text","text":",即爲數據","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"增加一個版本標識","attrs":{}},{"type":"text","text":",一般是通過爲數據庫錶行數據增加一個數字類型的“version”字段來實現。當讀取數據時,會將version字段的值一同讀出,數據每更新一次,對此version值加1操作。當我們提交更新的時候,判斷數據庫表對應記錄的當前版本信息與第一次取出來的version值進行比對,如果數據庫表當前版本號與第一次取出來的version值相等,則予以更新,否則認爲是非法操作。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"場景應用:針對上面的表我們增加一個版本標識:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"alter table mumu_test add `version` smallint(5) unsigned not null default '0' comment '版本號'","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"添加成功之後,更新操作:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"1.獲取抽獎券:\nupdate mumu_test set lottery = lottery + 5, version = version + 1 where id = 123 and version = 1;\n\n2.消耗抽獎券\nupdate mumu_test set lottery = lottery - 1, version = version + 1 where id = 123 and version = 2;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實現原理:更新數據的同時version+1,然後判斷本次update操作的影響行數,如果大於0,則說明本次更新成功,如果等於0,則說明本次更新沒有讓數據變更。當併發請求過來時,只需要拿到select的版本號,進行更新操作即可(where可帶上主鍵id),保證冪等。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"推薦使用","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"悲觀鎖實現冪等性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"顧名思義,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"悲觀鎖它是一種悲觀的心裏狀態,對應於生活中悲觀的人總是想着事情往壞的方向發展","attrs":{}},{"type":"text","text":"。它像是一個徹底地loser,它認爲別人每次去拿數據都會修改這條數據,所以每次拿數據的時候,都會使數據處於鎖定狀態。執行下面sql語句鎖住該條記錄:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"select * from mumu_test where userid = 123 and act_id = 'spring' for update;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"大家可以看到我並沒有使用主鍵id是查詢,首先我們並不知道這條記錄id值,所以我們通過uid+aid組合的唯一建作爲鎖錶行記錄條件,一定要使用主鍵或者唯一建,不然會將整張表都被鎖住,那麼其他的用戶就無法操作了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲悲觀鎖是需要在同一個事物操作過程中鎖住一行數據,假如我們事務邏輯耗時比較久,就會導致後面請求的堆積,直接影響到了整體響應時長。","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不推薦使用","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"Token令牌如何實現冪等性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所謂的token令牌其實就是爲了防止用戶重複提交一個表單信息,這一點基本上PHP的框架都會帶有token驗證。服務端需要生成一個","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"全局唯一的id","attrs":{}},{"type":"text","text":",(例如:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"snowflake雪花算法","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"美團Leaf算法","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"滴滴TinyID算法","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"百度Uidgenerator算法","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"uuid","attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"redis","attrs":{}},{"type":"text","text":"等)。","attrs":{}}]},{"type":"numberedlist","attrs":{"start":null,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"客戶端每次進入表單頁面可以優先申請一個唯一令牌存儲本地,服務端存儲令牌token值(redis,文件,memcache都可)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"每次發送請求時可以在Headers頭部中帶上當前這個token令牌","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"服務端驗證token是否存在,存在則刪除token,執行後續業務邏輯;不存在則響應客戶端重複提交提示語","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"生成全局唯一id的代碼,大家可以網上自行搜索,基本上是千篇一律的,放心抄過來使用就可以了。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"最後總結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"冪等性基本上在中大型公司項目需求中都能遇到,尤其是現在消息中間件(kafka、rabbitmq等)的廣泛使用,更加註重消息的冪等。那麼像我之前在電商公司,支付訂單、抽獎券、部分活動相關的中臺服務對接口的冪等性都是很重要的,所以我們在日常開發中,可以針對不同的業務場景選擇合適的冪等方案,即可滿足要求同時也減少性能影響,更重要的是不會因爲出","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"bug","attrs":{}},{"type":"text","text":"被產品運營","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"diss","attrs":{}},{"type":"text","text":"。開發真的好卑微啊~~~","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"聊冪等性這個詞語,也是自己想了很久。在這之前我推薦不少開發(經驗基本上5年+)到大廠,他們給的反饋就有冪等這個概念的詢問。ps:當然並不說明他們能力不行、技術差,冪等名詞一般大家很少去注意,尤其是常年待在一個公司,並不一定能接觸到冪等業務,就算接觸到了類似場景;而都是認爲對於一致性的要求不能重複插入數據,並不會想起是冪等這個名詞。所以並不奇怪,大家也不要在面試中遇到新的名詞就內心慌亂,手心出汗、腿發抖、發冷汗,我們完全可以跟面試官聊,是否可以換一種方式來問這個問題;我相信大部分的面試官都能接受,頂多就認爲你知識量不夠廣,不知道這些專業術語等等,不會太影響你後面的解決思路。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"好了,我是阿沐,謝謝你代碼寫的這麼好,還關注了我!⛽️ ⛽️ ⛽️","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章