Upstash Documentation

XADD

Add entry to stream.
3 min read

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#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
NOMKSTREAMNoNoDo not create a missing stream.
(KEEPREF | DELREF | ACKED)NoNoWhat 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>]NoNoTrim 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>)YesNoEntry 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>YesYesField 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.

ProtocolReply
RESP2Bulk string or Null bulk string or null array
RESP3Bulk string or Null
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