Redis BigKey —— by 久米泷

2020-06-07  本文已影响0人  久米泷

BigKey 的危害

redis 在删除 big key 时,会阻塞当前线程,然后 redis 又是单线程模型,从而引发大量读写超时。

定位 BigKey

  1. 可以通过 redis-cli --bigkeys 查看 redis 下每中数据结构中占用空间最大的 key。
  2. 可以通过 memory usage ${key} 查看 key 的占用空间。

解决方案

  1. redis 从4.0开始,引入了新的命令 unlink,来替代 delunlink 会先将 key 移出 keyspace,再开启异步线程去执行删除操作,所以不会造成阻塞。

unlink 官方释义:

This command is very similar to DEL: it removes the specified keys. Just like DEL a key is ignored if it does not exist. However the command performs the actual memory reclaiming in a different thread, so it is not blocking, while DEL is. This is where the command name comes from: the command just unlinks the keys from the keyspace. The actual removal will happen later asynchronously.

  1. 在 redis 配置文件 redis.conf 中开启 LAZY FREEING 下的指令,以保证 big key 再自动过期被回收时,也是异步完成。
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
slave-lazy-flush yes
上一篇 下一篇

猜你喜欢

热点阅读