主頁 > 知識庫 > 詳解redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

詳解redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

熱門標(biāo)簽:AI電銷 鐵路電話系統(tǒng) 百度競價排名 Linux服務(wù)器 地方門戶網(wǎng)站 呼叫中心市場需求 網(wǎng)站排名優(yōu)化 服務(wù)外包

前段時間在做用戶畫像的時候,遇到了這樣的一個問題,記錄某一個商品的用戶購買群,剛好這種需求就可以用到Redis中的Set,key作為productID,value就是具體的customerid集合,后續(xù)的話,我就可以通過productid來查看該customerid是否買了此商品,如果購買了,就可以有相關(guān)的關(guān)聯(lián)推薦,當(dāng)然這只是系統(tǒng)中的一個小業(yè)務(wù)條件,這時候我就可以用到SADD操作方法,代碼如下:

    static void Main(string[] args)
    {
      ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.23.151:6379");

      var db = redis.GetDatabase();

      var productID = string.Format("productID_{0}", 1);

      for (int i = 0; i  10; i++)
      {
        var customerID = i;

        db.SetAdd(productID, customerID);
      }
    }

一:問題

    但是上面的這段代碼很明顯存在一個大問題,Redis本身就是基于tcp的一個Request/Response protocol模式,不信的話,可以用wireshark監(jiān)視一下:

 

從圖中可以看到,有很多次的192.168.23.1 => 192.168.23.151 之間的數(shù)據(jù)往返,從傳輸內(nèi)容中大概也可以看到有一個叫做productid_xxx的前綴,

那如果有百萬次局域網(wǎng)這樣的round trip,那這個延遲性可想而知,肯定達(dá)不到我們預(yù)想的高性能。

 二:解決方案【Batch】

     剛好基于我們現(xiàn)有的業(yè)務(wù),我可以定時的將批量的productid和customerid進(jìn)行分組整合,然后用batch的形式插入到某一個具體的product的set中去,接下來我可以把上面的代碼改成類似下面這樣:

     static void Main(string[] args)
     {
       ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
 
       var db = redis.GetDatabase();
 
       var productID = string.Format("productID_{}", );
 
       var list = new Listint>();
 
 
       for (int i = ; i  ; i++)
       {
         list.Add(i);
       }
 
       db.SetAdd(productID, list.Select(i => (RedisValue)i).ToArray());
     }
 

 

從截圖中傳輸?shù)膔equest,response可以看到,這次我們一次性提交過去,極大的較少了在網(wǎng)絡(luò)傳輸方面帶來的尷尬性。。

 三:再次提出問題

product維度的畫像我們可以解決了,但是我們還有一個customerid的維度,也就是說我需要維護(hù)一個customerid為key的set集合,其中value的值為該customerid的各種平均值,比如說“總交易次數(shù)”,“總交易金額”。。。等等這樣的聚合信息,然后推送過來的是批量的customerid,也就是說你需要定時維護(hù)一小嘬set集合,在這種情況下某一個set的批量操作就搞不定了。。。原始代碼如下:

     static void Main(string[] args)
     {
       ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
 
       var db = redis.GetDatabase();
 
 
       //批量過來的數(shù)據(jù): customeridlist, ordertotalprice,具體業(yè)務(wù)邏輯省略
       var orderTotalPrice = ;
 
       var customerIDList = new Listint>();
 
       for (int i = ; i  ; i++)
       {
         customerIDList.Add(i);
       }
 
       //foreach更新每個redis 的set集合
       foreach (var item in customerIDList)
       {
         var customerID = string.Format("customerid_{}", item);
 
         db.SetAdd(customerID, orderTotalPrice);
       }
     }

四:解決方案【PipeLine】

=上面這種代碼在生產(chǎn)上當(dāng)然是行不通的,不過針對這種問題,redis早已經(jīng)提出了相關(guān)的解決方案,那就是pipeline機(jī)制,原理還是一樣,將命令集整合起來通過一條request請求一起送過去,由redis內(nèi)部fake出一個client做批量執(zhí)行操作,代碼如下:

     static void Main(string[] args)
     {
       ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("...:");
 
       var db = redis.GetDatabase();
 
 
       //批量過來的數(shù)據(jù): customeridlist, ordertotalprice,具體業(yè)務(wù)邏輯省略
       var orderTotalPrice = ;
 
       var customerIDList = new Listint>();
 
       for (int i = ; i  ; i++)
       {
         customerIDList.Add(i);
       }
 
       var batch = db.CreateBatch();
 
       foreach (var item in customerIDList)
       {
         var customerID = string.Format("customerid_{}", item);
 
         batch.SetAddAsync(customerID, orderTotalPrice);
       }
 
       batch.Execute();
     }

然后,我們再看下面的wireshark截圖,可以看到有很多的SADD這樣的小命令,這就說明有很多命令是一起過去的,大大的提升了性能。

 

 最后可以再看一下redis,數(shù)據(jù)也是有的,是不是很爽~~~

192.168.23.151:6379> keys *
 1) "customerid_0"
 2) "customerid_9"
 3) "customerid_1"
 4) "customerid_3"
 5) "customerid_8"
 6) "customerid_2"
 7) "customerid_7"
 8) "customerid_5"
 9) "customerid_6"
10) "customerid_4"

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Redis cluster集群的介紹
  • Spring-data-redis操作redis cluster的示例代碼
  • Windows環(huán)境下Redis Cluster環(huán)境搭建(圖文)
  • 如何用docker部署redis cluster的方法
  • 在Redis集群中使用pipeline批量插入的實(shí)現(xiàn)方法
  • python使用pipeline批量讀寫redis的方法
  • 詳解Java使用Pipeline對Redis批量讀寫(hmset&hgetall)
  • redis cluster支持pipeline的實(shí)現(xiàn)思路

標(biāo)簽:衡水 崇左 銅川 仙桃 湘潭 黃山 蘭州 湖南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266