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

Redis · Module

PEXPIRE

Sets a key's expiration timeout in milliseconds. Optional NX, XX, GT, and LT modes conditionally control whether the timeout is applied.

Since
Redis Open Source 2.6.0

How it works

PEXPIRE assigns a timeout to an existing key in milliseconds. Redis removes the key after the timeout elapses; optional mutually exclusive modes restrict setting the expiry based on whether an expiry exists or how the new timeout compares with the current one.

Syntax

PEXPIRE key milliseconds [NX | XX | GT | LT]

Parameters

NameTypeDescription
keykeyKey whose timeout is modified.
millisecondsintegerTimeout duration, in milliseconds.
modeoptionOptional mutually exclusive condition: NX, XX, GT, or LT.

Returns

Integer reply: 1 if the timeout was set; 0 if it was not set, including when the key does not exist or an option condition fails.

Key points

  • PEXPIRE is the millisecond-granularity counterpart of EXPIRE.
  • The key argument identifies the key whose lifetime should be changed.
  • The milliseconds argument specifies the duration before expiration.
  • NX protects an already-expiring key by applying the timeout only when no expiry exists.
  • XX requires the target key to already have an expiration.
  • GT allows replacement only when the new expiration is later than the current expiration.
  • LT allows replacement only when the new expiration is earlier than the current expiration.

Common mistakes

  • NX, XX, GT, and LT cannot be combined.
  • With GT, a key without an expiry is considered to have an infinite current TTL, so a finite new expiry does not replace it.
  • A missing key or a condition that fails returns 0 rather than applying an expiry.

Example: Set a millisecond expiration

SET session:42 "active"
PEXPIRE session:42 1500
PTTL session:42

Stores a value, assigns a 1.5-second expiration, then reads the remaining lifetime in milliseconds.

When to use it

Apply precise cache expiration

Session and cache entries need sub-second or exact millisecond lifetimes.

Conditionally initialize a timeout

A write should establish an expiry only if another operation has not already set one.

Adjust an existing expiration

A refresh should extend or shorten an existing lifetime only under a chosen comparison.

Interview questions

QWhat does PEXPIRE do, and what does it return?

PEXPIRE sets a key's time-to-live using milliseconds rather than seconds. It returns 1 when the timeout is applied and 0 when it is not, such as when the key does not exist.

Follow-up: How would you verify millisecond precision? Use PTTL after setting the timeout; it reports the remaining lifetime in milliseconds.

QExplain the NX, XX, GT, and LT options.

NX applies the timeout only if the key currently has no expiry, while XX requires an existing expiry. GT applies it only when the new timeout is greater than the current one, and LT requires it to be less.

Follow-up: What happens if these modes are combined? The options are mutually exclusive, so they must be supplied individually.

QWhen is an expired key removed and when are expiration events generated?

A positive timeout causes the key to expire after the specified duration. Redis can remove expired keys when accessed or through background expiration, so expiration-related events are not guaranteed to occur exactly when the TTL reaches zero.

Follow-up: What event accompanies a negative timeout? Redis deletes the key immediately and emits only a del event, rather than an expire event.

  • EXPIRE
  • PEXPIREAT
  • PEXPIRETIME
  • PTTL
  • TTL
  • SET

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