Streams and consumer groups
XADD, XREAD, consumer groups, pending entries and acknowledgements.
A Redis Stream stores append-only entries ordered by a two-part identifier (milliseconds-sequence). Each entry contains an ordered list of binary field/value pairs. Consumer groups allow multiple workers to partition a stream, track unacknowledged work in a Pending Entries List (PEL), and transfer stalled entries.
For an end-to-end transactional example combining SQL tables with Redis Streams, see the Transactional outbox tutorial.
Entries, IDs, and trimming
- Full 128-bit ID domain: Stream IDs consist of two unsigned 64-bit integers (
ms-seq).pgredispreserves the fulluint64range for both components insideredis._stream. - Entry payload fidelity: Field/value pairs retain their exact insertion order, duplicate field names within the same entry, and arbitrary binary bytes (including
NUL). - Write, read, and trim commands: Use
XADDto append entries (NOMKSTREAM,MAXLEN,MINID),XRANGEandXREVRANGEto query ID intervals (-,+, exclusive(bounds,COUNT),XLENto read the entry count, andXDELorXTRIMto remove entries. - Approximate trimming and
XSETID: Approximate trimming (MAXLEN ~orMINID ~) follows internal stream node boundaries configured bystream-node-max-entriesandstream-node-max-bytes, so it may retain more entries than the requested threshold.XSETIDupdates stream metadata (last_generated_id,entries_added,max_deleted_entry_id) without inserting an entry or waking blocked readers.
Consumer groups and pending entries
- Group lifecycle:
XGROUP CREATE(MKSTREAM,ENTRIESREAD),XGROUP SETID,XGROUP DESTROY,XGROUP CREATECONSUMER, andXGROUP DELCONSUMERmanage consumer group state inredis._stream_group. - Reading and acknowledging:
XREADGROUP GROUP <group> <consumer> ... STREAMS <key> >delivers new entries and records them inredis._stream_pelunlessNOACKis specified.XACKremoves acknowledged entries from the PEL. - Inspecting and claiming stalled work:
XPENDINGinspects unacknowledged entries by ID range, idle time (IDLE), and consumer.XCLAIMandXAUTOCLAIMtransfer ownership of pending entries to another consumer and increment delivery counters. - Deleted entries in the PEL: If an entry is deleted with
XDELwhile still referenced inredis._stream_pel, historicalXREADGROUP(0) orXCLAIMcalls return a nil/empty payload for that ID, matching Redis 7.2.4. Known gaps inv0.1.0-rc1:XINFO GROUPScomputeslagfrom stream length rather than adjusting forXDEL, andXAUTOCLAIMhas one cursor formatting difference when draining an empty queue (99/101inunit/type/stream-cgroups; see Status).
Blocking reads
- RESP connections:
XREAD BLOCK <ms>andXREADGROUP BLOCK <ms>wait for matchingXADDcommits. InsideMULTI/EXEC,BLOCKoptions run non-blockingly as immediate reads; external clients blocked on a stream wake only after anEXECbatch or SQL transaction commits. - SQL transactions: Ordinary
redis.commandcalls cannot block and commit a caller’s transaction while waiting. SQL callers useredis.blocking_begin,redis.blocking_poll, andredis.blocking_cancelacross transactions. - Restart behavior: Stream entries, consumer group offsets (
last_delivered_id), and PEL records persist in PostgreSQL WAL and survive restarts andpg_dump/pg_restore, whereas activeBLOCKnetwork waits are transient and require clients to reconnect.
