Set the expiration time of data

Redis is a memory database, memory space is limited, need to arrange reasonable management of memory space, so when writing data, need to consider whether the data needs to be permanently stored, if not, it is best to set the expiration time to save memory!

In Redis, you can use the expire command to specify the expiration time of a key. Here we need to emphasize the hash type

Redis refers to the data type of value, such as hash. Value is a hash table. Hash has two types of keys, one is the primary key in the redis key-value database. The other is the hash key of the value in key-value.

Note that expiration times for all data types in Redis apply to the primary key in key-value.

That is, there is no way for hash to expire a single piece of data. The same goes for list, set, or zset

Example Set a clearing policy for expired data

The following three policies are used to clear expired data:

  • Timed expiration: You need to create a timer for each key that is set to expire. The timer is cleared immediately when the key expires

    • Advantages: Data expiration is timely and memory friendly disadvantages: A timer needs to be created for each key with expiration time, which consumes a large amount of CPU resources to process expired data, affecting the cache response time and throughput
  • Lazy expiration: Only when a key is accessed, the system checks whether the key has expired

    • Advantages: This policy maximizes CPU resource savings
    • Disadvantages: Very memory unfriendly, there may be a large number of expired keys that are not accessed again and thus will not be cleared, taking up a large amount of memory space.
  • Expires regularly: Every once in a while, scan the Expires dictionary for some keys and clear the expires keys

    • This strategy is a compromise between the first two. By adjusting the interval of periodic scan and the time limit of each scan, you can achieve the optimal balance of CPU and memory resources in different situations.

The Expires dictionary stores Pointers to all expired keys and their expiration times. Expires is a hash structure where a hash key holds a pointer to the key space that has an expiration date set. The hash key holds the expiration time of the key, and the value is a UNIX timestamp with millisecond precision

The key space holds all the keys in the Redis cluster

Redis uses periodic expiration as a basic policy to ensure that most of the expired data is cleared without occupying too much CPU resources. In addition, lazy expiration is used to solve the problem that a small part of the expired data is not covered by periodic expiration.

Set the data elimination policy

Reasonable set up data expiration time, also set up a reasonable expiration data cleansing strategy can rest easy, sometimes volume increases suddenly, many data Redis are not to the expiration time, memory is running out, this time we have to make a choice of cleaning out some haven’t even some permanent data

Redis offers six elimination strategies in two broad categories

  • Selective elimination from global key space

    • Noeviction: New write operations will bug when memory is insufficient to accommodate new write data.
    • Allkeys-lru: Removes the least recently used key from the key space when memory is insufficient to accommodate new writes
    • Allkeys-random: Randomly removes a key from the key space when memory is insufficient to accommodate new writes.
  • Selective elimination from key space with expiration time set

    • Volatile – lRU: Removes the least recently used key from the expired key space when memory is insufficient to accommodate new writes
    • Volatile -random: Randomly removes a key from the expired key space when memory is insufficient to accommodate new writes.
    • Volatile – TTL: When the memory is insufficient to accommodate new data, the expiring key is removed from the expiring key space

Volatile – LRU, volatile- TTL and allkeys-lRU are the most commonly used policies