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
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.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 TLSREDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.
Redis CLI
Redis CLI
@upstash/redis
@upstash/redis
This command is not supported yet in
@upstash/redis.upstash_redis
upstash_redis
This command is not supported yet in
upstash_redis.ioredis
ioredis
node-redis
node-redis
redis-py
redis-py
go-redis
go-redis
jedis
jedis
redis-rs
redis-rs