当前位置:网站首页>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 .
边栏推荐
- Essay: RGB image color separation (with code)
- [flask] ORM one-to-one relationship
- C Gaode map obtains the address according to longitude and latitude
- Select sort and insert sort
- 「Redis源码系列」关于源码阅读的学习与思考
- Redis zadd导致的一次线上问题排查和处理
- Move a string of numbers backward in sequence
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
猜你喜欢

What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?

【Go实战基础】如何安装和使用 gin

以字节跳动内部 Data Catalog 架构升级为例聊业务系统的性能优化

Openshift deployment application

查看was发布的应用程序的端口

Kubernetes deploys Loki logging system

Minecraft安装资源包

Flink-使用流批一体API统计单词数量

Minecraft模组服开服

机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
随机推荐
图像变换,转置
一、Qt的核心类QObject
C nail development: obtain all employee address books and send work notices
统计字符串中各类字符的个数
[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
小米电视不能访问电脑共享文件的解决方案
cmd窗口中中文呈现乱码解决方法
【Go实战基础】如何安装和使用 gin
Gocv split color channel
使用递归函数求解字符串的逆置问题
[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)
Find the node with the smallest value range in the linked list and move it to the front of the linked list
Webflux responsive programming
「面试高频题」难度大 1.5/5,经典「前缀和 + 二分」运用题
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
2022/2/14 summary
Minecraft空岛服开服
Openshift build image
AMQ6126问题解决思路
oracle删除表空间及用户