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- Categoria
- Data types and commands
O que significa
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.
Causas comuns
- 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
Como corrigir
-
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
-
Guard computed TTLs before the call, and skip the command entirely when the intent was 'no expiry'.
-
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.
Reproduza com segurança
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
Perguntas
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.
Erros relacionados
ERR wrong number of argumentsArgument count is wrong22003PostgreSQL: valor numérico fora do intervalo1264MySQL: valor fora do intervalo para a coluna
More data types and commands errors
WRONGTYPEThe key holds a different data typeERR unknown commandRedis does not know this commandNOSCRIPTScript SHA1 is not cachedEXECABORTTransaction discarded after an errorBUSYServer is blocked by a long script
O código e o texto da mensagem são de Redis documentation and source code. Veja toda a Redis error list ou pesquise todos os erros do banco de dados.
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.
Texto e dados desta página: CC BY 4.0, com crédito ao SQLly (sqlly.app). Código e trechos SQL: MIT. Detalhes da licença O texto citado das mensagens de erro pertence ao fornecedor do banco de dados.