Use XADD to append an entry to a stream, creating the stream when it does not exist.
An entry is a set of field and value pairs identified by an ID of the form <milliseconds>-<sequence>. Passing * lets the server build the ID from the current time, which guarantees that IDs only ever increase; an explicit ID must be greater than the last one in the stream. The reply is the ID the entry was stored under. NOMKSTREAM skips creating a stream that does not exist yet and returns null instead.
The trimming options cap the stream in the same call, which is how a stream is kept from growing without bound. MAXLEN limits the number of entries and MINID drops entries with an ID below a threshold, which is the way to trim by age since IDs start with a timestamp. ~ makes the trim approximate, stopping at a convenient boundary, which is much cheaper than the exact = and is what most workloads should use; LIMIT caps how many entries a single call may evict.
KEEPREF, DELREF, and ACKED decide what happens to consumer group references of the entries that trimming removes: KEEPREF, the default, leaves those references in place, DELREF removes them as well, and ACKED only removes entries that every group has read and acknowledged.
Syntax#
Arguments#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<key> | Yes | No | Redis key targeted by the command. |
NOMKSTREAM | No | No | Do not create a missing stream. |
(KEEPREF | DELREF | ACKED) | No | No | What happens to consumer-group references of the entries that trimming removes: KEEPREF (the default) leaves them in place, DELREF removes them from every group's pending list, and ACKED only removes entries that every group has read and acknowledged. |
(MAXLEN | MINID) [= | ~] <threshold> [LIMIT <count>] | No | No | Trim the stream after the entry is added. MAXLEN caps the number of entries; MINID drops entries with a lower ID. = trims exactly and is the default; ~ trims approximately and is required before LIMIT, which caps how many entries a single call evicts. |
(* | <id>) | Yes | No | Entry ID: * lets the server generate one from the current time, or give an explicit <milliseconds>-<sequence> ID, which must be greater than the stream's last one. |
<field> <value> | Yes | Yes | Field and the value to store in it. Repeat to set several fields in one call. |
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 | Bulk string or Null bulk string or null array |
| RESP3 | Bulk string or Null |
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.