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

Redis · Command

GETEX

Returns a string value while optionally setting, changing, or removing the key's expiration. GETEX supports relative and absolute expiration forms and is a write command.

Since
6.2.0

How it works

GETEX reads the string stored at a key and can change that key's expiration in the same command. Choose one mutually exclusive option: a relative lifetime with EX or PX, an absolute expiration with EXAT or PXAT, or PERSIST to remove the existing lifetime.

Syntax

GETEX key [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]

Parameters

NameTypeDescription
keyRedis keyName of the string key to retrieve.
EX secondsoptional integerRelative expiration duration in seconds.
PX millisecondsoptional integerRelative expiration duration in milliseconds.
EXAT unix-time-secondsoptional integerAbsolute Unix expiration time in seconds.
PXAT unix-time-millisecondsoptional integerAbsolute Unix expiration time in milliseconds.
PERSISToptional flagRemove the key's associated TTL.

Returns

Bulk string reply containing the value of key; a nil reply if the key does not exist.

Key points

  • GETEX combines reading a key with optional expiration management.
  • EXAT sets expiration at an absolute Unix timestamp expressed in seconds.
  • PXAT sets expiration at an absolute Unix timestamp expressed in milliseconds.

Common mistakes

  • Only one expiration option may be supplied in a GETEX call.
  • Although it returns a value like GET, GETEX is classified as a write command because it can modify expiration metadata.
  • A missing key produces a nil reply rather than a string value.

Example: Read and refresh a cache entry

SET session:42 "user-data"
GETEX session:42 EX 300

Stores a value, then reads it while assigning a five-minute expiration.

When to use it

Cache entry refresh

Read a cached value and assign a fresh relative lifetime in one operation.

Relative expiration

Apply a millisecond-precision or second-precision deadline supplied by an application.

Absolute deadline

Use a shared Unix deadline when several processes must observe the same expiry instant.

Make a key persistent

Read a value while removing its existing TTL.

Interview questions

QWhat does GETEX do, and how does it differ from GET?

GETEX returns the string value for a key and optionally changes its expiration. Unlike GET, it is a write command because expiration metadata may be changed.

Follow-up: Why might GETEX be useful in a cache? It can retrieve an entry and refresh or assign its lifetime in one command, avoiding a separate expiration update.

QWhat expiration modes does GETEX support?

EX sets a relative lifetime in seconds, while PX uses milliseconds. EXAT and PXAT instead specify absolute Unix expiration times; PERSIST removes the key's associated TTL.

Follow-up: Which option is appropriate for a deadline shared across clients? EXAT or PXAT, because both represent an absolute expiration instant.

QWhat does GETEX return when the key is absent?

If the key exists, GETEX returns its string value. If it does not exist, the command returns a nil or null reply, depending on the client representation.

Follow-up: What should application code do with a missing result? Treat it as a cache miss and avoid assuming the returned value is an empty string.

  • GET
  • PERSIST
  • TTL

Source: GETEX. Checked 2026-10-16.