Coder's Cat

How Redis expire Key

2020-04-04

4 Redis commands to set expiration

There are four ways to set the expiration time in Redis:

  • expire key TTL(seconds): Set the key to expire after n seconds;
  • pexpire key TTL(milliseconds): Set the key to expire after n milliseconds;
  • expireat key timestamp: Set the key to expire after a specific timestamp (accurate to seconds);
  • pexpireat key millisecondsTimestamp: Set the key to expire after a specific timestamp (accurate to milliseconds);

    The time complexity is all O(1), and usages of them are similar.

"OK"
redis> EXPIRE mykey 10
(integer) 1
redis> TTL mykey
(integer) 10
redis> SET mykey "Hello World"
"OK"
redis> TTL mykey
(integer) -1
redis>

The reply of a expire command is:

1 if the timeout was set.
0 if key does not exist.

If you want to remove the expiration of a key, use persist key.

2 expiration mechanism in Redis

How Redis implements the strategy of removing an expired key?

One straight solution is we create a task with timer for those keys which set with expiration time. When the key reaches the expiration time, the timed task will be triggered to delete the key immediately.

Advantages: Friendly to memory, this deletion strategy can ensure that expired keys will be deleted as soon as possible, and release the memory used by the key.

Disadvantages: Not friendly to CPU. The task of deleting keys will cost CPU, and this is more obvious under the condition of high load. It may affect the server’s response time and throughput.

Redis is a memory key-value database designed for high performance. Let’s have a look at how it handles this challenge.

According to the documents of Redis, there are two mechanisms implemented to recover the memory used by expired keys.

Passive deletion

The expired key will continue to live in memory, but Redis adds a check process for keys with expired time.

The key will be deleted if it expires, and it is returned as usual if the key does not expire.

Advantages: Friendly to CPU, passive deletion make sure we only delete a key if necessary. An expired key but never queried will continue to live.

Disadvantages: More memory will be costed. An expired key but never queried will continue to live.

Active deletion

Because of the flaws of the previous solution, Redis add another active way to delete expired keys.

Redis periodically check a few keys with expiration. All the keys that are already expired are deleted from the keyspace.

Specifically, this is what Redis does 10 times per second:

Step 1: Test 20 random keys from the set of keys with an associated expire. Delete all the keys found expired.

Step 2: If more than 25% of keys were expired, start again from step 1.

This mechanism makes sure expired keys will not cost more than 25% memory.

Periodic deletion can be configured with the option hz in redis.conf, which defaults to 10 - which means 10 executions per second.

To limit the memory usage of Redis, we can also configue the maxmemory option in redis.conf.

Join my Email List for more insights, It's Free!😋

Tags: Misc