面试突击 002 | Redis 是如何处理已过期元素的?
1 面試題
Redis 如何處理已過期的元素?
2?涉及知識點(diǎn)
此問題涉及以下知識點(diǎn):
過期刪除策略有哪些?
這些過期策略有哪些優(yōu)缺點(diǎn)?
Redis 使用的是什么過期策略?
Redis 是如何優(yōu)化和執(zhí)行過期策略的?
3 答案
常見的過期策略:
定時(shí)刪除
惰性刪除
定期刪除
1)定時(shí)刪除
在設(shè)置鍵值過期時(shí)間時(shí),創(chuàng)建一個(gè)定時(shí)事件,當(dāng)過期時(shí)間到達(dá)時(shí),由事件處理器自動執(zhí)行鍵的刪除操作。
① 優(yōu)點(diǎn)
保證內(nèi)存可以被盡快的釋放
② 缺點(diǎn)
在 Redis 高負(fù)載的情況下或有大量過期鍵需要同時(shí)處理時(shí),會造成 Redis 服務(wù)器卡頓,影響主業(yè)務(wù)執(zhí)行。
2)惰性刪除
不主動刪除過期鍵,每次從數(shù)據(jù)庫獲取鍵值時(shí)判斷是否過期,如果過期則刪除鍵值,并返回 null。
① 優(yōu)點(diǎn)
因?yàn)槊看卧L問時(shí),才會判斷過期鍵,所以此策略只會使用很少的系統(tǒng)資源。
② 缺點(diǎn)
系統(tǒng)占用空間刪除不及時(shí),導(dǎo)致空間利用率降低,造成了一定的空間浪費(fèi)。
③?Redis 源碼解析
惰性刪除的源碼位于 src/db.c 文件的 expireIfNeeded 方法中,源碼如下:
int expireIfNeeded(redisDb *db, robj *key) {// 判斷鍵是否過期if (!keyIsExpired(db,key)) return 0;if (server.masterhost != NULL) return 1;/* 刪除過期鍵 */// 增加過期鍵個(gè)數(shù)server.stat_expiredkeys++;// 傳播鍵過期的消息propagateExpire(db,key,server.lazyfree_lazy_expire);notifyKeyspaceEvent(NOTIFY_EXPIRED,"expired",key,db->id);// server.lazyfree_lazy_expire 為 1 表示異步刪除(懶空間釋放),反之同步刪除return server.lazyfree_lazy_expire ? dbAsyncDelete(db,key) :dbSyncDelete(db,key); } // 判斷鍵是否過期 int keyIsExpired(redisDb *db, robj *key) {mstime_t when = getExpire(db,key);if (when < 0) return 0; /* No expire for this key *//* Don't expire anything while loading. It will be done later. */if (server.loading) return 0;mstime_t now = server.lua_caller ? server.lua_time_start : mstime();return now > when; } // 獲取鍵的過期時(shí)間 long long getExpire(redisDb *db, robj *key) {dictEntry *de;/* No expire? return ASAP */if (dictSize(db->expires) == 0 ||(de = dictFind(db->expires,key->ptr)) == NULL) return -1;/* The entry was found in the expire dict, this means it should also* be present in the main dict (safety check). */serverAssertWithInfo(NULL,key,dictFind(db->dict,key->ptr) != NULL);return dictGetSignedIntegerVal(de); }所有對數(shù)據(jù)庫的讀寫命令在執(zhí)行之前,都會調(diào)用 expireIfNeeded 方法判斷鍵值是否過期,過期則會從數(shù)據(jù)庫中刪除,反之則不做任何處理。
3)定期刪除
每隔一段時(shí)間檢查一次數(shù)據(jù)庫,隨機(jī)刪除一些過期鍵。
Redis 默認(rèn)每秒進(jìn)行 10 次過期掃描,此配置可通過 Redis 的配置文件 redis.conf 進(jìn)行配置,配置鍵為 hz 它的默認(rèn)值是 hz 10 。
需要注意的是:Redis 每次掃描并不是遍歷過期字典中的所有鍵,而是采用隨機(jī)抽取判斷并刪除過期鍵的形式執(zhí)行的。
定期刪除的執(zhí)行流程:
① 優(yōu)點(diǎn)
通過限制刪除操作的時(shí)長和頻率,來減少刪除操作對 Redis 主業(yè)務(wù)的影響,同時(shí)也能刪除一部分過期的數(shù)據(jù)減少了過期鍵對空間的無效占用。
②?缺點(diǎn)
內(nèi)存清理方面沒有定時(shí)刪除效果好,同時(shí)沒有惰性刪除使用的系統(tǒng)資源少。
③?Redis 源碼解析
定期刪除的核心源碼在 src/expire.c 文件下的 activeExpireCycle 方法中,源碼如下:
void activeExpireCycle(int type) {static unsigned int current_db = 0; /* 上次定期刪除遍歷到的數(shù)據(jù)庫ID */static int timelimit_exit = 0; /* Time limit hit in previous call? */static long long last_fast_cycle = 0; /* 上一次執(zhí)行快速定期刪除的時(shí)間點(diǎn) */int j, iteration = 0;int dbs_per_call = CRON_DBS_PER_CALL; // 每次定期刪除,遍歷的數(shù)據(jù)庫的數(shù)量long long start = ustime(), timelimit, elapsed;if (clientsArePaused()) return;if (type == ACTIVE_EXPIRE_CYCLE_FAST) {if (!timelimit_exit) return;// ACTIVE_EXPIRE_CYCLE_FAST_DURATION 是快速定期刪除的執(zhí)行時(shí)長if (start < last_fast_cycle + ACTIVE_EXPIRE_CYCLE_FAST_DURATION*2) return;last_fast_cycle = start;}if (dbs_per_call > server.dbnum || timelimit_exit)dbs_per_call = server.dbnum;// 慢速定期刪除的執(zhí)行時(shí)長timelimit = 1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/server.hz/100;timelimit_exit = 0;if (timelimit <= 0) timelimit = 1;if (type == ACTIVE_EXPIRE_CYCLE_FAST)timelimit = ACTIVE_EXPIRE_CYCLE_FAST_DURATION; /* 刪除操作的執(zhí)行時(shí)長 */long total_sampled = 0;long total_expired = 0;for (j = 0; j < dbs_per_call && timelimit_exit == 0; j++) {int expired;redisDb *db = server.db+(current_db % server.dbnum);current_db++;do {// .......expired = 0;ttl_sum = 0;ttl_samples = 0;// 每個(gè)數(shù)據(jù)庫中檢查的鍵的數(shù)量if (num > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP)num = ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP;// 從數(shù)據(jù)庫中隨機(jī)選取 num 個(gè)鍵進(jìn)行檢查while (num--) {dictEntry *de;long long ttl;if ((de = dictGetRandomKey(db->expires)) == NULL) break;ttl = dictGetSignedInteger// 過期檢查,并對過期鍵進(jìn)行刪除if (activeExpireCycleTryExpire(db,de,now)) expired++;if (ttl > 0) {/* We want the average TTL of keys yet not expired. */ttl_sum += ttl;ttl_samples++;}total_sampled++;}total_expired += expired;if (ttl_samples) {long long avg_ttl = ttl_sum/ttl_samples;if (db->avg_ttl == 0) db->avg_ttl = avg_ttl;db->avg_ttl = (db->avg_ttl/50)*49 + (avg_ttl/50);}if ((iteration & 0xf) == 0) { /* check once every 16 iterations. */elapsed = ustime()-start;if (elapsed > timelimit) {timelimit_exit = 1;server.stat_expired_time_cap_reached_count++;break;}}/* 每次檢查只刪除 ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP/4 個(gè)過期鍵 */} while (expired > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP/4);}// ....... }activeExpireCycle 方法在規(guī)定的時(shí)間,分多次遍歷各個(gè)數(shù)據(jù)庫,從過期字典中隨機(jī)檢查一部分過期鍵的過期時(shí)間,刪除其中的過期鍵。
這個(gè)函數(shù)有兩種執(zhí)行模式,一個(gè)是快速模式一個(gè)是慢速模式,體現(xiàn)是代碼中的 timelimit 變量,這個(gè)變量是用來約束此函數(shù)的運(yùn)行時(shí)間的。快速模式下 timelimit 的值是固定的,等于預(yù)定義常量 ACTIVE_EXPIRE_CYCLE_FAST_DURATION,慢速模式下,這個(gè)變量的值是通過 ? 1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/server.hz/100 計(jì)算的。
總結(jié)
本文講了常見的過期刪除策略:
定時(shí)刪除
惰性刪除
定期刪除 而 Redis 使用的是惰性刪除 + 定期刪除的組合策略。
【END】
近期熱文
面試珍藏:最常見的200多道Java面試題(2019年最新版)
Java面試詳解(2020版):500+ 面試題和核心知識點(diǎn)詳解
面試突擊 | Redis 如何從海量數(shù)據(jù)中查詢出某一個(gè) Key?視頻版
關(guān)注下方二維碼,訂閱更多精彩內(nèi)容
朕已閱?
總結(jié)
以上是生活随笔為你收集整理的面试突击 002 | Redis 是如何处理已过期元素的?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第 3-3 课:泛型和迭代器 + 面试题
- 下一篇: 实战:Redis 性能优化方案