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.
Prerequisites
Before you begin, ensure that you have:
A Tair DRAM-based or persistent memory-optimized instance running minor version 1.2.3 or later
(Recommended) Updated the instance to the latest minor version for additional features and stability — see Update the minor version of an instance
(For cluster or read/write splitting instances) Updated the proxy nodes to the latest minor version so all commands run as expected
Command overview
| Command | Syntax | Description |
|---|---|---|
| CAS | CAS key oldvalue newvalue [EX|PX|EXAT|PXAT time] | Compares the key's current value with oldvalue. If they match, updates the value to newvalue. |
| CAD | CAD key value | Compares the key's current value with value. If they match, deletes the key. |
Syntax conventions
| Notation | Meaning |
|---|---|
UPPERCASE | Command keyword |
italic | Variable — replace with an actual value |
[options] | Optional parameter; parameters without brackets are required |
A|B | Mutually 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
| Parameter | Description |
|---|---|
key | Target key of the string type |
oldvalue | Expected current value of the key |
newvalue | Value to set if oldvalue matches the current value |
EX time | Relative expiration in seconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire. |
PX time | Relative expiration in milliseconds. 0 causes the key to expire immediately. If omitted, any existing TTL is removed and the key does not expire. |
EXAT time | Absolute 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 time | Absolute 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. |
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
| Value | Meaning |
|---|---|
1 | Value matched; key updated |
0 | Value did not match; key unchanged |
-1 | Key does not exist |
| Error | Other 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
| Parameter | Description |
|---|---|
key | Target key of the string type |
value | Expected current value of the key |
Return value
| Value | Meaning |
|---|---|
1 | Value matched; key deleted |
0 | Value did not match; key unchanged |
-1 | Key does not exist |
| Error | Other 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)