Redis 开发规范 (From阿里)学习总结

[size=large][b]一、键值设计[/b][/size]
[color=red][size=medium][b]1. key名设计[/b][/size][/color]
[color=red][b](1)【建议】: 可读性和可管理性[/b][/color]
[color=blue][b]以业务名(或数据库名)为前缀(防止key冲突),用冒号分隔,比如业务名:表名:id[/b][/color]

ugc:video:1


[b](2)【建议】:简洁性[/b]
保证语义的前提下,[b]控制key的长度,当key较多时,内存占用也不容忽视[/b],例如:

user:{uid}:friends:messages:{mid} 简化成u:{uid}:fr:m:{mid}


[color=red][b](3)【强制】:不要包含特殊字符[/b][/color]
反例:包含空格、换行、单双引号以及其他转义字符

[size=medium][b]2. value设计[/b][/size]
[b](1)【强制】:拒绝bigkey(防止网卡流量、慢查询)[/b]
string类型控制在10KB以内,hash、list、set、zset元素个数不要超过5000。

反例:一个包含200万个元素的list。

[color=red][b]非字符串的bigkey,不要使用del删除,使用hscan、sscan、zscan方式渐进式删除,同时要注意防止bigkey过期时间自动删除问题(例如一个200万的zset设置1小时过期,会触发del操作,造成阻塞,而且该操作不会不出现在慢查询中(latency可查)),查找方法和删除方法[/b][/color]

[b](2)【推荐】:选择适合的数据类型。[/b]
例如:实体类型(要合理控制和使用数据结构内存编码优化配置,例如ziplist,但也要注意节省内存和性能之间的平衡)

反例:
set user:1:name tom
set user:1:age 19
set user:1:favor football

[b][color=red]正例:[/color][/b]
hmset user:1 name tom age 19 favor football


[color=red][b]3.【推荐】:控制key的生命周期,redis不是垃圾桶。[/b][/color]
[b][color=blue]建议使用expire设置过期时间(条件允许可以打散过期时间,防止集中过期),不过期的数据重点关注idletime。[/color][/b]

[color=red][size=large][b]二、命令使用[/b][/size][/color]
[b]1.【推荐】 O(N)命令关注N的数量[/b]
例如hgetall、lrange、smembers、zrange、sinter等并非不能使用,但是需要明确N的值。有遍历的需求可以使用hscan、sscan、zscan代替。

[b]2.【推荐】:禁用命令[/b]
[color=blue][b]禁止线上使用keys、flushall、flushdb等,通过redis的rename机制禁掉命令,或者使用scan的方式渐进式处理。[/b][/color]

[b]3.【推荐】合理使用select[/b]
redis的多数据库较弱,使用数字进行区分,很多客户端支持较差,同时多业务用多数据库实际还是单线程处理,会有干扰。

[color=red][b]4.【推荐】使用批量操作提高效率[/b][/color]

原生命令:例如mget、mset。
非原生命令:可以使用pipeline提高效率。


但要注意控制一次批量操作的元素个数(例如500以内,实际也和元素字节数有关)。

注意两者不同:


1. 原生是原子操作,pipeline是非原子操作。
2. pipeline可以打包不同的命令,原生做不到
3. pipeline需要客户端和服务端同时支持。


[color=blue][b]5.【建议】Redis事务功能较弱,不建议过多使用[/b][/color]
Redis的事务功能较弱(不支持回滚),而且集群版本(自研和官方)要求一次事务操作的key必须在一个slot上(可以使用hashtag功能解决)

[b]6.【建议】Redis集群版本在使用Lua上有特殊要求:[/b]
1.所有key都应该由 KEYS 数组来传递,redis.call/pcall 里面调用的redis命令,key的位置,必须是KEYS array, 否则直接返回error,"-ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS array"
2.所有key,必须在1个slot上,否则直接返回error, "-ERR eval/evalsha command keys must in same slot"
[b]7.【建议】必要情况下使用monitor命令时,要注意不要长时间使用。[/b]

[size=large][b]三、客户端使用[/b][/size]
[b]1.【推荐】[/b]
[color=blue][b]避免多个应用使用一个Redis实例[/b][/color]

正例:不相干的业务拆分,公共数据做服务化。

[b]2.【推荐】[/b]
使用带有连接池的数据库,可以有效控制连接,同时提高效率,标准使用方式:

执行命令如下:
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//具体的命令
jedis.executeCommand()
} catch (Exception e) {
logger.error("op key {} error: " + e.getMessage(), key, e);
} finally {
//注意这里不是关闭连接,在JedisPool模式下,Jedis会被归还给资源池。
if (jedis != null)
jedis.close();
}


3.【建议】
[b]高并发下建议客户端添加熔断功能(例如netflix hystrix)[/b]

4.【推荐】
[b]设置合理的密码,如有必要可以使用SSL加密访问(阿里云Redis支持)[/b]

[size=large][b]四.删除操作[/b][/size]

[b]1. Hash删除: hscan + hdel[/b]
public void delBigHash(String host, int port, String password, String bigHashKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Entry<String, String>> scanResult = jedis.hscan(bigHashKey, cursor, scanParams);
List<Entry<String, String>> entryList = scanResult.getResult();
if (entryList != null && !entryList.isEmpty()) {
for (Entry<String, String> entry : entryList) {
jedis.hdel(bigHashKey, entry.getKey());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));

//删除bigkey
jedis.del(bigHashKey);
}

[b]2. List删除: ltrim[/b]
public void delBigList(String host, int port, String password, String bigListKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
long llen = jedis.llen(bigListKey);
int counter = 0;
int left = 100;
while (counter < llen) {
//每次从左侧截掉100个
jedis.ltrim(bigListKey, left, llen);
counter += left;
}
//最终删除key
jedis.del(bigListKey);
}

[b]3. Set删除: sscan + srem[/b]
public void delBigSet(String host, int port, String password, String bigSetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<String> scanResult = jedis.sscan(bigSetKey, cursor, scanParams);
List<String> memberList = scanResult.getResult();
if (memberList != null && !memberList.isEmpty()) {
for (String member : memberList) {
jedis.srem(bigSetKey, member);
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));

//删除bigkey
jedis.del(bigSetKey);
}

[b]4. SortedSet删除: zscan + zrem[/b]
public void delBigZset(String host, int port, String password, String bigZsetKey) {
Jedis jedis = new Jedis(host, port);
if (password != null && !"".equals(password)) {
jedis.auth(password);
}
ScanParams scanParams = new ScanParams().count(100);
String cursor = "0";
do {
ScanResult<Tuple> scanResult = jedis.zscan(bigZsetKey, cursor, scanParams);
List<Tuple> tupleList = scanResult.getResult();
if (tupleList != null && !tupleList.isEmpty()) {
for (Tuple tuple : tupleList) {
jedis.zrem(bigZsetKey, tuple.getElement());
}
}
cursor = scanResult.getStringCursor();
} while (!"0".equals(cursor));

//删除bigkey
jedis.del(bigZsetKey);
}


转自:[url]https://yq.aliyun.com/articles/531067[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值