当前位置:网站首页>Troubleshooting and handling of an online problem caused by redis zadd
Troubleshooting and handling of an online problem caused by redis zadd
2022-07-02 09:07:00 【Programmer Qiqi】
background
Recently, there are user feedback , The anchor received 1881 Numerical gift , Digital display under the avatar 881, Normally , Should be displayed 1881 The numerical .

After investigation , Because redis zadd The problem of data inconsistency in the case of concurrency .
Troubleshoot problems
After checking the gift giving log , Find out mongodb Data update is normal , however redis Data exception , After checking the business code, we found the problem .
The business code is as follows :

The logic here is to update mongodb, Then on redis The numerical value is carried out zadd Cover . In general, there is no problem , But if you encounter a little high concurrency :
- A Threads and B Threads are running simultaneously on mongo Modify , At the database level , B On A Modification of .
- But in the process , Threads B Ahead of thread A call redis modify , Threads A Yes redis 's changes override B The numerical .
- Lead to redis Data maintenance error in .
Problem fix
Use Lua Script modification redis value , In case of concurrency, override logic is not processed , Avoid old values overwriting new values , utilize mongodb Self accretion guarantees atomicity . The code is as follows :

Lua The script is as follows :
SAFE_ZADD_SCRIPT = """
local key = KEYS[1]
local field = ARGV[1]
local new_value = tonumber(ARGV[2])
local old_value = redis.call("zscore", key, field)
local res = nil
if (not old_value) or (tonumber(old_value) < new_value)
then
res = redis.call('zadd', key, new_value, field)
end
return res
"""
Copy code Why not use zincrby?
zincrby Although it is guaranteed that there will be no concurrency problems , But if key Fail or be cleaned up , It will cause the ranking list to change from 0 Start counting , Causing data errors .
边栏推荐
猜你喜欢
![[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the](/img/dc/c0ea188ef353ded86759dbe9b29df3.jpg)
[staff] the lines and spaces of the staff (the nth line and the nth space in the staff | the plus N line and the plus N space on the staff | the plus N line and the plus N space below the staff | the

kubernetes部署loki日志系统

Don't spend money, spend an hour to build your own blog website

Avoid breaking changes caused by modifying constructor input parameters
![[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)](/img/7f/2cd789339237b7a881bfed7b7545a9.jpg)
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)

Minecraft install resource pack

Minecraft plug-in service opening

Linux安装Oracle Database 19c RAC

Synchronize files using unison

win10使用docker拉取redis镜像报错read-only file system: unknown
随机推荐
QT -- how to set shadow effect in QWidget
[blackmail virus data recovery] suffix Hydra blackmail virus
Gocv split color channel
使用递归函数求解字符串的逆置问题
Minecraft module service opening
PCL calculates the intersection of three mutually nonparallel planes
一个经典约瑟夫问题的分析与解答
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
判断是否是数独
Count the number of various characters in the string
Qt QTimer类
WSL installation, beautification, network agent and remote development
Linux安装Oracle Database 19c
【Go实战基础】gin 如何自定义和使用一个中间件
Minecraft air Island service
Function ‘ngram‘ is not defined
[staff] time mark and note duration (staff time mark | full note rest | half note rest | quarter note rest | eighth note rest | sixteenth note rest | thirty second note rest)
Minecraft群組服開服
Essay: RGB image color separation (with code)
Webflux responsive programming