Redis 中的過期元素是如何被處理的?影片+圖文版給你答案——面試突擊 002 期

本文以面試問題「Redis 中的過期元素是如何被處理的?」為切入點,用影片加圖文的方式和大家聊聊 Redis 過期元素被處理的相關知識點。

涉及的知識點

  1. 過期刪除策略有哪些?
  2. 這些過期策略有哪些優缺點?
  3. Redis 使用的是什麼過期策略?
  4. Redis 是如何優化和執行過期策略的?

影片答案

點擊查看影片內容:https://www.bilibili.com/video/av88741972/

圖文答案

常見的過期策略:

  • 定時刪除
  • 惰性刪除
  • 定期刪除

1)定時刪除

在設置鍵值過期時間時,創建一個定時事件,當過期時間到達時,由事件處理器自動執行鍵的刪除操作。

① 優點

保證記憶體可以被儘快的釋放

② 缺點

在 Redis 高負載的情況下或有大量過期鍵需要同時處理時,會造成 Redis 伺服器卡頓,影響主業務執行。

2)惰性刪除

不主動刪除過期鍵,每次從資料庫獲取鍵值時判斷是否過期,如果過期則刪除鍵值,並返回 null。

① 優點

因為每次訪問時,才會判斷過期鍵,所以此策略只會使用很少的系統資源。

② 缺點

系統佔用空間刪除不及時,導致空間利用率降低,造成了一定的空間浪費。

③ 源碼解析

惰性刪除的源碼位於 src/db.c 文件的 expireIfNeeded 方法中,源碼如下:

int expireIfNeeded(redisDb *db, robj *key) {      // 判斷鍵是否過期      if (!keyIsExpired(db,key)) return 0;      if (server.masterhost != NULL) return 1;      /* 刪除過期鍵 */      // 增加過期鍵個數      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;  }  // 獲取鍵的過期時間  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);  }

所有對資料庫的讀寫命令在執行之前,都會調用 expireIfNeeded 方法判斷鍵值是否過期,過期則會從資料庫中刪除,反之則不做任何處理。

3)定期刪除

每隔一段時間檢查一次資料庫,隨機刪除一些過期鍵。

Redis 默認每秒進行 10 次過期掃描,此配置可通過 Redis 的配置文件 redis.conf 進行配置,配置鍵為 hz 它的默認值是 hz 10

需要注意的是:Redis 每次掃描並不是遍歷過期字典中的所有鍵,而是採用隨機抽取判斷並刪除過期鍵的形式執行的。

定期刪除的執行流程:

① 優點

通過限制刪除操作的時長和頻率,來減少刪除操作對 Redis 主業務的影響,同時也能刪除一部分過期的數據減少了過期鍵對空間的無效佔用。

② 缺點

記憶體清理方面沒有定時刪除效果好,同時沒有惰性刪除使用的系統資源少。

③ 源碼解析

定期刪除的核心源碼在 src/expire.c 文件下的 activeExpireCycle 方法中,源碼如下:

void activeExpireCycle(int type) {      static unsigned int current_db = 0; /* 上次定期刪除遍歷到的資料庫ID */      static int timelimit_exit = 0;      /* Time limit hit in previous call? */      static long long last_fast_cycle = 0; /* 上一次執行快速定期刪除的時間點 */      int j, iteration = 0;      int dbs_per_call = CRON_DBS_PER_CALL; // 每次定期刪除,遍歷的資料庫的數量      long long start = ustime(), timelimit, elapsed;      if (clientsArePaused()) return;      if (type == ACTIVE_EXPIRE_CYCLE_FAST) {          if (!timelimit_exit) return;          // ACTIVE_EXPIRE_CYCLE_FAST_DURATION 是快速定期刪除的執行時長          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;      // 慢速定期刪除的執行時長      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; /* 刪除操作的執行時長 */      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;              // 每個資料庫中檢查的鍵的數量              if (num > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP)                  num = ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP;              // 從資料庫中隨機選取 num 個鍵進行檢查              while (num--) {                  dictEntry *de;                  long long ttl;                  if ((de = dictGetRandomKey(db->expires)) == NULL) break;                  ttl = dictGetSignedInteger                  // 過期檢查,並對過期鍵進行刪除                  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 個過期鍵 */          } while (expired > ACTIVE_EXPIRE_CYCLE_LOOKUPS_PER_LOOP/4);      }      // .......  }

activeExpireCycle 方法在規定的時間,分多次遍歷各個資料庫,從過期字典中隨機檢查一部分過期鍵的過期時間,刪除其中的過期鍵。

這個函數有兩種執行模式,一個是快速模式一個是慢速模式,體現是程式碼中的 timelimit 變數,這個變數是用來約束此函數的運行時間的。快速模式下 timelimit 的值是固定的,等於預定義常量 ACTIVE_EXPIRE_CYCLE_FAST_DURATION,慢速模式下,這個變數的值是通過 1000000*ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC/server.hz/100 計算的。

總結

本文講了常見的過期刪除策略:

  • 定時刪除
  • 惰性刪除
  • 定期刪除

Redis 採用的是惰性刪除 + 定期刪除的組合策略,更多內容,詳見影片部分。