日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Redis集群中的二次投票問題探討(redis集群二次投票)

Recent,when using Redis in various projects, it is sometimes necessary to use the Redis Cluster mode to ensure service stability. In this case, when using it in voting, there will be a problem of ‘second vote’. Now let’s explore it.

First of all, let’s define the so-called ‘second vote’. When using Redis Cluster as the medium for voting, due to the distributed architecture of the Redis Cluster, the same data of different voting nodes may be distributed to different master and slave nodes of the same Redis Cluster. In this case, if a voting is re-issued, the same data may be written to the master and slave nodes of the same Redis Cluster. At this point, when the data is searched, the voting data of the same user ID appears twice, resulting in the so-called duplicate vote problem.

Therefore, in order to solve this problem, we need to consider how to avoid duplicate votes when using the Redis Cluster. Among them, there are two simple and feasible solutions.

The first is to use pipe buffering. When a user votes, it is necessary to determine whether the user ID has been written in all the master and slave nodes of the corresponding Redis Cluster. If it has not been written, it can be written in batches. For example, you can use the following code:

MULTI — open the buffer pool set userId 1 — Set key-value prs EXEC — Submit the buffer pool

Through this code, we can ensure that a data is written to the Redis Cluster only once.

The second is to use Redlock distributed mutex lock. When using Redis Cluster in voting, we need to use distributed locks in the voting process, such as the Redlock distributed mutex lock. This lock can ensure that the same user ID is written only once to the Redis Cluster. The code is as follows:

//Defines the function used to obtn distributed locks const Redlock = require(“redlock”); const lock = new Redlock([client.client1(), client.client2(), client.client3()], {retryCount: 10}); //Information required when obtning a lock const id = “userId” const resource = [“voting”,id].join(“:”); const ttl = 10000 const attemptLock = function () { lock.lock(resource, ttl).then(function (votingLock) { //Read data set userId 1 // Write data votingLock.unlock().catch(function (err) { //Force to unlock }) }) } // Call the attemptsLock function to lock attemptLock();

Using Redlock distributed mutex lock, we can ensure that the same user ID is written to the Redis Cluster only once, thereby avoiding the ‘second vote’ problem.

In summary, when using Redis cluster in voting services, in order to avoid the ‘second vote’ problem, we can use the pipe buffering or Redlock distributed mutex lock to ensure that only one voting data of the same user ID is written to all the master and slave nodes of the Redis Cluster.

創(chuàng)新互聯(lián)成都網(wǎng)站建設(shè)公司提供專業(yè)的建站服務(wù),為您量身定制,歡迎來電(028-86922220)為您打造專屬于企業(yè)本身的網(wǎng)絡(luò)品牌形象。
成都創(chuàng)新互聯(lián)品牌官網(wǎng)提供專業(yè)的網(wǎng)站建設(shè)、設(shè)計(jì)、制作等服務(wù),是一家以網(wǎng)站建設(shè)為主要業(yè)務(wù)的公司,在網(wǎng)站建設(shè)、設(shè)計(jì)和制作領(lǐng)域具有豐富的經(jīng)驗(yàn)。


標(biāo)題名稱:Redis集群中的二次投票問題探討(redis集群二次投票)
本文URL:http://www.dlmjj.cn/article/dpgchgh.html