4.瑞士军刀Redis

 

4

慢查询 找到系统瓶颈命令

  • 生命周期
    • 发送命令-》 排队 -》 执行命令 -》 返回结果
    • 两点说明
      • 慢查询发生在第三阶段
      • 客户端超时不一定慢查询,但慢查询是客户端超时的一个可能因素
  • 两个配置

    • slowlog-max-len 队列长度
      • 先进先出队列
      • 固定长度
      • 保存在内存
    • slowlog-log-slower-than
      • 慢查询阀值(单位:微秒)
      • slowlog-log-slower-than = 0 记录所有命令到慢查询 队列长度
      • slowlog-log-slower-than < 0 不记录任何命令
    • 默认值
      • config get slowlog-max-len = 128
      • config get slowlog-log-slower-than = 10000
    • 修改配置文件 重启
    • 动态配置
      • config set slowlog-max-len 1000
      • config set slowlog-log-slower-than 10000
  • 三个命令

    • slowlog get [n] 获取慢查询队列
    • slowlog len 获取慢查询队列长度
    • slowlog reset 清空慢查询队列
  • 运维经验
    • slowlog-log-slower-than 不要设置过大 默认10ms 通常设置1ms
    • slowlog-max-len 不要设置过小 通常设置1000左右
    • 理解命令生命周期
    • 定期持久化慢查询

pipeline 提高客户端的效率

  • 什么是流水线

    • 1次时间 = 1次网络时间 + 1次命令时间
    • n次时间 = n次网络时间 + n次命令时间 mget mset
    • but not hmset hmset
    • 1次pipline = 1次网络时间 + n次命令时间
    命令N个命令操作1次pipline(n个命令)
    时间 n次网络+n次命令 1次网络 + n次命令
    数据量 1条命令 n条命令
    • Redis的命令时间是微秒级别
    • pipeline每次条数要控制 (网络)

pipline-jedis实现

  • maven依赖 10000 此命令 通过双层for循环进行拆分发送
  • jedis.pipelined()
  • pipeline.hset("hashkey:"+i,"field"+i,"value"+i);
  • pipeline.syncAndReturnAll()
  • 与原生的M操作相比pipeline非原子操作,m是原子操作

  • 使用建议

    • 注意每次pipeline携带数据量
    • pipeline每次只能作用在一个redis节点上
    • M操作和pipeline的区别

发布订阅

角色

  • 发布者 publisher
  • 订阅者 subcriber
  • 频道 channel

模型 无法消息堆积

  • 发布者 --发布消息 redis-cli
    • redis server 频道
      • 订阅者 redis-cli
      • 订阅者 redis-cli
      • 订阅者 redis-cli

API

  • publish
    • publish channel message
      • publish sohu:tv "hello world"
      • 3 # 订阅者数量
  • subscribe 订阅
    • subscribe [channel] #一个或多个
      • subscribe sohu:tv
  • unsubscribe
    • unsubscribe [channel] #一个或多个
      • unsubscribe sohu:tv
  • 其他
    • psubscribe [pattern..] 订阅模式
    • punsubscribe [pattern..] 退订指定模式
    • pubsub channels # 列出至少有一个订阅者的频道
    • pubsub numsub [channel] # 列出给定频道的订阅者数量

发布订阅和消息队列

  • 发布订阅所有的客户端都能收到
  • 消息订阅对于消息有那个客户端抢到了消息 其他客户端就收不到这个消息了

总结

  • 发布订阅模式中的角色
  • 重要的API

Bitmap 提供节省内存的方案 位图

位图

  • setbit key offset value # 给位图指定索引设置值
  • getbit key offset
  • bitcount key [start end] # 获取位图指定范围位值为1的个数
  • bitop op destkey key [key] 做多个bitmap的and or not xor操作并将结果保存在destkey中
  • bitpos key tagetBit [start] [end]
  • 计算位图指定范围第一个偏移量对应的值等于targetBit位置

相关命令

独立用户统计

  • 使用set Bitmap

1亿用户 5千万独立

数据类型每个userid占用空间需要存储的用户量全部存储量
set 32位(建设userid用的是整形,实际很多网站用的是长整形) 50,000,000 32位*50,000,00=200MB
bitmap 1位 100,000,000 1位*100,000,000=12.5MB

对比

 一天一个月一年
set 200M 6G 72G
Bitmap 12.5M 375M 4.5G

10万独立用户

数据类型每个userid占用空间需要存储的用户量全部存储量
set 32位(建设userid用的是整形,实际很多网站用的是长整形) 1,000,000 32位*1,000,00=4MB
bitmap 1位 100,000,000 1位*100,000,000=12.5MB

使用经验

  1. type=string 最大512MB
  2. 注意setbit时的偏移量,可能有较大耗时
  3. 位图不是绝对好

HyperLogLog 使用极小的内存实现独立用户的统计

  • 新型数据结构
    • 本质还是字符串
  • 三个命令
    • pfadd key element [element..] : 向HyperLogLog添加元素
    • pfcount key [key..] 计算HyperLogLog的独立总数
    • pfmerge destkey sourcekey [sourcekey..] 合并多个hyperloglog
    • demo
      • pfadd 2019_03_06:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-4"
      • pfcount 019_03_06:unique:ids 4
      • pfadd 2019_03_06:unique:ids "uuid-1" "uuid-2" "uuid-3" "uuid-90"
      • pfcount 019_03_06:unique:ids 5
  • 内存消耗 百万独立用户 | | 内存消耗| |-|-| |1天|15KB| |1个月|450KB| |1年|15KB*365=5MB|
  • 使用经验
    • 是否能容忍错误 错误率:0.81%
    • 是否需要单挑内存

GEO 地理信息

GEO是什么

GEO(地理信息定位):存储经纬度,计算两地距离,范围计算

5个城市经纬度

城市经度纬度简称
北京 116.28 39.55 beijing
天津 117.12 39.08 tianjin
石家庄 114.29 38.01 shijiazhuang
唐山 118.01 39.38 tangshan
保定 115.29 38.51 baoding

相关命令

  • geoadd key longitude latitude member

    • 命令行代码

      geoadd cities:locations 116.28 39.55 beijing
      geoadd cities:locations 117.12 39.08 tianjin
      geoadd cities:locations 114.29 38.01 shijiazhuang
      geoadd cities:locations 118.01 39.38 tangshan
      geoadd cities:locations 115.29 38.51 baoding

  • geodist key member1 member2 [unit]
    • 获取两个地理位置的距离
    • unit m km mi(英里)ft(尺)
  • georadius key logitude latitude radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
  • georradiusbymember key member radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]

获取指定位置范围内的地址位置信息集合

  • withcoord 返回结果中包含经纬度
  • withdist 返回结果中包含距离中心节点位置
  • withhash 返回结果中包含geohash
  • COUNT count 指定返回结果的数量
  • asc|desc 返回结果中按照距离中心节点的距离做升序或降序
  • store key 将返回结果的地理位置信息保存到指定键
  • storedist key 将返回结果距离中心节点的距离保存到指定key

georadiusbymember cities:locations beijing 150km 距离北京150km范围内的城市

相关说明

  • since 3.2+
  • type geoKey = zset
  • 没有删除API: zrem key member

 

 

posted @ 2020-01-12 18:14  Richie`  阅读(234)  评论(0编辑  收藏  举报