> ## Content Index
> Fetch the complete content index at: https://debugly.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# Redis Updated the Value. Where Did the Expiration Go?
- URL: https://debugly.dev/redis-set-clears-ttl-keepttl/
- Published: 2026-11-12T21:42:00.000Z
- Updated: 2026-11-12T21:42:00.000Z
- Description: A successful Redis SET can leave a cache entry with no expiration. Diagnose TTL -1 and choose whether updates should preserve or reset the deadline.
- Author: Rohit Bhadani
- Tags: Databases, Caching, Debugging

The cache refresh returns `OK`. A minute later, the key that should have expired is still there. If Redis `TTL` now returns `-1`, check the last write before you blame the expiry worker: a successful plain `SET` replaces the value and removes its existing expiration. Use `KEEPTTL` to preserve the existing deadline, or set a new one on the write with `EX` or `PX`.

*This is an AI-prepared, documentation-based explainer, not a personally reproduced incident. The Redis commands below are illustrative and were not run for this article. See the* [*editorial policy*](https://debugly.dev/editorial-policy/)*.*

## The value is new. The deadline is gone.

Imagine a worker that caches a shipping quote for a minute. The create path uses `SET … EX 60`. Later, a separate refresh path adds a carrier label and writes the resulting JSON with plain `SET`. The update succeeds, the UI sees the new label, and the quote is now free to outlive the price it was meant to represent. The cache got a promotion to permanent storage without so much as a meeting.

[Redis documents expiration as part of the write contract](https://redis.io/docs/latest/commands/set/?ref=debugly.dev): a successful `SET` discards the previous TTL unless you ask Redis to preserve it or attach a new expiration. That differs from an in-place operation such as `INCR` or `HSET`, which [leave an existing key timeout alone](https://redis.io/docs/latest/commands/expire/?ref=debugly.dev). If a refactor swaps a field update for serializing and replacing the whole object, test the expiry behavior too.

## Check the value and the clock together

On a disposable Redis instance, run the following in quick succession:

```
SET demo:quote standard EX 60
TTL demo:quote
SET demo:quote standard-with-label
GET demo:quote
TTL demo:quote
```

The first TTL should be nonnegative while the key is still volatile. The second should be `-1`: the value is present with no expiration. A `-2` means the key is missing; it doesn’t tell you whether the key expired, was deleted, or never existed. [PTTL](https://redis.io/docs/latest/commands/pttl/?ref=debugly.dev) gives milliseconds and the same distinction on modern Redis. Check the key namespace, database and server role before comparing readings. Separate commands are separate observations: another writer can change the key between them.

In an application, look at what each write path actually sends. A convenience wrapper may set a default expiration on creation and omit it on refresh. A successful response tells you the command ran; it doesn’t tell you the command carried the expiration policy. If a key keeps showing a positive TTL that jumps upward instead, look for a writer resetting it with a fresh duration. That's a different problem from losing it.

## Choose the policy before choosing the flag

- **Keep the original deadline:** `SET demo:quote revised XX KEEPTTL`. `XX` prevents recreating a missing key, and `KEEPTTL` (Redis 6.0+) retains any expiration the existing key already has. A persistent key stays persistent. This is an existence check, not a check that the key is still volatile or has the value you originally read.
- **Give each successful refresh a new minute:** `SET demo:quote revised EX 60`. The value and expiration are set together. A separate `SET` followed by `EXPIRE` leaves a gap where a crash can strand a persistent key, or a competing write can receive the expiry intended for another value.
- **Use an absolute cutoff:** if the quote has an authoritative expiry timestamp, `EXAT` or `PXAT` (Redis 6.2+) can set that deadline explicitly. Check units and reject already invalid quotes in the application; a new relative TTL could outlive the upstream cutoff.

If refresh must update only the version you read, `XX` alone is insufficient: another worker can replace the key between read and write. Use a documented conditional write supported by your Redis version, or an appropriate [optimistic transaction](https://redis.io/docs/latest/develop/using-commands/transactions/?ref=debugly.dev). Then test creation, refresh before expiry, refresh after the key disappears, and refresh of a key that was already persistent. Use [TTL](https://redis.io/docs/latest/commands/ttl/?ref=debugly.dev) or `PTTL` to assert policy rather than an exact number of elapsed milliseconds. Debugly’s [Redis pipelining walkthrough](https://debugly.dev/redis-round-trips-pipelining-latency/) is useful when batching these calls; batching reduces round trips, but it doesn’t turn a sequence into an atomic write.