Upstash Documentation

MSETEX

Set multiple keys with one expiration.
3 min read

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#

ArgumentRequiredRepeatableDescription
<numkeys>YesNoNumber of key-value pairs that follow. Must be at least 1.
<key> <value>YesYesKey and the value to store in it. Repeat <numkeys> times.
(NX | XX)NoNoChoose 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)NoNoChoose 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#

  • NX and XX are mutually exclusive, and at most one expiration form may be given.
  • The condition is evaluated over the whole group: with NX a single existing key stops the write, and with XX a single missing key does.
  • Without an expiration option the keys are written without a lifetime, discarding any they had. KEEPTTL preserves each key's own current lifetime.
  • EX and PX must be greater than 0. An EXAT or PXAT deadline in the past deletes the keys instead of writing them, and the reply is still 1.
  • 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.

ProtocolReply
RESP2Integer: 1 if the keys were set, 0 if a condition prevented the write
RESP3Integer: 1 if the keys were set, 0 if a condition prevented the write
Note

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
Note

This command is not supported yet in @upstash/redis.

upstash_redis
Note

This command is not supported yet in upstash_redis.

ioredis
node-redis
redis-py
go-redis
jedis
redis-rs