xadd
XADD
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Append an entry to a stream
Usage: XADD key ID field value.
The ID must always be greater than the previous ID.
You can set an ID, but '*' (asterisk) is usual so Redis always generates a larger ID.
Example
| Command> | XADD sensor-1234 * temperature 98.7 |
| Result> | 1538319053569-0 |
|
An ID has two numbers: <millisecondsTime>-<sequenceNumber> The first is a millisecond timestamp; sequenceNumber is an 8-byte integer. sequenceNumber increments 0, 1, 2, 3, ... within the same millisecond to avoid duplicates. There is effectively no limit on entries in the same millisecond. ID=* uses the clock of the server running the Redis instance. |
Note after failover
If master A fails and replica B is promoted, XADD is rejected when B's timestamp is behind A's. Keep server clocks in sync.
When you specify an ID
- If the source data already has a value you can use as an ID, specifying it is a good option. For example, if a sensor emits millisecond IDs and Redis receives them once per second, they will not collide.
- You can also generate the first part of the ID with a key.
INCR sensor-1234-timestamp - XADD sensor-0001 0-1 temperature 101
0 or 0-0 cannot be used as an ID. - XADD sensor-0001 1000 temperature 102
You may omit sequenceNumber.
Options
- NOMKSTREAM
Does not create a new key. Appends only to an existing stream. No Make Stream.
This prevents creating a key by mistake.
Added in version 6.2. - MAXLEN
To cap stream length while appending, use MAXLEN as below.
XADD sensor-1234 MAXLEN 10 * temperature 100
Delete and append run as one operation, so this is slower than append-only. There is a special option for that case:
XADD sensor-1234 MAXLEN ~ 10 * temperature 100
~ deletes only when it can do so cheaply. - MINID
threshold is an ID. Entries with an ID less than that ID are deleted.
Added in version 6.2. - LIMIT
Limits how many entries are deleted. Added in version 6.2.
Syntax
XADD key [NOMKSTREAM] [MAXLEN|MINID [=|~] threshold [LIMIT count]] *|ID field value [field value ...]
- Available since version 5.0.0.
- NOMKSTREAM, MINID, and LIMIT were added in version 6.2.
- Time complexity is O(log(N)), where N is the number of entries already in the stream.
- Returns the ID.
| SQL Insert | On Redis Enterprise 7.3.0 or later, try INSERT. |
| << STREAMS Introduction | XADD | XADDS >> |
|---|
Email
답글이 올라오면 이메일로 알려드리겠습니다.
