Upstash Documentation

ZADD

Add members with scores.
3 min read

Use ZADD to add members with a score to a sorted set, or to change the score of members that are already there.

Members are kept ordered by score, and members with equal scores are ordered lexicographically, which is what makes range queries by score or by member name possible. Scores are double precision floats and accept +inf and -inf. The key is created on first use, and the reply counts the members that were added.

NX only adds new members and never changes an existing score, while XX only updates members that already exist. GT and LT update a score only when the new one is greater or less than the current one, which is how you keep a running maximum or minimum, such as a high score or an earliest deadline, without reading the old value first. CH makes the reply count every member that changed, added or updated, instead of only the new ones. INCR treats the given score as an increment and returns the member's new score, behaving like ZINCRBY for a single member and returning null when a condition prevented the change.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
(NX | XX)NoNoChoose one form: NX (only add new members, never update an existing one); XX (only update members that already exist).
(GT | LT)NoNoChoose one form: GT (only update when the new score is greater than the current one); LT (only update when it is less). Neither blocks new members from being added.
CHNoNoCount changed members rather than only new members.
INCRNoNoIncrement one member instead of adding normally.
<score> <member>YesYesScore followed by the member it applies to. Repeat to add several members.

Important points#

  • NX cannot be combined with XX, GT, or LT, and GT and LT cannot be used together.
  • RESP2 represents floating-point reply values as bulk strings; RESP3 may use native double replies. Client libraries commonly decode either form to a language number.

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
RESP2Null bulk string or null array or Integer or Bulk string containing a number
RESP3Null or Integer or Double
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
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs