当前位置:网站首页>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 .
边栏推荐
- libusb的使用
- 使用IBM MQ远程连接时报错AMQ 4043解决思路
- Find the node with the smallest value range in the linked list and move it to the front of the linked list
- C# 调用系统声音 嘀~
- QT -- how to set shadow effect in QWidget
- Dix ans d'expérience dans le développement de programmeurs vous disent quelles compétences de base vous manquez encore?
- PCL calculates the intersection of three mutually nonparallel planes
- [blackmail virus data recovery] suffix Rook3 blackmail virus
- C# 百度地图,高德地图,Google地图(GPS) 经纬度转换
- C4D quick start tutorial - C4d mapping
猜你喜欢
整理秒杀系统的面试必备!!!
Flink-使用流批一体API统计单词数量
Kubesphere virtualization KSV installation experience
机器学习之数据类型案例——基于朴素贝叶斯法,用数据辩男女
C nail development: obtain all employee address books and send work notices
将一串数字顺序后移
Avoid breaking changes caused by modifying constructor input parameters
查看was发布的应用程序的端口
Multi version concurrency control mvcc of MySQL
Solution of Xiaomi TV's inability to access computer shared files
随机推荐
「Redis源码系列」关于源码阅读的学习与思考
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
汉诺塔问题的求解与分析
Installing Oracle database 19C for Linux
Minecraft install resource pack
What is the future value of fluorite mine of karaqin Xinbao Mining Co., Ltd. under zhongang mining?
Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method
Cartoon rendering - average normal stroke
Gocv boundary fill
Redis sorted set data type API and application scenario analysis
[blackmail virus data recovery] suffix Crylock blackmail virus
【Go实战基础】gin 高效神器,如何将参数绑定到结构体
京东面试官问:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
随笔:RGB图像颜色分离(附代码)
聊聊消息队列高性能的秘密——零拷贝技术
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)
CSDN Q & A_ Evaluation
Oracle 相关统计
Count the number of various characters in the string
统计字符串中各类字符的个数