user1844634
user1844634

Reputation: 1263

Upper and lower time limits for redis key expiry time

Are there limits (upper/lower bounds) on the amount of time that a key in redis is allowed to persist? If so, what are they?

Upvotes: 1

Views: 7924

Answers (3)

chavy
chavy

Reputation: 1068

Got an issue ReplyError: ERR value is not an integer or out of range and have the same question about maximum TTL, so, I found that the maximum value (! in seconds) is 2 147 483 647. The more detailed answer you can find here

Upvotes: 3

LuFFy
LuFFy

Reputation: 9297

There are 4 Commands to Make a Redis Key Expire :

  • EXPIRE (Time Passed in Seconds)

Set a timeout on key. After the timeout has expired, the key will automatically be deleted.

Example :

redis> SET mykey "Hello"
"OK"
redis> EXPIRE mykey 10
(integer) 1
redis> TTL mykey
(integer) 10
redis> SET mykey "Hello World"
"OK"
redis> TTL mykey
(integer) -1
redis>
  • EXPIREAT (Time Passed as Unix Timestamp)

EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since January 1, 1970). A timestamp in the past will delete the key immediately.

Example :

redis> SET mykey "Hello"
"OK"
redis> EXISTS mykey
(integer) 1
redis> EXPIREAT mykey 1293840000
(integer) 1
redis> EXISTS mykey
(integer) 0
  • PEXPIRE (Time Passed in MiliSeconds)

This command works exactly like EXPIRE but the time to live of the key is specified in milliseconds instead of seconds.

Example :

redis> SET mykey "Hello"
"OK"
redis> PEXPIRE mykey 1500
(integer) 1
redis> TTL mykey
(integer) 1
redis> PTTL mykey
(integer) 1498
  • PEXPIREAT (Time Passed in MiliSeconds TimeStamp)

PEXPIREAT has the same effect and semantic as EXPIREAT, but the Unix time at which the key will expire is specified in milliseconds instead of seconds.

redis> SET mykey "Hello"
"OK"
redis> PEXPIREAT mykey 1555555555005
(integer) 1
redis> TTL mykey
(integer) 58130168
redis> PTTL mykey
(integer) 58130167824

Upvotes: 3

for_stack
for_stack

Reputation: 22936

The precision of expiration is millisecond (with SET KEY VALUE PX mill or PEXPIRE mill). So, the min expire time is 1 millisecond.

The max expire time is infinite, i.e. if you do NOT set any expiration, the key will never expire.

Upvotes: 1

Related Questions