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

相关推荐
两点王爷6 小时前
PostgreSQL 常用 SQL 语句与 GIS 相关函数详解
数据库·后端
两点王爷6 小时前
PostgreSQL 好用又独特的特性与空间函数
数据库
梦帮科技6 小时前
AI 音乐产品的发布工程:验证门、数据发布、回滚与生产运维纪律
数据结构·数据库·架构·node.js·音视频·动态规划·推荐算法
TDengine (老段)8 小时前
TDengine 常见问题 TOP3
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
HanhahnaH8 小时前
Redis单线程和Tair多线程架构设计对比
分布式·缓存
螺蛳粉 螺蛳粉10 小时前
MySQL 分布式集群系列 · 第五篇——全方位对比:NDB、MGR、主从复制、分库分表怎么选?
数据库·分布式·mysql
wdfk_prog13 小时前
ROS教程07:从 ros::start() 顺着源码读懂 Master、XML-RPC 与 Topic 注册发现
运维·缓存·docker·容器·ros
Escalating_xu14 小时前
【内存管理发展史】从 8086 实模式到分页:CPU 如何把虚拟地址变成物理地址
linux·缓存
wdfk_prog14 小时前
用 Git Submodule + Sparse Checkout 管理 RT-Thread:内核、BSP、第三方库与业务代码分层实践
运维·缓存·docker·容器·ros
努力努力再努力wz14 小时前
【Docker入门系列】镜像为什么能复用?一文吃透 Docker Image、Registry、运行时架构与常用命令
缓存·docker·容器