惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 茁壮的小草

join查询中索引建议 tomcat连接池的“bug” 连接池介绍 JDBC介绍 Hadoop完全分布式搭建 Apache Hadoop YARN 使用 kubebuilder 创建并部署 k8s-operator 单链表反转 单链表操作 如何在Kubernetes 里添加自定义的 API 对象(一) 搭建golang开发环境(1.14之后版本) 【源码】Redis命令处理过程 K8S配置存活、就绪和启动探测器 【源码】Redis Server启动过程 K8S中的Objects MAC 安装MySQL-python 公司里使用gitlab管理项目 vscode配置django开发环境最佳实战(mac) virtualenv最佳实战(windows)
【源码】Redis exists命令bug分析
茁壮的小草 · 2022-02-08 · via 博客园 - 茁壮的小草

本文基于社区版Redis 4.0.8

1、复现条件

  • 版本:社区版Redis 4.0.10以下版本
  • 使用场景:开启读写分离的主从架构或者集群架构(master只负责写流量,slave负责读流量)

案例:

# 写入一条带过期时间10s的key
10.90.73.147:12345> set luxiu1 1 ex 10
OK
10.90.73.147:12345> get luxiu1
"1"
10.90.73.147:12345> exists luxiu1
(integer) 1
......
#等待10s,待key过期
......

10.90.73.147:12345> ttl luxiu1 
(integer) -2 #确定key已经过期了
10.90.73.147:12345> get luxiu1
(nil)  #没有问题,该key不存在了
10.90.73.147:12345> exists luxiu1
(integer) 1 #还能查到
10.90.73.147:12345> exists luxiu1
(integer) 1 #还能查到

2、源码分析

在分析该问题前,需要了解Redis在读写分离模式下读到过期数据的问题:

Redis过期key的删除策略采用惰性删除和定时删除:

惰性删除:主节点每次处理读取命令时,都会检查键是否超时,如果超时则执行del命令删除键对象,之后del命令也会异步发送给从节点。需要注意的是为了保证复制的一致性,从节点自身永远不会主动删除超时数据;

定时删除:Redis主节点在内部定时任务会循环采样一定数量的键,当发现采样的键过期时执行del命令,之后再同步给从节点;

如果此时数据大量过期,主节点采样速度跟不上过期速度且主节点没有读取过期键的操作,那么从节点将无法收到del命令。这时在从节点上可以读取到已经超时的数据。Redis在3.2版本解决了这个问题,在从节点上读取数据之前也会检查键的过期时间来决定是否返回数据。但是,4.0.10版本以下的exists命令实现方式有问题,导致该命令还是查询到过期数据问题。

下面是4.0.10以下版本exists命令实现源码:

问题就在于expireIfNeeded这个函数,它的功能就是惰性删除,判断如果key过期了就进行del,我们是读写分离架构,slave不进行del,如下代码:

直接返回1,并不进行到del操作。

所以exists查询到过期key一直存在。

3、问题解决

在社区版4.0.11以上版本已经修复了该bug:

lookupKeyRead函数调用lookupKeyReadWithFlags(db,key,LOOKUP_NONE)

lookupKeyReadWithFlags函数逻辑如下:

最后,可以升级到4.0.12版本解决该问题。