What is the mechanism for expiring data in Redis?

Redis’s expiration cleaning mechanism is achieved through the use of two methods, namely lazy deletion and periodic deletion.

  1. Lazy deletion: When a client tries to access an expired key, Redis will detect that the key has expired and delete it. This method ensures memory efficiency as deletion only occurs when the key is accessed. However, the downside of lazy deletion is that expired keys may remain in memory for some time until they are accessed and deleted.
  2. Scheduled deletion: Redis will use a scheduled task to regularly delete expired keys. Periodically, Redis will scan a portion of the database to check and remove any expired keys. Although this ensures timely deletion of expired keys, it does come with some performance overhead as it requires periodically traversing the database to check for expired keys.

It is important to note that Redis’s expiration cleaning mechanism is based on a combination of lazy deletion and periodic deletion. In practice, Redis will determine the proportion of lazy deletion and periodic deletion based on configuration parameters and server load to achieve optimal memory usage efficiency and performance.

广告
Closing in 10 seconds
bannerAds