【Redis】Redis 字典中的rehash

简易流程:

Redis rehash

Redis 渐进式rehash

看完这两篇,对Redis渐进式rehash有一个疑问:如果Redis 里的 渐进式rehash 是根据访问老字典的数据来做数据迁移到新字典,那么老字典的某些很少访问到的数据应该怎么迁移到新数据?

源码级别详细:

美团:Redis 如何 rehash

huangz1900(Redis设计和实现作者)源码解惑

/* Our hash table implementation performs rehashing incrementally while
 * we write/read from the hash table. Still if the server is idle, the hash
 * table will use two tables for a long time. So we try to use 1 millisecond
 * of CPU time at every call of this function to perform some rehahsing.
 *
 * 虽然服务器在对数据库执行读取/写入命令时会对数据库进行渐进式 rehash ,
 * 但如果服务器长期没有执行命令的话,数据库字典的 rehash 就可能一直没办法完成,
 * 为了防止出现这种情况,我们需要对数据库执行主动 rehash 。
 *
 * The function returns 1 if some rehashing was performed, otherwise 0
 * is returned.
 *
 * 函数在执行了主动 rehash 时返回 1 ,否则返回 0 。
 */
    int incrementallyRehash(int dbid) {
 
        /* Keys dictionary */
        if (dictIsRehashing(server.db[dbid].dict)) {
            dictRehashMilliseconds(server.db[dbid].dict,1);
            return 1; /* already used our millisecond for this loop... */
        }
 
        /* Expires */
        if (dictIsRehashing(server.db[dbid].expires)) {
            dictRehashMilliseconds(server.db[dbid].expires,1);
            return 1; /* already used our millisecond for this loop... */
        }
 
        return 0;
    }

除了在渐进式rehash中,通过改查的方式不断地将命中的老字典元素迁移到新字典之外,redis还有一种主动rehash(active rehashing)的方式,主动去做这种迁移。并且该方式是可以通过配置文件来配置的,相关conf如下表所示:(大致意思是:默认1秒做十次,如果你有严格的延迟要求,你可以把主动rehash关掉)

Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
order to help rehashing the main Redis hash table (the one mapping top-level
keys to values). The hash table implementation Redis uses (see dict.c)
performs a lazy rehashing: the more operation you run into a hash table
that is rehashing, the more rehashing "steps" are performed, so if the
server is idle the rehashing is never complete and some more memory is used
by the hash table.

The default is to use this millisecond 10 times every second in order to
actively rehash the main dictionaries, freeing memory when possible.

If unsure:
use "activerehashing no" if you have hard latency requirements and it is
not a good thing in your environment that Redis can reply from time to time
to queries with 2 milliseconds delay.

use "activerehashing yes" if you don't have such hard requirements but
want to free memory asap when possible.
activerehashing yes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值