当前位置:网站首页>Redis最佳实践
Redis最佳实践
2022-07-25 08:06:00 【行万里路,读万卷书】
1. Redis键值设计
1.1 优雅的key结构
Redis的Key虽然可以自定义,但最好遵循下面的几个最佳实践约定:
- 遵循基本格式:[业务名称]:[数据名]:[id]
- 长度不超过44字节
- 不包含特殊字符
例如:我们的登录业务,保存用户信息,其key是这样的:
优点:
- 可读性强
- 避免key冲突
- 方便管理
- 更节省空间:key是string类型,底层编码包含int、embstr和raw三种。embstr在小于44字节时使用,采用连续内存空间,内存占用更小
1.2 什么是BigKey
BigKey通常以Key的大小和Key中成员的数量来综合判定,例如:
- Key本身的数据量过大:一个String类型的Key,它的值为5 MB。
- Key中的成员数过多:一个ZSET类型的Key,它的成员数量为10,000个。
- Key中成员的数据量过大:一个Hash类型的Key,它的成员数量虽然只有1,000个但这些成员的Value(值)总大小为100 MB。
推荐值:
- 单个key的value小于10KB
- 对于集合类型的key,建议元素数量小于1000
1.2.1 BigKey的危害
- 网络阻塞
对BigKey执行读请求时,少量的QPS就可能导致带宽使用率被占满,导致Redis实例,乃至所在物理机变慢 - 数据倾斜
BigKey所在的Redis实例内存使用率远超其他实例,无法使数据分片的内存资源达到均衡 - Redis阻塞
对元素较多的hash、list、zset等做运算会耗时较久,使主线程被阻塞 - CPU压力
对BigKey的数据序列化和反序列化会导致CPU的使用率飙升,影响Redis实例和本机其它应用
1.2.2 如何发现BigKey
- redis-cli --bigkeys
利用redis-cli提供的–bigkeys参数,可以遍历分析所有key,并返回Key的整体统计信息与每个数据的Top1的big key - scan扫描
自己编程,利用scan扫描Redis中的所有key,利用strlen、hlen等命令判断key的长度(此处不建议使用MEMORY USAGE) - 第三方工具
利用第三方工具,如 Redis-Rdb-Tools 分析RDB快照文件,全面分析内存使用情况 - 网络监控
自定义工具,监控进出Redis的网络数据,超出预警值时主动告警
1.2.3 如何删除BigKey
BigKey内存占用较多,即便时删除这样的key也需要耗费很长时间,导致Redis主线程阻塞,引发一系列问题。
- redis 3.0 及以下版本
如果是集合类型,则遍历BigKey的元素,先逐个删除子元素,最后删除BigKey - Redis 4.0以后
Redis在4.0后提供了异步删除的命令:unlink
边栏推荐
- 475-82(230、43、78、79、213、198、1143)
- Enq: HW - fault analysis caused by content waiting
- Google AI can't understand the comments of netizens, and the wrong meaning will be as high as 30%. Netizens: you don't understand my stem
- Native form submission data
- Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
- 如何仅用递归函数和栈操作逆序一个栈
- Summer Challenge harmonyos - slider slider for custom components
- Programmers can't play at the age of 35. Is it a fact or a misunderstanding?
- Vs2019 C MFC installation
- [paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
猜你喜欢
P1086 [NOIP2004 普及组第二题] 花生采摘

batchnorm 和layernorm的区别

Teach you to use cann to convert photos into cartoon style

The 17th revitalization cup computer programmer (cloud computing platform operation and development) finals

Introduction and installation of mongodb

Open source, innovators win, 2022 "science and innovation China" open source innovation list selection is fully open!

第十七届振兴杯计算机程序设计员(云计算平台运维与开发)决赛

efcore在Saas系统下多租户零脚本分表分库读写分离解决方案
Mysql-5.7 installation

CAS operation
随机推荐
Problems easily ignored by POM
How to do a good job in safety development?
A queue of two stacks
机器学习理论及案例分析(part2)--回归
How does MTK change the boot logo?
Weblux default IO threads
redis客户端工具redis-insight推荐
What products and funds should novices invest in first?
Practical operation: elegant downtime under large-scale micro service architecture
Redis core principles
While (~scanf ("%d", & n)) is equivalent to while (scanf ("%d", & n)! =eof)
batchnorm 和layernorm的区别
Using one stack to sort another stack
Dijkstra序列(暑假每日一题 5)
P1046 [noip2005 popularity group t1] Taotao picking apples
【着色器实现Shadow投影效果_Shader效果第八篇】
Didi eta (estimate the travel time)
Learn when playing No 6 | the magic of document library lies in
Uiautomator2 common commands
Advanced C language (XII) - dynamic memory management