当前位置:网站首页>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开始计数,导致数据错误。
边栏推荐
- 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
- [flask] ORM one-to-one relationship
- 寻找链表中值域最小的节点并移到链表的最前面
- Sentinel easy to use
- Openshift deployment application
- Qunhui NAS configuring iSCSI storage
- Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
- cmd窗口中中文呈现乱码解决方法
- Leetcode sword finger offer brush questions - day 22
- 2022/2/14 summary
猜你喜欢

How to realize asynchronous programming in a synchronous way?

Openshift build image

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

Sentinel reports failed to fetch metric connection timeout and connection rejection
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

Openshift container platform community okd 4.10.0 deployment

Function ‘ngram‘ is not defined

Minecraft air Island service

Solid principle: explanation and examples

Minecraft安装资源包
随机推荐
Mirror protocol of synthetic asset track
CSDN Q & A_ Evaluation
oracle删除表空间及用户
判断是否是数独
Kubernetes deploys Loki logging system
Service de groupe minecraft
Using recursive functions to solve the inverse problem of strings
Detailed explanation of NIN network
Right click menu of QT
OpenShift 部署应用
随笔:RGB图像颜色分离(附代码)
oracle修改数据库字符集
Qt——如何在QWidget中设置阴影效果
PCL calculates the intersection of three mutually nonparallel planes
Chrome debugging
Linux binary installation Oracle database 19C
Minecraft空岛服开服
将一串数字顺序后移
Image transformation, transpose
2022/2/14 summary