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

Redis · Command

SET

Sets a key to a string, creating it or replacing its current value. Conditions, expiration controls, and GET support conditional writes, lifetimes, and retrieval of the previous string.

How it works

SET writes a string value to a key, creating it when absent or replacing its existing value regardless of the existing type. Optional conditions control whether the write occurs, while expiration options assign or preserve a lifetime; GET can return the previous string value instead of the normal status response.

Syntax

SET key value [NX | XX | IFEQ ifeq-value | IFNE ifne-value | IFDEQ ifdeq-digest | IFDNE ifdne-digest] [GET] [EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]

Parameters

NameTypeDescription
keyRedis keyName of the key.
valuestringThe string value to set.

Returns

Without GET, OK on success; with NX or XX when the condition fails, nil; with GET, the old string or nil. An error occurs with GET if the existing value is not a string.

Key points

  • SET writes string data even when the key currently contains another Redis data type.
  • A missing key is created by a successful unconditional SET.
  • NX turns SET into an insert-only operation.
  • XX prevents SET from creating a new key.
  • GET changes the reply to the prior string value, or nil for an absent key.
  • EX assigns a positive whole-number lifetime measured in seconds.

Common mistakes

  • Use at most one condition option in a command.
  • Expiration options are mutually exclusive; choose one lifetime behavior.
  • With GET, a non-string existing value causes an error rather than returning it.
  • A successful overwrite removes the old expiry unless KEEPTTL is used.

Example: Conditional write with expiration

SET session:user:42 active EX 300 NX
GET session:user:42

Creates the key only when absent and sets a five-minute expiration.

When to use it

Insert-if-absent initialization

Create a value only when no value is present, avoiding an overwrite.

Expiring cache values

Attach a lifetime directly to a cache entry during the write.

Conditional updates

Replace an existing value only when the key is already present.

Interview questions

QWhat does SET do when the key already exists?

SET stores a string at the specified key. It creates a missing key and overwrites an existing value regardless of its type; a successful ordinary write returns OK.

Follow-up: How can a write be conditional? Use NX for an absent key or XX for an existing key, with the condition options mutually exclusive.

QHow does SET control expiration?

EX and PX specify relative expiration in seconds or milliseconds. EXAT and PXAT specify absolute Unix expiration times, while KEEPTTL preserves the existing lifetime.

Follow-up: What happens if multiple expiration options are supplied? They are mutually exclusive, so the command must select only one.

QWhat does the GET option change?

GET makes SET return the old string stored at the key, or nil when the key did not exist. If the existing value is not a string, Redis returns an error and aborts SET.

Follow-up: Does GET make SET type-safe for other Redis data types? No; it errors on a non-string existing value rather than converting or replacing it.

  • GET
  • SETNX
  • SETEX
  • PSETEX
  • TTL
  • EXPIRE

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