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#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
(NX | XX) | No | No | Choose one form: NX (only add new members, never update an existing one); XX (only update members that already exist). |
(GT | LT) | No | No | Choose 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. |
CH | No | No | Count changed members rather than only new members. |
INCR | No | No | Increment one member instead of adding normally. |
<score> <member> | Yes | Yes | Score followed by the member it applies to. Repeat to add several members. |
Important points#
NXcannot be combined withXX,GT, orLT, andGTandLTcannot 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.
| Protocol | Reply |
|---|---|
| RESP2 | Null bulk string or null array or Integer or Bulk string containing a number |
| RESP3 | Null or Integer or Double |
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.