redis-排查命中率降低问题

1.命中率降低带来的问题

高并发系统,当命中率低于平常的的运行情况,或者低于70%时,会产生2个影响。

  1. 有大量的请求需要查DB,加大DB的压力;
  2. 影响redis自身的性能

不同的业务场景,阈值不一样,一般低于70%时应当排查问题

2.查看命中率

一般是监控工具感知到redis命中率下降,给开发人员发送预警信息,然后开发人员接入排查

方式1-info获取

使用info命令获取

java 复制代码
INFO stats
java 复制代码
total_connections_received:363810
total_commands_processed:4185370
instantaneous_ops_per_sec:4
instantaneous_write_ops_per_sec:0
instantaneous_read_ops_per_sec:0
instantaneous_other_ops_per_sec:4
total_net_input_bytes:313599223
total_net_output_bytes:6840749159
total_net_repl_input_bytes:0
total_net_repl_output_bytes:158034894
instantaneous_input_kbps:0.16
instantaneous_output_kbps:6.80
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
expire_cycle_cpu_milliseconds:65077
evicted_keys:0
keyspace_hits:240           -- 命中次数
keyspace_misses:15          -- 未命中次数
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:683
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
tracking_total_keys:0
tracking_total_items:0
tracking_total_prefixes:0
unexpected_error_replies:0
total_error_replies:539
instantaneous_error_replies_ops_per_sec:0
total_reads_processed:4599582
total_writes_processed:3669362
io_threaded_reads_processed:0
io_threaded_writes_processed:0
client_query_buffer_limit_disconnections:0
client_output_buffer_limit_disconnections:0
slot_psync_ok:0
slot_psync_err:0
total_commands_received:4185652
commands_received_per_sec:4
evicted_keys_per_sec:0
hits_per_sec:0
misses_per_sec:0
hit_rate_percentage:0.00
cmd_slowlog_count:0
traffic_control_input:0
traffic_control_input_status:0
traffic_control_output:0
traffic_control_output_status:0
stat_avg_rt:37
stat_max_rt:227

keyspace_hits:240 -- 命中次数

keyspace_misses:15 -- 未命中次数

最近1分钟的命中率:获取两个时间点的命中次数

1分钟的命中次数: 第二次的命中次数 - 第一次的命中次数

1分钟的未命中次数:第二次的未命中次数 - 第一次的未命中次数

命中率 = 1分钟的命中次数 / (1分钟的命中次数 + 1分钟的未命中次数)

方式2-阿里云监控工具

3.获取未命中的key

在应用程序中记录未命中的key

java 复制代码
public class RedisCacheUtil {

    public static <T> T hget(String key, String field) throws BizBussinessRuntimeException {
        try {
            Object v = hashOperations.get(key, field);
            if (v == null) {
                log.warn("Redis未命中键:" + key);
            }
            return (T)v;
        } catch (Exception e) {
            log.error("hget(" + key + ") 操作失败!,msg:" + e.getMessage());
            throw new BizBussinessRuntimeException(IErrMsg.ERR_REDIS, "Redis操作失败");
        }
    }

    public static <T> T getObject(String key) throws BizBussinessRuntimeException {
        try {
            Object v = valueOperations.get(key);
            if (v == null) {
                log.warn("Redis未命中键:" + key);
            }
            return (T) v;
        } catch (Exception e) {
            log.error("getObject(" + key + ") 操作失败!,msg:" + e.getMessage());
            throw new BizBussinessRuntimeException(IErrMsg.ERR_REDIS, "Redis操作失败");
        }
    }

    // 其它代码...
}

可以使用DB、ES记录最近的未命中的key,命中率下降时,则分析这个时间段的key的分布,进而分析出原因。

4.可能导致下降的原因

大量的同一个key: 缓存击穿(热点数据过期、key突然变成热点)

大量DB不存在的key: 缓存穿透(人为攻击、代码bug)

大量的多个key: 缓存雪崩

相关推荐
冒泡的肥皂7 分钟前
说下数据存储
数据库·后端·mysql
码河漫步1 小时前
win11安装mysql社区版数据库
数据库·mysql
Wang's Blog1 小时前
MySQL: 存储引擎深度解析:Memory与Federated的特性与应用场景
数据库·mysql
学习中的程序媛~1 小时前
Spring 事务(@Transactional)与异步(@Async / CompletableFuture)结合的陷阱与最佳实践
java·数据库·sql
员大头硬花生2 小时前
九、InnoDB引擎-MVCC
数据库·sql·mysql
一条闲鱼_mytube2 小时前
mvcc 简介
数据库
稻香味秋天2 小时前
单元测试指南
数据库·sqlserver·单元测试
JosieBook2 小时前
【数据库】Apache IoTDB数据库在大数据场景下的时序数据模型与建模方案
数据库·apache·iotdb
全栈胖叔叔-瓜州2 小时前
关于微软最新数据库引擎sqlserver2025 关于向量距离函数调用的问题
数据库·microsoft
全栈小53 小时前
【Rust】从0到1开发和运行Web相关功能,并简单实现数据库连接和查询
数据库·rust