🚀【高併發技術專題】你需要了解的秒殺方案

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"一、前言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 很多小夥伴反饋說,高併發學了那麼久,但是在真正做項目時,仍然不知道如何下手處理高併發業務場景!甚至很多小夥伴仍然停留在只是簡單的提供接口(CRUD)階段,不知道學習的併發知識如何運用到實際項目中,就更別提如何構建高併發系統了!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 究竟什麼樣的系統算是高併發系統?今天,我們就一起解密高併發業務場景下典型的秒殺系統的架構,讓大家學以致用。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"二、電商系統架構","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 在電商領域,存在着典型的秒殺業務場景,那何謂秒殺場景呢。簡單的來說就是一件商品的購買人數遠遠大於這件商品的庫存,而且這件商品在很短的時間內就會被搶購一空。比如每年的618、雙11大促,小米新品促銷等業務場景,就是典型的秒殺業務場景。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 我們可以將電商系統的架構簡化成下圖所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a0/a05c1ac7863283b67d608abedf2f8de5.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 由圖所示,我們可以簡單的將電商系統的核心層分爲:負載均衡層、應用層和持久層。接下來,我們就預估下每一層的併發量。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"假如負載均衡層使用的是高性能的Nginx,則我們可以預估Nginx最大的併發度爲:10W+,這裏是以萬爲單位。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"假設應用層我們使用的是Tomcat,而Tomcat的最大併發度可以預估爲800左右,這裏是以百爲單位。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"假設持久層的緩存使用的是Redis,數據庫使用的是MySQL,MySQL的最大併發度可以預估爲1000左右,以千爲單位。Redis的最大併發度可以預估爲5W左右,以萬爲單位。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 所以,負載均衡層、應用層和持久層各自的併發度是不同的,那麼,爲了提升系統的總體併發度和緩存,我們通常可以採取哪些方案呢?","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 系統擴容","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 系統擴容包括垂直擴容和水平擴容,增加設備和機器配置,絕大多數的場景有效。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 緩存","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"本地緩存或者集中式緩存,減少網絡IO,基於內存讀取數據。大部分場景有效。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"3. 讀寫分離","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"採用讀寫分離,分而治之,增加機器的並行處理能力。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"三、秒殺系統的特點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於秒殺系統來說,我們可以從業務和技術兩個角度來闡述其自身存在的一些特點。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 業務特點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 這裏,我們可以使用","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"12306","attrs":{}},{"type":"text","text":"網站來舉例,每年春運時,12306網站的訪問量是非常大的,但是網站平時的訪問量卻是比較平緩的,也就是說,每年春運時節,12306網站的訪問量會出現瞬時突增的現象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 再比如,小米秒殺系統,在上午10點開售商品,10點前的訪問量比較平緩,10點時同樣會出現併發量瞬時突增的現象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 所以,秒殺系統的流量和併發量我們可以使用下圖來表示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/bf/bf928723233d72507001dfe9def0b6b3.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"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":" 我們可以將秒殺系統的特點總結如下:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/03/030813b4cdac3fd403583b3aa88a42f0.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"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","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 規定的時間內進行;秒殺活動中商品的數量有限;商品的價格會遠遠低於原來的價格,也就是說,在秒殺活動中,商品會以遠遠低於原來的價格出售。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 例如,秒殺活動的時間僅限於某天上午10點到10點半,商品數量只有10萬件,售完爲止,而且商品的價格非常低,例如:1元購等業務場景。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 限時、限量和限價可以單獨存在,也可以組合存在。","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":" 需要提前配置活動;活動還未開始時,用戶可以查看活動的相關信息;秒殺活動開始前,對活動進行大力宣傳。","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":" 購買的人數數量龐大;商品會迅速售完。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 在系統流量呈現上,就會出現一個突刺現象,此時的併發訪問量是非常高的,大部分秒殺場景下,商品會在極短的時間內售完。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 技術特點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以將秒殺系統的技術特點總結如下:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d3/d3c4532c43beaac927b6eef84d52c565.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"①. 瞬時併發量非常高","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"大量用戶會在同一時間搶購商品;瞬間併發峯值非常高。","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":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"系統中商品頁訪問量巨大;商品的可購買數量非常少;庫存的查詢訪問數量遠遠大於商品的購買數量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 在商品頁中往往會加入一些限流措施,例如早期的秒殺系統商品頁會加入驗證碼來平滑前端對系統的訪問流量,近期的秒殺系統商品詳情頁會在用戶打開頁面時,提示用戶登錄系統。這都是對系統的訪問進行限流的一些措施。","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":" 秒殺系統的業務流程一般比較簡單;總體上來說,秒殺系統的業務流程可以概括爲:下單減庫存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 針對這種短時間內大流量的系統來說,就不太適合使用系統擴容了,因爲即使系統擴容了,也就是在很短的時間內會使用到擴容後的系統,大部分時間內,系統無需擴容即可正常訪問。那麼,我們可以採取哪些方案來提升系統的秒殺性能呢?","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"四、秒殺系統方案","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"針對秒殺系統的特點,我們可以採取如下的措施來提升系統的性能。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/3827d0800c13e2098d8e2301f355c086.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"①. 異步解耦","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"將整體流程進行拆解,核心流程通過隊列方式進行控制。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"②. 限流防刷","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"控制網站整體流量,提高請求的門檻,避免系統資源耗盡。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"③. 資源控制","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"將整體流程中的資源調度進行控制,揚長避短。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 由於應用層能夠承載的併發量比緩存的併發量少很多。","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"所以,在高併發系統中,我們可以直接使用OpenResty由負載均衡層訪問緩存,避免了調用應用層的性能損耗","attrs":{}},{"type":"text","text":"。同時,由於秒殺系統中,商品數量比較少,我們也可以使用動態渲染技術,CDN技術來加速網站的訪問性能。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如果在秒殺活動開始時,併發量太高時,我們可以將用戶的請求放入隊列中進行處理,併爲用戶彈出排隊頁面。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"五、秒殺系統時序圖","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 網上很多的秒殺系統和對秒殺系統的解決方案,並不是真正的秒殺系統,他們採用的只是同步處理請求的方案,一旦併發量真的上來了,他們所謂的秒殺系統的性能會急劇下降。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 下單流程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來看一下秒殺系統在同步下單時的時序圖:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/30/308674945866ef6335247724c31971f7.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"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":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"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","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"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","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"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":"color","attrs":{"color":"#F5222D","name":"red"}},{"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":"color","attrs":{"color":"#F5222D","name":"red"}},{"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":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 在秒殺業務中,往往會將商品庫存等信息存放在緩存中,此時,還需要驗證秒殺活動使用的商品庫存是否足夠,並且需要扣減秒殺活動的商品庫存數量。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"計算秒殺的價格","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"由於在秒殺活動中,商品的秒殺價格和商品的真實價格存在差異,所以,需要計算商品的秒殺價格。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 注意:如果在秒殺場景中,系統涉及的業務更加複雜的話,會涉及更多的業務操作,這裏,我只是列舉出一些常見的業務操作。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"②. 提交訂單","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"訂單入口","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"將用戶提交的訂單信息保存到數據庫中。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"扣減真實庫存","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"此時的排隊時間可能是15秒,也可能是30秒,甚至是更長時間。這就存在一個問題:在用戶發起秒殺請求到服務器返回結果的這段時間內,客戶端和服務器之間的連接不會被釋放,這就會佔大量佔用服務器的資源。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"很多介紹如何實現秒殺系統的文章都是採用的這種方式,那麼,這種方式能做秒殺系統嗎?答案是可以做,但是這種方式支撐的併發量並不是太高。此時,有些網友可能會問:我們公司就是這樣做的秒殺系統啊!上線後一直在用,沒啥問題啊!","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 我想說的是:使用同步下單方式確實可以做秒殺系統,但是同步下單的性能不會太高。之所以你們公司採用同步下單的方式做秒殺系統沒出現大的問題,那是因爲你們的秒殺系統的併發量沒達到一定的量級,也就是說,你們的秒殺系統的併發量其實並不高。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 所以,很多所謂的秒殺系統,存在着秒殺的業務,但是稱不上真正的秒殺系統,原因就在於他們使用的是同步的下單流程,限制了系統的併發流量。之所以上線後沒出現太大的問題,是因爲系統的併發量不高,不足以壓死整個系統。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 如果12306、淘寶、天貓、京東、小米等大型商城的秒殺系統是這麼玩的話,那麼,他們的系統遲早會被玩死,他們的系統工程師不被開除纔怪!所以,在秒殺系統中,這種同步處理下單的業務流程的方案是不可取的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 以上就是同步下單的整個流程操作,如果下單流程更加複雜的話,就會涉及到更多的業務操作。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 異步下單流程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 既然同步下單流程的秒殺系統稱不上真正的秒殺系統,那我們就需要採用異步的下單流程了。異步的下單流程不會限制系統的高併發流量。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c4/c44b7343e6dfaf2a763949926f83afac.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"①. 用戶發起秒殺請求","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 用戶發起秒殺請求後,商城服務會經過如下業務流程。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"檢測驗證碼是否正確","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 例如,在秒殺活動中,我們出售1000件商品,此時在消息隊列中存在1000個請求,如果後續仍然有用戶發起秒殺請求,則後續的請求我們可以不再處理,直接向用戶返回商品已售完的提示。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 所以,使用限流後,我們可以更快的處理用戶的請求和釋放連接的資源。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"發送MQ","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 用戶的秒殺請求通過前面的驗證後,我們就可以將用戶的請求參數等信息發送到MQ中進行異步處理,同時,向用戶響應結果信息。在商城服務中,會有專門的異步任務處理模塊來消費消息隊列中的請求,並處理後續的異步流程。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 用戶發起秒殺請求時,異步下單流程比同步下單流程處理的業務操作更少,它將後續的操作通過MQ發送給異步處理模塊進行處理,並迅速向用戶返回響應結果,釋放請求連接。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"②. 異步處理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們可以將下單流程的如下操作進行異步處理。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"判斷活動是否已經結束。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"判斷本次請求是否處於系統黑名單,爲了防止電商領域同行的惡意競爭可以爲系統增加黑名單機制,將惡意的請求放入系統的黑名單中。可以使用攔截器統計訪問頻次來實現。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"扣減緩存中的秒殺商品的庫存數量。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"生成秒殺Token,這個Token是綁定當前用戶和當前秒殺活動的,只有生成了秒殺Token的請求才有資格進行秒殺活動。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 這裏我們引入了異步處理機制,在異步處理中,系統使用多少資源,分配多少線程來處理相應的任務,是可以進行控制的。","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":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"這裏,可以採取客戶端短輪詢查詢是否獲得秒殺資格的方案。例如,客戶端可以每隔3秒鐘輪詢請求服務器,查詢是否獲得秒殺資格,這裏,我們在服務器的處理就是判斷當前用戶是否存在秒殺Token,如果服務器爲當前用戶生成了秒殺Token,則當前用戶存在秒殺資格。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 此時,可能會有網友會問:採用短輪詢查詢的方式,會不會存在直到超時也查詢不到是否具有秒殺資格的狀態呢?","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"答案是:有可能!這裏我們試想一下秒殺的真實場景,商家參加秒殺活動本質上不是爲了賺錢,而是提升商品的銷量和商家的知名度,吸引更多的用戶來買自己的商品。","attrs":{}},{"type":"text","text":"所以,我們不必保證用戶能夠100%的查詢到是否具有秒殺資格的狀態。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","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","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 客戶端提交秒殺結算時,會將秒殺Token一同提交到服務器,商城服務會驗證當前的秒殺Token是否有效","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"加入秒殺購物車","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"商城服務在驗證秒殺Token合法並有效後,會將用戶秒殺的商品添加到秒殺購物車。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"⑤. 提交訂單","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"訂單入庫","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"將用戶提交的訂單信息保存到數據庫中。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"刪除Token","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"秒殺商品訂單入庫成功後,刪除秒殺Token。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 這裏大家可以思考一個問題:我們爲什麼只在異步下單流程的粉色部分採用異步處理,而沒有在其他部分採取異步削峯和填谷的措施呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 這是因爲在異步下單流程的設計中,無論是在產品設計上還是在接口設計上,我們在用戶發起秒殺請求階段對用戶的請求進行了限流操作,可以說,系統的限流操作是非常前置的。在用戶發起秒殺請求時進行了限流,系統的高峯流量已經被平滑解決了,再往後走,其實系統的併發量和系統流量並不是非常高了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 所以,網上很多的文章和帖子中在介紹秒殺系統時,說是在下單時使用異步削峯來進行一些限流操作,那都是在扯淡!因爲下單操作在整個秒殺系統的流程中屬於比較靠後的操作了,限流操作一定要前置處理,在秒殺業務後面的流程中做限流操作是沒啥用的。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"六、高併發“黑科技”與致勝奇招","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 假設,在秒殺系統中我們使用Redis實現緩存,假設Redis的讀寫併發量在","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"5萬","attrs":{}},{"type":"text","text":"左右。我們的商城秒殺業務需要支持的併發量在100萬左右。如果這100萬的併發全部打入Redis中,Redis很可能就會掛掉,那麼,我們如何解決這個問題呢?接下來,我們就一起來探討這個問題。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 在高併發的秒殺系統中,如果採用Redis緩存數據,則Redis緩存的併發處理能力是關鍵,因爲很多的前綴操作都需要訪問Redis。而異步削峯只是基本的操作,關鍵還是要保證Redis的併發處理能力。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 解決這個問題的關鍵思想就是:分而治之,將商品庫存分開放。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1. 暗度陳倉","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 我們在Redis中存儲秒殺商品的庫存數量時,可以將秒殺商品的庫存進行“分割”存儲來提升Redis的讀寫併發量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 例如,原來的秒殺商品的id爲10001,庫存爲1000件,在Redis中的存儲爲(10001, 1000),我們將原有的庫存分割爲5份,則每份的庫存爲200件,此時,我們在Redia中存儲的信息爲(10001_0, 200),(10001_1, 200),(10001_2, 200),(10001_3, 200),(10001_4, 200)。","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/90/90f228a073247bd45ba9bfd7b39f8959.jpeg","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 此時,我們將庫存進行分割後,每個分割後的庫存使用商品id加上一個數字標識來存儲,這樣,在對存儲商品庫存的每個Key進行Hash運算時,得出的Hash結果是不同的,這就說明,存儲商品庫存的Key有很大概率不在Redis的同一個槽位中,這就能夠提升Redis處理請求的性能和併發量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":" 分割庫存後,我們還需要在Redis中存儲一份商品id和分割庫存後的Key的映射關係,此時映射關係的Key爲商品的id,也就是10001,Value爲分割庫存後存儲庫存信息的Key,也就是10001_0,10001_1,10001_2,10001_3,10001_4。在Redis中我們可以使用List來存儲這些值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 在真正處理庫存信息時,我們可以先從Redis中查詢出秒殺商品對應的分割庫存後的所有Key,同時使用AtomicLong來記錄當前的請求數量,使用請求數量對從Redia中查詢出的秒殺商品對應的分割庫存後的所有Key的長度進行求模運算,得出的結果爲0,1,2,3,4。再在前面拼接上商品id就可以得出真正的庫存緩存的Key。此時,就可以根據這個Key直接到Redis中獲取相應的庫存信息。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2. 移花接木","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 在高併發業務場景中,我們可以直接使用Lua腳本庫(OpenResty)從負載均衡層直接訪問緩存。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" ","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#F5222D","name":"red"}},{"type":"strong","attrs":{}}],"text":"這裏,我們思考一個場景:如果在秒殺業務場景中,秒殺的商品被瞬間搶購一空。此時,用戶再發起秒殺請求時,如果系統由負載均衡層請求應用層的各個服務,再由應用層的各個服務訪問緩存和數據庫,其實,本質上已經沒有任何意義了,因爲商品已經賣完了,再通過系統的應用層進行層層校驗已經沒有太多意義了!!而應用層的併發訪問量是以百爲單位的,這又在一定程度上會降低系統的併發度。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":" 爲了解決這個問題,此時,我們可以在系統的負載均衡層取出用戶發送請求時攜帶的用戶id,商品id和秒殺活動id等信息,直接通過Lua腳本等技術來訪問緩存中的庫存信息。如果秒殺商品的庫存小於或者等於0,則直接返回用戶商品已售完的提示信息,而不用再經過應用層的層層校驗了。針對這個架構,我們可以參見本文中的電商系統的架構圖(正文開始的第一張圖)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章