Explore Redis data expiration policies

Source: Internet
Author: User

Explore Redis data expiration policies

Use the EXPIRE key seconds command to set the data expiration time. If the return value is 1, the setting is successful. If the return value is 0, the key does not exist or the expiration time cannot be set successfully.

After the expiration time is set on the key, the key will be automatically deleted after the specified number of seconds. Keys with a specified expiration time are called unstable in Redis.

When the key is deleted by the DEL command or reset by the SET or GETSET commands, the expiration time associated with the key is cleared.

Redis 127.0.0.1: 6379> set mykey "test expire"
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 97
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 93
Redis 127.0.0.1: 6379> set mykey "test expire reset"
OK
Redis 127.0.0.1: 6379> ttl mykey
(Integer)-1

Redis 127.0.0.1: 6379> set mykey "test expire"
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 98
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 91
Redis 127.0.0.1: 6379> getset mykey "test expire reset"
"Test expire"
Redis 127.0.0.1: 6379> ttl mykey
(Integer)-1

This also means that all operations that only update the value stored in the key and do not replace the original value of the key with the new value will not affect the expiration time set on the key. For example, if you use the INCR command to increase the key value or use the LPUSH command to add a new element to the list or use the HSET command to update the hash field value, the original expiration time setting will be cleared.

Redis 127.0.0.1: 6379> set mykey 1
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 95
Redis 127.0.0.1: 6379> incr mykey
(Integer) 2
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 77

Redis 127.0.0.1: 6379> lpush listkey 1
(Integer) 1
Redis 127.0.0.1: 6379> expire listkey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl listkey
(Integer) 94
Redis 127.0.0.1: 6379> lpush listkey 2
(Integer) 2
Redis 127.0.0.1: 6379> ttl listkey
(Integer) 82

Redis 127.0.0.1: 6379> hmset hashkey name "redis" passwd "redis"
OK
Redis 127.0.0.1: 6379> expire hashkey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl hashkey
(Integer) 95
Redis 127.0.0.1: 6379> hset hashkey passwd "redis. vs. mysql"
(Integer) 0
Redis 127.0.0.1: 6379> ttl hashkey
(Integer) 66

You can also run the PERSIST command to clear the preset expiration time and change the key to persistent.

Redis 127.0.0.1: 6379> set mykey 'test clear expire'
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 96
Redis 127.0.0.1: 6379> persist mykey
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer)-1

If the key is renamed by the RENAME Command, the expiration time associated with the key is passed to the key of the new name.

Redis 127.0.0.1: 6379> get mykeynew
(Nil)
Redis 127.0.0.1: 6379> set mykey 'test expire transfer'
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 96
Redis 127.0.0.1: 6379> rename mykey mykeynew
OK
Redis 127.0.0.1: 6379> ttl mykey
(Integer)-1
Redis 127.0.0.1: 6379> ttl mykeynew
(Integer) 80

If the key is rewritten by the RENAME command, for example, if there is a RENAME mykey_ B mykey_a key named mykey_a and a key named mykey_ B, renaming mykey_ B as an existing mykey_a will inherit all the features of mykey_ B regardless of the original settings of mykey_a, including the expiration time settings.

Redis 127.0.0.1: 6379> set mykey_ B 'B'
OK
Redis 127.0.0.1: 6379> set mykey_a 'A'
OK
Redis 127.0.0.1: 6379> expire mykey_ B 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey_ B
(Integer) 93
Redis 127.0.0.1: 6379> ttl mykey_a
(Integer)-1
Redis 127.0.0.1: 6379> rename mykey_ B mykey_a
OK
Redis 127.0.0.1: 6379> ttl mykey_ B
(Integer)-1
Redis 127.0.0.1: 6379> ttl mykey_a
(Integer) 66

When EXPIRE key seconds is applied to a key with an expiration time set, the original expiration time is updated to the new expiration time.

Redis 127.0.0.1: 6379> set mykey 'test expire Update'
OK
Redis 127.0.0.1: 6379> expire mykey 100
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 95
Redis 127.0.0.1: 6379> expire mykey 1000
(Integer) 1
Redis 127.0.0.1: 6379> ttl mykey
(Integer) 998

Appendix:
Key expiration time
Generally, Redis keys are not automatically associated with the expiration time when they are created. Keys will exist for a long time unless they are deleted by commands such as DEL. The EXPIRE command cluster can associate the specified key with an expiration time, at the cost of an additional memory overhead. After the key expires, Redis must ensure that the key is removed during timeout. The key expiration time can be updated by the EXPIRE command or completely removed by the PERSIST command.

Precision of expiration time
In Redis2.4, the expire precision is not high, usually between 0 and 1 second. After Redis2.6, the expire precision can be controlled within 0 to 1 millisecond.

Expiration and persistence

Key expiration information is stored as an absolute Unix timestamp (stored in milliseconds after Redis2.6 ). This means that even if the Redis instance is not running, it will not affect the key expiration time.
To make the expiration time work more correctly, you must check the computer's time. If you move an RDB snapshot from one computer to another, something interesting will happen. For example, when data is loaded, it will time out. Even if the computer's time is incorrect or changes occur in the same instance of the same machine, this is also a problem. For example, the expiration time of a batch of data is set to 1000, after that, the computer time is unexpectedly faster than 2000 seconds, so these keys will expire immediately, instead of 1000 seconds later.

How does Redis expire keys?
Redis keys expire in two ways: passive and active
When clients tries to access a key with an expiration time set and has expired, it is an active expiration method.
However, this is not enough because it is possible that some keys will never be accessed again. These keys with an expiration time must also be deleted after expiration. Therefore, Redis periodically tests and processes a batch of keys with an expiration time at random. Expired keys tested will be deleted. Typically, Redis performs the following steps 10 times per second:
1. randomly test 100 keys with an expiration time
2. Delete All expired keys found
3. If the number of deleted keys exceeds 25, repeat Step 1.
This is a simple probability-based algorithm. The basic assumption is that the extracted samples can represent the entire key space. redis continues to clean up expired data until the percentage of keys to be expired falls below 25%. This also means that the number of keys that have expired at any given time, but still occupy the memory space, is the maximum number of write operations per second divided by 4.

Expiration in replication link and AOF files
In order to get the correct behavior without causing consistency problems, when a key expires, the DEL operation will be recorded in the AOF file and passed to all related slave. That is, the expiration and deletion operations are uniformly carried out in the master instance and passed down, instead of being controlled by each salve. In this way, data inconsistency does not occur. After the slave is connected to the master node, the expired key cannot be cleared immediately (you need to wait for the DEL operation passed by the master node ), slave still needs to manage and maintain the expiration status in the dataset so that the Server Load balancer can be upgraded to the master to perform the expiration processing independently as the master.

Install and test Redis in Ubuntu 14.04

Redis cluster details

Install Redis in Ubuntu 12.10 (graphic explanation) + Jedis to connect to Redis

Redis series-installation, deployment, and maintenance

Install Redis in CentOS 6.3

Learning notes on Redis installation and deployment

Redis. conf

Redis details: click here
Redis: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.