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

Redis · Command

EXPIREAT

Sets a key's expiration to an absolute Unix timestamp in seconds. Optional NX, XX, GT, or LT conditions control when the deadline may be changed.

How it works

EXPIREAT assigns a key an expiration deadline expressed as an absolute Unix timestamp in seconds rather than as a relative duration. Optional conditions can restrict whether Redis changes the deadline; a timestamp in the past removes the key immediately.

Syntax

EXPIREAT key unix-time-seconds [NX | XX | GT | LT]

Parameters

NameTypeDescription
keykeyThe key whose expiration is being changed.
unix-time-secondsintegerAbsolute expiration time as a Unix timestamp in seconds.
conditionenumOptional mutually exclusive condition: NX, XX, GT, or LT.

Returns

Integer 1 if the timeout was set; integer 0 if it was not set, such as when the key does not exist or a supplied condition prevents the operation.

Key points

  • The deadline is an absolute epoch time in seconds, not a duration counted from the command's execution.
  • NX makes the command conditional on the key currently lacking an expiration.
  • XX limits the update to keys that already have a configured expiration.
  • GT changes the deadline only when the requested expiration is later than the existing one.
  • LT changes the deadline only when the requested expiration is earlier than the existing one.
  • A successful expiration update returns integer 1.

Common mistakes

  • Passing a past timestamp does not merely create an already-expired deadline; Redis deletes the key immediately.
  • Use at most one of NX, XX, GT, or LT in a command.
  • When using GT, a key without an existing expiration is considered to have an infinite current TTL, so a finite new deadline will not be greater.

Example: Set an absolute expiration

SET session:user:42 "active"
EXPIREAT session:user:42 4102444800
TTL session:user:42

Sets a key, assigns it an absolute expiration time in Unix seconds, then checks its remaining TTL.

When to use it

Absolute deadline coordination

Coordinate expiration with an externally calculated deadline shared across services.

Expiration preservation

Restore or migrate data while preserving an expiration time represented as an epoch timestamp.

Bounded deadline updates

Prevent a refresh from shortening or extending a deadline by using LT or GT.

Interview questions

QWhat does EXPIREAT do, and what does it return?

EXPIREAT sets a key's expiration using an absolute Unix timestamp in seconds. It returns 1 when the expiration is set and 0 when the key is missing or the requested condition prevents the change.

Follow-up: How would you apply a deadline only if the key already has one? Use the XX option, which skips the update when the key is persistent.

QHow do NX and XX change EXPIREAT behavior?

NX applies the deadline only when the key currently has no expiration. XX applies it only when the key already has an expiration; these conditional modes cannot be combined.

Follow-up: Which option ensures a new deadline is later than the current one? GT; a persistent key is treated as having an infinite TTL for this comparison.

QHow does EXPIREAT differ from EXPIRE?

The timestamp is measured in seconds since January 1, 1970, unlike EXPIRE, which accepts a relative number of seconds. A deadline in the past deletes the key immediately.

Follow-up: What operational event differs for a past timestamp? Redis deletes the key and generates a del event rather than an expire event.

  • EXPIRE
  • PEXPIREAT
  • EXPIRETIME
  • TTL
  • PERSIST

Source: EXPIREAT | Docs. Checked 2026-10-16.