Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Command

EXPIRE

Sets a relative expiration timeout, in seconds, on a key. When the timeout elapses, Redis automatically deletes the key.

How it works

EXPIRE assigns a relative timeout, in seconds, to an existing key. Redis automatically removes the key after the timeout; calling EXPIRE again replaces the current timeout, while conditional options can restrict when the replacement occurs.

Syntax

EXPIRE key seconds [NX | XX | GT | LT]

Parameters

NameTypeDescription
keykeyThe key whose expiration should be set.
secondsintegerThe time to live, in seconds.
conditionenumOptional condition: NX, XX, GT, or LT.

Returns

An integer reply: 1 if the timeout was set, or 0 if the key does not exist or the requested condition is not satisfied.

Key points

  • EXPIRE sets a relative expiration period measured in seconds.
  • Once the lifetime elapses, Redis automatically deletes the key.
  • Calling EXPIRE again on an expiring key is supported.
  • A subsequent EXPIRE replaces the previous time-to-live with the new value.
  • Expiration remains through ordinary value-preserving operations but can be cleared by deletion or overwrite.
  • Although EXPIRE accepts seconds, Redis internally resolves expiration to one millisecond.
  • Expiration metadata is included in replication and disk persistence.

Common mistakes

  • Commands that delete or overwrite a key's value can remove its expiration, so updating a cached value may make it persistent.
  • A key with an expiration is considered volatile in Redis terminology.
  • EXPIRE does not create a missing key; the command's timeout is ineffective unless the target key already exists.

Example: Cache a session for five minutes

SET session:123 user-42
EXPIRE session:123 300 NX
TTL session:123

Creates a five-minute cache entry only when the key has no existing expiration, then checks its remaining lifetime.

When to use it

Expire sessions

Session and authentication state

Bound cache lifetime

Cached API responses or computed results

Clean up temporary keys

Temporary locks or coordination markers

Interview questions

QWhat does EXPIRE do and what does it return?

EXPIRE takes a key and a relative lifetime in seconds, then returns 1 when the timeout is set or 0 when it is not. A missing key therefore produces 0.

Follow-up: How would you apply a timeout only when the key has none? Use the NX option: EXPIRE key seconds NX.

QWhat happens when EXPIRE is called on a key that already has a timeout?

A later EXPIRE normally replaces the key's existing timeout with the new number of seconds. NX, XX, GT, and LT constrain whether that replacement is allowed.

Follow-up: Which option updates only a longer existing timeout? GT permits the change only when the new timeout is greater than the current one.

QWhat precision does Redis use for key expiration?

The timeout is measured in seconds, but Redis expiration resolution is one millisecond. Redis also preserves expiration information across persistence and replication.

Follow-up: Which commands provide millisecond-based expiration and inspection? PEXPIRE sets milliseconds, while PTTL reports the remaining lifetime in milliseconds.

  • EXPIREAT
  • EXPIRETIME
  • PEXPIRE
  • PERSIST
  • PTTL
  • SET
  • TTL

Source: EXPIRE | Docs - Redis. Checked 2026-10-16.