PERSIST against a key with no expiration
Production Risk
Very Low. It is informational.
This is an informational error from the `PERSIST` command. It indicates that the target key does not have an expiration set, so there is nothing for the `PERSIST` command to remove. The key is already persistent.
- 1Calling `PERSIST` on a key that was created without an expiration.
- 2Calling `PERSIST` on a key that has already had its expiration removed.
- 3A logic bug where a key is assumed to have an expiration when it does not.
A client creates a key without a TTL and then tries to make it persistent.
SET mykey "permanent" PERSIST mykey
expected output
(integer) 0
Fix 1
Check `TTL` before calling `PERSIST`
WHEN If your logic requires knowing if the key was changed
TTL mykey # returns -1 if no expiry # returns -2 if key does not exist # returns N > 0 if expiry is set
Why this works
The `TTL` command allows you to check the status of a key's expiration. You can use this to decide whether calling `PERSIST` is necessary. `PERSIST` itself returns `1` if the timeout was removed and `0` if not, which is the primary way to check.
Fix 2
No action needed if the goal is just to ensure persistence
WHEN The end state is more important than the action
// Application logic simply ensures the key is persistent.
// A return value of 0 from PERSIST is not an error.
redis.persist("mykey");Why this works
If your goal is simply to make sure a key has no expiration, then calling `PERSIST` is idempotent. A return value of `0` means the key was already in the desired state.
✕ Treat the `0` return value as a failure
The command's contract is to return `1` on a change and `0` on no change. A `0` is a success signal that the key was already persistent.
Some older Redis versions might not have returned a specific error message, just the integer reply.
Key expiration handling logic.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev