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

相关推荐
semantist@语校12 分钟前
第五十八篇|从城市节律到制度密度:近畿日本语学院的数据建模与关西语校结构工程
大数据·服务器·数据库·人工智能·百度·ai·知识图谱
小宇的天下38 分钟前
Calibre 工具的几何处理基础(11-1)
数据库·oracle
talenteddriver1 小时前
mysql: MySQL索引基础概念
数据库·mysql
king_harry1 小时前
PostgreSQL WAL 原理剖析、日志堆积治理与流复制监控
数据库·postgresql·wal·流复制
默默前行的虫虫1 小时前
nicegui网页多用户数据隔离总结
数据库·sql
特立独行的猫a2 小时前
PostgreSQL客户端工具介绍:从性能测试到跨平台管理
数据库·docker·postgresql·客户端·pgadmin4
微爱帮监所写信寄信2 小时前
微爱帮监狱写信寄信小程序:MySQL核心日志与备份恢复安全架构
数据库·mysql·小程序·邮局·监狱寄信·挂号信·邮政
isNotNullX2 小时前
数据迁移怎么做?有什么好用的数据库迁移工具推荐吗?
数据库·数字化·数据迁移·企业管理
云老大TG:@yunlaoda3602 小时前
华为云国际站代理商DAS的跨境合规适配的应用场景有哪些?
网络·数据库·华为云
3824278273 小时前
python3网络爬虫开发实战 第二版:绑定回调
开发语言·数据库·python