Working with Streams

1. Adding Stream Entries

CommandOptions
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

CommandDescription
XRANGE key start end [COUNT n]Range, use - and + for bounds
XREVRANGE key end startReverse order
XREAD [COUNT n] [BLOCK ms] STREAMS k1 k2 id1 id2Use $ for last-id (only new entries)

3. Getting Stream Length

CommandReturns
XLEN keyEntry count

4. Trimming Streams

CommandOptions
XTRIM key MAXLEN [~|=] nBy size; ~ approximate (fast)
XTRIM key MINID [~|=] idDrop entries with id < threshold
XTRIM ... LIMIT countCap eviction per call

5. Deleting Stream Entries

CommandNotes
XDEL key id [id ...]Tombstones the entry; macro-node space reclaimed on rewrite

6. Creating Consumer Groups

CommandDescription
XGROUP CREATE key group id|$ [MKSTREAM]$ = only new messages
XGROUP SETID key group idReset starting position
XGROUP DESTROY key groupDelete group
XGROUP CREATECONSUMER key group consumerPre-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
IDMeaning
>Undelivered messages only
0 / specific idReplay pending entries for this consumer

8. Acknowledging Messages

CommandDescription
XACK key group id [id ...]Remove from PEL on successful processing

9. Getting Pending Messages

CommandReturns
XPENDING key groupSummary: count, min/max id, consumers
XPENDING key group [IDLE ms] start end count [consumer]Detailed list

10. Claiming Pending Messages

CommandDescription
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

CommandDetail
XINFO STREAM key [FULL]Length, last ID, groups
XINFO GROUPS keyPer group: pending, lag
XINFO CONSUMERS key groupPer consumer activity

12. Managing Consumers

CommandDescription
XGROUP DELCONSUMER key group consumerRemove consumer (re-claims its pending)
XINFO CONSUMERS key groupInactive vs active

13. Setting Stream ID

CommandNotes
XADD key ms-seq f vCustom ID, must be > last-id
XSETID key id [ENTRIESADDED n] [MAXDELETEDID id]Adjust last-id metadata 7.0+