// Redis error reference

ERR invalid expire time: Expire value is out of range

The command accepted a TTL option but the number with it is not valid: it must be a positive integer within the supported range.

Error prefix
ERR invalid expire time
类别
Data types and commands
错误信息
ERR invalid expire time in 'set' command

它的意思

Commands that can expire a key, such as SET with EX, PX, EXAT or PXAT, EXPIRE, PEXPIRE, EXPIREAT and GETEX, validate the TTL before doing anything. A zero, a negative number, or a timestamp far outside the representable range produces this error with the command named, and the key is left untouched.

Two shapes of mistake dominate. A computed TTL that resolved to 0 or negative, where the code meant 'no expiry' and passed it straight through. And confusing relative with absolute: EX takes seconds from now, EXAT takes a unix timestamp. Handing a timestamp to EX does not error; it sets a TTL decades out, so the symptom is a key that never expires.

常见原因

  • A computed TTL resolved to 0 or negative, for example a deadline that was already in the past.
  • An absolute unix timestamp was passed to the relative option, EX instead of EXAT or the reverse.
  • The client sent the TTL in the wrong form, such as the string 60s or a nil value.

Find the cause

SET session:abc data EX 0
# (error) ERR invalid expire time in 'set' command - the check itself
TTL session:abc
# The key is unchanged: -1 (no TTL) or -2 (missing)
TIME
# The server clock, for computing EXAT values

如何修复它

  1. Check the value and unit at the call site; relative options need positive integers:

    SET session:abc data EX 3600
    # 3600 seconds from now
    SET session:abc data EXAT 1798761600
    # absolute unix timestamp
  2. Guard computed TTLs before the call, and skip the command entirely when the intent was 'no expiry'.

  3. When the intent is 'keep the existing TTL', say so instead of recomputing it:

    SET session:abc newdata KEEPTTL

Other ways to handle it

Centralize TTL computation in one client helper so every expiry goes through the same validation, and prefer EXPIRE after the write when the TTL comes from a later decision. KEEPTTL replaces most recomputation of existing expiries.

安全地复现它

redis-cli SET session:abc data EX 0
# (error) ERR invalid expire time in 'set' command
redis-cli SET session:abc data EX 60
# OK
redis-cli TTL session:abc
# (integer) 60 or a little less

问题

Does EX 0 delete the key immediately?

No, SET rejects it before anything happens. To expire on purpose, use EXPIRE, which treats a non-positive TTL as 'delete now', while SET's EX option validates instead.

Is the timestamp in seconds or milliseconds?

EX and EXAT are seconds; PX and PXAT are milliseconds. Mixing the two does not raise this error; it sets the wrong expiry silently, so read TTL or PTTL after the write to confirm.

相关错误

More data types and commands errors

代码和消息文本来自 Redis documentation and source code。浏览完整的 Redis error list 或者 搜索每个数据库错误.

// 错误更少,修复更快

Work on Redis in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for Redis and the other databases you use.

本页文字和数据: CC BY 4.0,请注明出处 SQLly (sqlly.app)。 代码和 SQL 片段: MIT. 许可详情 引用的错误消息文本归其数据库厂商所有。