Working with Streams
1. Adding Stream Entries
| Command | Options |
|---|---|
XADD key [NOMKSTREAM] [MAXLEN|MINID ~ n] * f v [f v ...] | * auto-generates ID; ~ approximate trim |
XADD events MAXLEN ~ 10000 * type "login" user "42"
2. Reading Stream Entries
| Command | Description |
|---|---|
XRANGE key start end [COUNT n] | Range, use - and + for bounds |
XREVRANGE key end start | Reverse order |
XREAD [COUNT n] [BLOCK ms] STREAMS k1 k2 id1 id2 | Use $ for last-id (only new entries) |
3. Getting Stream Length
| Command | Returns |
|---|---|
XLEN key | Entry count |
4. Trimming Streams
| Command | Options |
|---|---|
XTRIM key MAXLEN [~|=] n | By size; ~ approximate (fast) |
XTRIM key MINID [~|=] id | Drop entries with id < threshold |
XTRIM ... LIMIT count | Cap eviction per call |
5. Deleting Stream Entries
| Command | Notes |
|---|---|
XDEL key id [id ...] | Tombstones the entry; macro-node space reclaimed on rewrite |
6. Creating Consumer Groups
| Command | Description |
|---|---|
XGROUP CREATE key group id|$ [MKSTREAM] | $ = only new messages |
XGROUP SETID key group id | Reset starting position |
XGROUP DESTROY key group | Delete group |
XGROUP CREATECONSUMER key group consumer | Pre-create consumer |
7. Reading as Consumer
XREADGROUP GROUP workers w1 COUNT 10 BLOCK 5000 STREAMS jobs >
XREADGROUP GROUP workers w1 COUNT 10 STREAMS jobs 0
| ID | Meaning |
|---|---|
> | Undelivered messages only |
0 / specific id | Replay pending entries for this consumer |
8. Acknowledging Messages
| Command | Description |
|---|---|
XACK key group id [id ...] | Remove from PEL on successful processing |
9. Getting Pending Messages
| Command | Returns |
|---|---|
XPENDING key group | Summary: count, min/max id, consumers |
XPENDING key group [IDLE ms] start end count [consumer] | Detailed list |
10. Claiming Pending Messages
| Command | Description |
|---|---|
XCLAIM key group new-consumer min-idle id [id ...] [JUSTID] | Steal stale messages |
XAUTOCLAIM key group consumer min-idle start [COUNT n] [JUSTID] | Cursor-based auto-claim 6.2+ |
11. Getting Stream Info
| Command | Detail |
|---|---|
XINFO STREAM key [FULL] | Length, last ID, groups |
XINFO GROUPS key | Per group: pending, lag |
XINFO CONSUMERS key group | Per consumer activity |
12. Managing Consumers
| Command | Description |
|---|---|
XGROUP DELCONSUMER key group consumer | Remove consumer (re-claims its pending) |
XINFO CONSUMERS key group | Inactive vs active |
13. Setting Stream ID
| Command | Notes |
|---|---|
XADD key ms-seq f v | Custom ID, must be > last-id |
XSETID key id [ENTRIESADDED n] [MAXDELETEDID id] | Adjust last-id metadata 7.0+ |