Redis keys are not expiring - Laravel, Predis

Redis keys are not expiring - Laravel, Predis

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Redis keys are not expiring - Laravel, Predis

When working with Laravel and Predis, it is common to assume that setting a TTL (Time To Live) on a key guarantees its eventual deletion. If you find that your keys persist indefinitely despite having set an expiration time, the issue usually lies not in the Redis server itself, but in how the application logic interacts with the key's lifecycle.

The specific pattern causing this behavior involves creating a key only if it does not exist and then attempting to set an expiration on that same condition. Consider this typical implementation:

if (!$redis->exists($some_unique_key)) {
    $redis->set($some_unique_key, 'Some Value');
    // Intention is to expire in 60 seconds
    $redis->expire($some_unique_key, 60); 
}

At first glance, this logic appears sound. You check for existence, write the value if missing, and immediately attach a TTL. However, there is a subtle but critical flaw in how the SET command behaves regarding existing keys versus new ones.

The root cause of the expiration failure is often related to how the key was initially created or updated within the same logical block. If the logic path involves updating an existing key rather than strictly creating a new one, the behavior changes. More specifically, if you use SET on a key that already exists (even if your exists() check passed because you are in the creation branch, race conditions or logic errors can occur), standard SET commands without the proper flags will overwrite the value but will not automatically update the TTL unless explicitly told to do so.

However, the most frequent culprit in this scenario is the order of operations when the key does exist, or how the expire command interacts with the state of the key at that exact moment. If you are using Predis (which acts as a thin wrapper around the Redis protocol), the underlying Redis server dictates the behavior.

The solution often involves ensuring that the expiration is set consistently every time the key is touched, rather than only during creation. But if you strictly want to handle the "create or update" logic, you must be aware of the EX or PX modifiers available in the SET command itself.

Instead of relying solely on the separate expire() call after a conditional write, you can leverage the built-in expiration modifiers within the set method. This ensures that regardless of whether the key existed before (and was overwritten) or is new, the TTL is applied atomically in the same operation:

$redis->set($some_unique_key, 'Some Value', [
    'expiresIn' => 60
]);

By passing the expiration array directly to the set method, you eliminate the race condition where a key might be updated without an associated TTL. This approach aligns with the Redis documentation for the SET command, which states that if you specify an EX, PX, or NX argument, the command will only set the value and the expiration in one atomic step.

It is also worth noting that using expire() after a set call can sometimes be problematic if the key was just updated via a standard SET without the XX or EX flags in a different context, though in the specific snippet provided above, the logic should hold if the key is truly new. The real danger arises when you update an existing key; a standard set call preserves the old TTL (or sets it to infinite if no TTL was previously set), and a subsequent expire call might fail or behave unexpectedly depending on the exact Redis version and timing.

For robust session-like storage, where keys must strictly adhere to a lifespan, relying on the SET command's native expiration support is generally safer than chaining separate commands. This pattern minimizes the window for errors and ensures that the key never outlives its intended duration, regardless of how many times it is written to.

If you are using Laravel 5.4 with Predis, this behavior is consistent with the library's implementation of the Redis protocol. There is no configuration flag in the standard Redis client libraries that overrides this default behavior; the solution lies strictly in the command syntax used to write the data.

Our network