All Products
Search
Document Center

Tair (Redis® OSS-Compatible):Redis String command enhancements

Last Updated:Mar 28, 2026

CAS (Compare And Set) and CAD (Compare And Delete) are atomic conditional commands for native Redis strings on Tair instances. CAS updates a key's value only when it matches the expected value; CAD deletes a key only when its value matches.

Note

These commands apply to native Redis strings only. For the equivalent operations on TairStrings, use EXCAS and EXCAD.

Prerequisites

Before you begin, ensure that you have:

Command overview

CommandSyntaxDescription
CASCAS key oldvalue newvalue [EX|PX|EXAT|PXAT time]Compares the key's current value with oldvalue. If they match, updates the value to newvalue.
CADCAD key valueCompares the key's current value with value. If they match, deletes the key.

Syntax conventions

NotationMeaning
UPPERCASECommand keyword
italicVariable — replace with an actual value
[options]Optional parameter; parameters without brackets are required
A|BMutually exclusive — specify only one
...Parameter can be repeated

CAS

Atomically compares the current value of a key with oldvalue and, if they match, replaces it with newvalue.

Syntax: CAS key oldvalue newvalue [EX|PX|EXAT|PXAT time]

Time complexity O(1)

Parameters

ParameterDescription
keyTarget key of the string type
oldvalueExpected current value of the key
newvalueValue to set if oldvalue matches the current value
EX timeRelative expiration in seconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire.
PX timeRelative expiration in milliseconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire.
EXAT timeAbsolute expiration as a Unix timestamp in seconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire.
PXAT timeAbsolute expiration as a Unix timestamp in milliseconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire.
Note

If the key has a Time to Live (TTL) and you run CAS without an expiration parameter, the TTL is removed and the key will not expire.

Return value

ValueMeaning
1Value matched; key updated
0Value did not match; key unchanged
-1Key does not exist
ErrorOther failure

Examples

The following session demonstrates all three CAS outcomes: mismatch, match, and expiry.

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> CAS foo baa bzz
(integer) 0
127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> CAS foo bar bzz
(integer) 1
127.0.0.1:6379> GET foo
"bzz"
127.0.0.1:6379> CAS foo bzz too EX 10
(integer) 1
127.0.0.1:6379> GET foo
"too"

After the 10-second TTL expires, the key is gone:

127.0.0.1:6379> GET foo
(nil)

CAD

Atomically compares the current value of a key with value and, if they match, deletes the key.

Syntax: CAD key value

Time complexity O(1)

Parameters

ParameterDescription
keyTarget key of the string type
valueExpected current value of the key

Return value

ValueMeaning
1Value matched; key deleted
0Value did not match; key unchanged
-1Key does not exist
ErrorOther failure

Examples

The following session demonstrates all three CAD outcomes: mismatch, key not found, and successful deletion.

127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> CAD foo bzz
(integer) 0
127.0.0.1:6379> CAD not-exists xxx
(integer) -1
127.0.0.1:6379> CAD foo bar
(integer) 1
127.0.0.1:6379> GET foo
(nil)

What's next