Skip to main content
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

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.
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.
This command is not supported yet in @upstash/redis.
This command is not supported yet in upstash_redis.