Use MSETEX to set several keys and give them all the same expiration in one atomic step.
MSET can write a group of keys together but leaves them without a lifetime, so attaching one has to be a second round trip during which the keys already exist unbounded. MSETEX closes that window: the values and the expiration are applied together, which is what you want for a set of keys that are only meaningful as a group, such as the parts of one cached response.
NX writes only when none of the keys exists and XX only when all of them do. The check covers the whole group, so a single key breaking the condition leaves everything unwritten and the reply is 0. EX, PX, EXAT, and PXAT set the lifetime as a duration or as an absolute deadline, and KEEPTTL keeps whatever lifetime each key already had. Without any of these the keys are written without an expiration, discarding any they had, exactly as MSET does.
<numkeys> says how many key-value pairs follow; anything after them is read as options.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<numkeys> | Yes | No | Number of key-value pairs that follow. Must be at least 1. |
<key> <value> | Yes | Yes | Key and the value to store in it. Repeat <numkeys> times. |
(NX | XX) | No | No | Choose one form: NX (write only when none of the keys exists); XX (write only when all of them exist). |
(EX <seconds> | PX <milliseconds> | EXAT <unix-time-seconds> | PXAT <unix-time-milliseconds> | KEEPTTL) | No | No | Choose one form: EX (set a lifetime in seconds); PX (set a lifetime in milliseconds); EXAT (expire at a Unix timestamp in seconds); PXAT (expire at a Unix timestamp in milliseconds); KEEPTTL (preserve each key's existing lifetime). |
Important points#
NXandXXare mutually exclusive, and at most one expiration form may be given.- The condition is evaluated over the whole group: with
NXa single existing key stops the write, and withXXa single missing key does. - Without an expiration option the keys are written without a lifetime, discarding any they had.
KEEPTTLpreserves each key's own current lifetime. EXandPXmust be greater than0. AnEXATorPXATdeadline in the past deletes the keys instead of writing them, and the reply is still1.- The options may be given in any order after the key-value pairs.
Response#
The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.
| Protocol | Reply |
|---|---|
| RESP2 | Integer: 1 if the keys were set, 0 if a condition prevented the write |
| RESP3 | Integer: 1 if the keys were set, 0 if a condition prevented the write |
Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.
Examples#
TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.
Redis CLI
@upstash/redis
This command is not supported yet in @upstash/redis.
upstash_redis
This command is not supported yet in upstash_redis.