EXPIRE seconds

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Delete a key automatically after the specified seconds

Usage: expire key seconds [type]
📍 type is one of string/list/set/zset/hash/stream.
      The type argument was added in ent811. Please use it.

Example

Command>set key value
Result>1
Command>expire key 5 string
Result>1
Command>ttl key string
Result>3
Command>ttl key string
Result>-2
Command>get key
Result>(nil)

View animation


Overwriting with SET removes expire time

If SET is run on the same key after expire time is set, expire time is removed and the key is not deleted.
Such commands include set, getset.
Commands such as incr, lpush, sadd, zadd, and hset are not affected.

Example

Command>set key value
Result>1
Command>expire key 5 string
Result>1
Command>ttl key string
Result>3
Command>set key new_value
Result>1
Command>ttl key string
Result>-1
Command>get key
Result>new_value

View animation


RENAME does not remove expire time

Changing the key name does not remove expire time.

Example

Command>set key value
Result>1
Command>expire key 5 string
Result>1
Command>rename key new_key string
Result>OK
Command>ttl key string
Result>-2
Command>get key
Result>(nil)

View animation


If the Redis server goes down after EXPIRE

What if the server starts after the expire time has already passed?
When the server starts, it checks expire time and runs DEL.
In fact, even if you run EXPIRE, it is recorded as EXPIREAT in AOF.

Example

Command>set key value
Result>1
Command>expire key 5 string
Result>1
Command>shutdown
Command>Redis Server Start   started 10 seconds later  
Result>del key10   run automatically  

Three cases of deleting keys that have expire set

1) Periodically (10 times per second) delete expired keys

serverCron()->databasesCron(), which runs 10 times per second (every 100ms), runs activeExpireCycle() to delete expired keys.
📚 The interval is determined by the redis.conf hz value. '10' means 10 times per second, '20' means 20 times per second.
Here activeExpireCycle() runs as SLOW type,   and SLOW type has a 25ms time limit.   It can still run longer than 25ms.   That is because it randomly checks whether keys have expired, runs one set (20 times repeated 16 times, 160 times in total), then checks elapsed time and stops if 25ms has passed.
If 25ms has not passed, it runs another set (20 * 16) and checks elapsed time again.   Because of this, elapsed time can exceed 25ms.   For example, if one set takes 10ms, the second set reaches 20ms, which is still under 25ms so it continues, and after the third set at 30ms it is over 25ms so it stops.   If there are many keys to delete and they are hashes with many fields, one set can take 50ms. Then it runs every 100ms, and if it used 50ms, CPU usage becomes 50%.
INFO STATS shows expired_time_cap_reached_count, which is the count of runs that exceeded 25ms.

2) After each command finishes, delete expired keys

After each command finishes, beforeSleep() runs activeExpireCycle() to delete expired keys.
This runs as FAST type, with a short 1ms time limit.   However FAST type also runs in sets of 20 * 16 as above, so if there are keys to delete it typically takes 5ms or more.

3) Each command deletes only that one key

When each command runs, it checks whether the key has expired before the main work, and deletes it if expired.
For example, GET key checks whether "key" exists, then checks expire time; if it has expired, it deletes the key and returns null to the client.   If expire time has not passed yet or there is no expire time, it returns the value.


Expired-key deletion and CPU usage


📚<Image 1>
  • On the Redis server, keys with expire time are deleted 10 times per second (redis.conf hz).
  • Looking at the 17:50 time range of Commands/sec, no commands are running. Yet CPU usage (%) rose up to 50%.
  • Memory (RSS) usage decreases from 17:49 as keys with expire time are deleted.
  • In this case about 15 million keys were deleted in 5 minutes. That is about 50,000 keys per second on average.
    In the peak period it likely deleted up to about 70,000 keys, using up to 50% CPU.
  • Why this case is shown: CPU can be used even when no new commands are issued, and even without running DEL, keys with expire time can be deleted automatically, reducing key count and memory usage.


Syntax

EXPIRE key seconds [type]

  • Available since version 1.0.0.
  • Time complexity is O(1).
  • ent811 type added.
Related commands SETEX, TTL, PERSIST, PSETEX, EXPIREAT, PEXPIRE, PEXPIREAT, PTTL
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< RENAMENX EXPIRE EXPIREAT >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.