当前位置:网站首页>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 .
边栏推荐
- Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
- Connect function and disconnect function of QT
- commands out of sync. did you run multiple statements at once
- Linux binary installation Oracle database 19C
- C4D quick start tutorial - C4d mapping
- C# 调用系统声音 嘀~
- 判断是否是数独
- Win10 uses docker to pull the redis image and reports an error read only file system: unknown
- 远程连接IBM MQ报错AMQ4036解决方法
- Matplotlib剑客行——容纳百川的艺术家教程
猜你喜欢

Kubesphere virtualization KSV installation experience

C language - Blue Bridge Cup - 7 segment code

Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`

Data type case of machine learning -- using data to distinguish men and women based on Naive Bayesian method

Finishing the interview essentials of secsha system!!!
![[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)](/img/ae/1ecb352c51a101f237f244da5a2ef7.jpg)
[staff] common symbols of staff (Hualian clef | treble clef | bass clef | rest | bar line)

ORA-12514问题解决方法

Minecraft install resource pack

Multi version concurrency control mvcc of MySQL

Qunhui NAS configuring iSCSI storage
随机推荐
Using recursive functions to solve the inverse problem of strings
使用递归函数求解字符串的逆置问题
cmd窗口中中文呈现乱码解决方法
Mysql安装时mysqld.exe报`应用程序无法正常启动(0xc000007b)`
Select sort and insert sort
AMQ6126问题解决思路
Kubernetes deploys Loki logging system
Matplotlib swordsman Tour - an artist tutorial to accommodate all rivers
C4D quick start tutorial - C4d mapping
How to realize asynchronous programming in a synchronous way?
十年开发经验的程序员告诉你,你还缺少哪些核心竞争力?
【Go实战基础】如何安装和使用 gin
Kubesphere virtualization KSV installation experience
Connect function and disconnect function of QT
【Go实战基础】gin 如何获取 GET 和 POST 的请求参数
Openshift deployment application
Installing Oracle database 19C for Linux
选择排序和插入排序
NPOI 导出Word 字号对应
Qt的connect函数和disconnect函数