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: 缓存雪崩

相关推荐
fen_fen7 小时前
Oracle建表语句示例
数据库·oracle
砚边数影9 小时前
数据可视化入门:Matplotlib 基础语法与折线图绘制
数据库·信息可视化·matplotlib·数据可视化·kingbase·数据库平替用金仓·金仓数据库
orange_tt9 小时前
Djiango配置Celery
数据库·sqlite
云小逸10 小时前
【nmap源码学习】 Nmap网络扫描工具深度解析:从基础参数到核心扫描逻辑
网络·数据库·学习
肉包_51110 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++
霖霖总总10 小时前
[小技巧64]深入解析 MySQL InnoDB 的 Checkpoint 机制:原理、类型与调优
数据库·mysql
ALex_zry10 小时前
Redis Cluster 分布式缓存架构设计与实践
redis·分布式·缓存
此刻你11 小时前
常用的 SQL 语句
数据库·sql·oracle
それども11 小时前
分库分表的事务问题 - 怎么实现事务
java·数据库·mysql
·云扬·12 小时前
MySQL Binlog 配置指南与核心作用解析
数据库·mysql·adb