XADD
Appends a new entry to a stream. Creates the stream if it doesn’t exist.Syntax
Parameters
The name of the stream
The entry ID in the format
timestamp-sequence. Use * for auto-generation, or timestamp-* to auto-generate the sequence number.One or more field-value pairs to store in the entry. Must provide at least one pair.
Return Value
Bulk string reply: The ID of the added entry.ID Generation
*- Auto-generates both timestamp (current time in ms) and sequence (0)timestamp-*- Uses the provided timestamp, auto-generates sequencetimestamp-sequence- Uses both provided values
Errors
-ERR The ID specified in XADD must be greater than 0-0-ERR The ID specified in XADD is equal or smaller than the target stream top item
Examples
Implementation Details
Fromstore.go:317-384, the implementation handles:
-
Auto ID generation (line 318-320):
-
Sequence auto-increment (line 331-344): When using
timestamp-*, the sequence is automatically set based on the last entry -
ID validation (line 347-358): Ensures new IDs are greater than the last entry and greater than
0-0 -
Notification (line 379-384): Signals blocked
XREADcommands when new data arrives
XRANGE
Returns a range of entries from a stream between two IDs.Syntax
Parameters
The name of the stream
Minimum entry ID (inclusive). Use
- for the smallest ID in the stream.Maximum entry ID (inclusive). Use
+ for the largest ID in the stream.Return Value
Array reply: List of entries, where each entry is a two-element array:- The entry ID
- An array of field-value pairs
Examples
Implementation Details
Fromstore.go:385-426, the implementation:
- Handles special bounds (line 388-390): Converts
+to maximum possible ID - Parses IDs (line 387-402): Splits IDs into timestamp and sequence components
- Filters entries (line 408-422): Includes entries where both timestamp and sequence are within range
XREAD
Read data from one or multiple streams, optionally blocking until new data arrives.Syntax
Parameters
Block for the specified number of milliseconds if no data is available. Use
0 to block indefinitely until new data arrives. Must be lowercase.One or more stream keys to read from
The ID to start reading from for each stream. Use
$ to read only new entries (greater than the last entry), or a specific ID like 0-0 to read all entries, or any ID to read entries after that ID.Return Value
Array reply: For each stream, returns a two-element array:- The stream name
- An array of entries
nil if the timeout expires with no new data.
Examples
Blocking Behavior
When using theblock option:
- With timeout (
block 1000): Waits up to the specified milliseconds for new data - Without timeout (
block 0): Waits indefinitely until new data arrives - Non-blocking (no
block): Returns immediately with available data
Implementation Details
Fromstore.go:427-506, key features:
-
Blocking implementation (line 434-453):
-
Special
$handling (line 469-479): Returns only the most recent entry - Threshold filtering (line 482-500): Returns only entries with IDs greater than the specified threshold