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

Redis · Module

GETSET

GETSET atomically replaces a key's string value and returns the value previously stored there. It is deprecated since Redis 6.2.0; new code should use SET with GET.

How it works

GETSET performs an atomic replacement of a key's string value and returns the value that was there before. If the key is absent, it creates it and returns a nil reply; a successful replacement also removes any existing expiration.

Syntax

GETSET key value

Parameters

NameTypeDescription
keyRedisArgumentThe name of the key.
valueRedisArgumentThe new value to set.

Returns

A bulk string containing the old value, or a nil reply if the key does not exist; an error is returned when the existing key is not a string.

Key points

  • It combines replacement and retrieval of the previous string value into one atomic operation.
  • Its command form takes exactly one key and the replacement value.
  • The key argument identifies the string entry to replace.
  • The value argument becomes the key's new string content.
  • When an old value exists, the reply contains that previous value.
  • A missing key produces a nil reply after the new value is stored.
  • A useful pattern is to increment an event counter and atomically read-and-reset it.

Common mistakes

  • GETSET is deprecated from Redis 6.2.0 onward; use SET with the GET option for new code or migrations.
  • The operation fails if the existing key contains a non-string data type.
  • A successful update removes the key's previous TTL.

Example: Replace a value and retrieve the old one

redis-cli SET mykey "Hello"
redis-cli GETSET mykey "World"
redis-cli GET mykey

The command replaces the existing string and prints its previous value; the final GET confirms the new value.

When to use it

Read and reset a counter

A producer increments an event counter, while a consumer periodically reads and clears the accumulated count as one atomic operation.

Compatibility migration

A legacy Redis integration needs to preserve the atomic replace-and-return-old-value behavior during migration.

Interview questions

QWhat does GETSET do, and what does it return when the key is missing?

GETSET atomically writes a new string value and returns the old string value. If the key does not exist, it returns nil after creating the key.

Follow-up: How should new code replace GETSET? Use SET with the GET option, which provides the replacement-and-return-old-value behavior while avoiding this deprecated command.

QWhat replies and errors can GETSET produce?

It returns a bulk string containing the prior value, or a nil reply when the key is absent. If the key exists with a non-string value, Redis returns an error.

Follow-up: What happens to expiration? A successful replacement discards the key's previous TTL, so use an appropriate SET expiration option when migrating.

QGive a practical atomic use case for GETSET.

The command is atomic, so it is useful when a counter must be read and reset without an intervening update. A documented pattern increments a counter with INCR and resets it with GETSET mycounter "0".

Follow-up: Why is SET GET preferable for new implementations? GETSET is deprecated as of Redis 6.2.0, while SET supports the GET option and additional expiration controls.

  • SET
  • GET
  • INCR
  • GETEX
  • GETDEL

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