当前位置:网站首页>Redis zadd导致的一次线上问题排查和处理
Redis zadd导致的一次线上问题排查和处理
2022-07-02 06:33:00 【程序员柒柒】
背景
最近有用户反馈,主播收了1881数值礼物,头像下数字显示881, 正常来说,应该显示1881的数值。

经过排查,是因为redis zadd在并发情况下导致数据不一致的问题。
问题排查
经过对送礼日志的排查,发现mongodb数据更新正常,但是redis数据异常,查看业务代码后发现了问题。
业务代码如下:

这里的逻辑是先更新 mongodb, 然后对redis数值进行zadd覆盖. 一般情况下没啥问题, 但如果稍微遇到高并发:
- A线程跟B线程同时对mongo进行修改操作, 在数据库层面, B的修改后于A的修改.
- 但在进程中, 线程B先于线程A调用redis修改, 线程A对redis的修改覆盖了B的数值.
- 导致redis中数据维护错误.
问题修复
使用Lua脚本修改redis值,并发情况下不处理覆盖逻辑, 避免旧值覆盖新值,利用mongodb自增保证原子性。 代码如下:

Lua脚本如下:
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
"""
复制代码为啥不使用zincrby?
zincrby虽然保证不会有并发问题,但是如果key失效或者被清理,会导致排行榜从0开始计数,导致数据错误。
边栏推荐
- Leetcode sword finger offer brush questions - day 22
- Qunhui NAS configuring iSCSI storage
- Solid principle: explanation and examples
- Qt——如何在QWidget中设置阴影效果
- C#钉钉开发:取得所有员工通讯录和发送工作通知
- C Baidu map, Gaode map, Google map (GPS) longitude and latitude conversion
- Luogu greedy part of the backpack line segment covers the queue to receive water
- Kubesphere virtualization KSV installation experience
- QT qtimer class
- 整理秒杀系统的面试必备!!!
猜你喜欢
![[blackmail virus data recovery] suffix Hydra blackmail virus](/img/27/f44334cf98229d0f8b33c70a878ca8.jpg)
[blackmail virus data recovery] suffix Hydra blackmail virus

Linux安装Oracle Database 19c RAC

Programmer training, crazy job hunting, overtime ridiculed by colleagues deserve it

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

Honeypot attack and defense drill landing application scheme

MYSQL安装出现问题(The service already exists)

Sqli labs (post type injection)

Luogu greedy part of the backpack line segment covers the queue to receive water

群辉 NAS 配置 iSCSI 存储

Finishing the interview essentials of secsha system!!!
随机推荐
2022/2/14 summary
Qt QTimer类
Minecraft安装资源包
QT drag event
[blackmail virus data recovery] suffix Crylock blackmail virus
Minecraft plug-in service opening
Oracle修改表空间名称以及数据文件
Find the node with the smallest value range in the linked list and move it to the front of the linked list
gocv opencv exit status 3221225785
Tensorflow2 keras 分类模型
Solution of Xiaomi TV's inability to access computer shared files
Web技术发展史
Getting started with k8s: building MySQL with Helm
C language implementation of mine sweeping game
Minecraft模组服开服
使用递归函数求解字符串的逆置问题
C call system sound beep~
Dip1000 runaway
Using recursive functions to solve the inverse problem of strings
C Gaode map obtains the address according to longitude and latitude