Skip to main content

Usage

Parameters

key
string
required
The key of the stream.
id
string
required
The entry ID. Can be:
  • "*" - Fully automatic ID generation (recommended)
  • "<ms>-<seq>" - Explicit ID (e.g., "1526919030474-55")
  • "<ms>-*" - Auto-generate sequence number for the given millisecond timestamp (Redis 8+)
IDs must be greater than previous entries in the stream.
entries
Record<string, unknown>
required
The field-value pairs to store in the stream entry. Values are automatically serialized to strings.
options
XAddCommandOptions
Optional configuration:

Response

result
string
The ID of the added entry (e.g., "1526919030474-0").Returns null if nomkStream: true and the stream doesn’t exist.

Examples

Add entry with automatic ID

Add multiple entries

Store complex data

Use explicit ID

Trim stream by maximum length

Trim stream by minimum ID

Don’t create stream if it doesn’t exist

Limit trimming operations

Stream IDs Explained

Format

Stream IDs have the format: <milliseconds>-<sequence>
  • milliseconds - Unix timestamp in milliseconds
  • sequence - Sequence number for entries added in the same millisecond
Example: 1678901234567-0
  • Timestamp: 1678901234567 (March 15, 2023)
  • Sequence: 0 (first entry in that millisecond)

Automatic Generation

Using "*" automatically generates an ID:

ID Ordering

IDs must always increase:

Important Notes

Trimming Performance

  • Use comparison: "~" (approximate) for better performance
  • Exact trimming ("=") may block for large deletions
  • Set limit to prevent long blocking operations

Memory Management

Without trimming, streams grow indefinitely. Consider: